alberzyzz alberzyzz - 1 year ago 58
PHP Question

There was an error running the query [Unknown column 'test' in 'where clause']

I have a database with a row called "username". I need to show the value of an other row depending on the username that is logged in. To do so, I have the following script:

$db = new mysqli('', '', '', '');

if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');

$sql = <<<SQL
SELECT * FROM `users` WHERE username = $username

if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');

while($row = $result->fetch_assoc()){
$text_cli = $row['text_cli'];
$text_trad = $row['text_trad'];

And the value of
is stored previously.

And finally:

<div id="text_cli"><?php echo $text_trad ?></div>
<div id="text_cli"><?php echo $text_cli ?></div>

But I get this error: There was an error running the query [Unknown column 'test' in 'where clause'].

"test" is a fake username created to test the code.

Thank you for your help.

Answer Source

Just put your username variable inside quotes in your SQL like this: '$username'

SELECT * FROM `users` WHERE username ='$username'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download