1

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!

2
  • 1
    Your $username variable is not enclosed in quotes in your query. Also, this is vulnerable to SQL injection - use parameters in your queries. Commented Apr 8, 2014 at 16:47
  • 1
    Just got a Serial upvoting reversed on me :) -70 rep. Still its a net gain. :) That was a fun ride. Commented Apr 9, 2014 at 6:02

1 Answer 1

1

Missing quotes in SQL query, try following

$sql ="SELECT id FROM users WHERE username = '".$username."'";
                                             ^ quotes      ^
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.