简体   繁体   中英

How to retrieve column data from an MSSQL Query?

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!

Missing quotes in SQL query, try following

$sql ="SELECT id FROM users WHERE username = '".$username."'";
                                             ^ quotes      ^

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM