1

After almost a day of researching I cannot really see what the error with my code is. I am trying to create a small prototype function that returns the number of rows of query results.

The error I get is: Uncaught PDOException: SQLSTATE[42000]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]The multi-part identifier "my@email.com" could not be bound. in C:\inetpub\wwwroot\index.php:59

Row 59 being $stmt->execute();.

I believe the issue comes from my SQL query but I am not 100% sure.

The way the code works is by getting values from a POST input and it checks in a row and column if it contains said value in a schema.

Additionally, I am using MSSQL(not MySQLi) and PHP for the task.


This is the main bit where I want the php code to send an SQL query and the check if the result exists.

<?php  
    include "connect.php";

    if(isset($_POST['login']))
    {
        $email = $_POST['EMAIL'];
        $password = $_POST['PASSWORD']; 

        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $stmt = $conn->prepare("SELECT * FROM users WHERE EMAIL=$email;"); 
        $stmt->execute();

        $count= $stmt->rowCount();
        echo "Row count: $count \n";
    }
?> 

Just to mention I have tried all kinds of changes to the SQL query like:

$stmt = $conn->prepare("SELECT * FROM users WHERE EMAIL='".$email."';");

and

$stmt = $conn->prepare("SELECT * FROM users WHERE EMAIL=?;");

with an array input that had $email in it.

All returned the same identical error.


Here I have the connect.php, just in case the issue might come from here.

<?php 
        $servername = "localhost";
        $username = "sa";
        $password = ""; 

        try 
        {
            $conn = new PDO("sqlsrv:Server=$servername; Database=db", $username, $password);
            // set the PDO error mode to exception
            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            echo "Connected!";
            return $conn;
        }

        catch(PDOException $e)
        {
            echo "Connection failed: " . $e->getMessage();
        }
?>

1 Answers1

0

Basically the way I solved the problem was to have the EMAIL and PASSWORD converted to VARCHAR's:

$stmt = $conn->prepare("SELECT ID, EMAIL, LAST_NAME FROM users WHERE CONVERT(VARCHAR, EMAIL)='my@email.com' AND CONVERT(VARCHAR, PASSWORD)='1234'");