Question

I am trying to use a mssql Query to retrieve an id from a table row.

This is the code I am trying to use:

$username = $_COOKIE['ID_my_site']; 
    $sql ="SELECT id FROM users WHERE username = ".$username."";
    $query = mssql_query($sql, $conn);
    $array = mssql_fetch_assoc($query);
    $acc_id=stripslashes($array['id']);

    var_dump ($sql);
echo '<script>
alert("'.$acc_id.'");
</script>';

When I use this though, the sql is correct, from what I see using var_dump. But the alert from JavaScript is blank.

How can I get the alert to display the data from the id column?

The id data should be 5.

Thank you for any help, all help is appreciated.

If you +1 my question I will +1 your answer.

I will +1 an answer if I choose it as best answer, regardless if you +1 my question or not!

Était-ce utile?

La solution

Missing quotes in SQL query, try following

$sql ="SELECT id FROM users WHERE username = '".$username."'";
                                             ^ quotes      ^
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top