简体   繁体   中英

Unknown column 'id' in 'where clause'

Any idea why i might be getting this error when submitting to a DB?

Unknown column 'id' in 'where clause' Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/somebody/public_html/sendmessage.php on line 41

while($success == FALSE) { 
$rand = rand(100000, 999999); 

$q = "SELECT * FROM $tablename WHERE id = '$rand'"; 
$r = mysql_query($q, $link);

echo mysql_error();


if(mysql_num_rows($r)) { **THIS IS LINE 41** 
    continue; 
} else { 
    $success = TRUE; 
} 
} 

Your table, whose name is stored in $tablename , does not have a column named id . This makes the query fail, and all following database functions will also fail.

That doesn't look too nice there,

first make sure you HAVE a column "id" in that table at all. and beware "id" is NOT "Id" nor "ID" nor "iD"! After being absolutely sure you HAVE that column, and still getting an error, try it with quatation marks:

$q = "SELECT * FROM ". $tablename ." WHERE 'id'='". $rand."'";

Try something like this. Thanks

while($success == FALSE) { $rand = rand(100000, 999999);

$q = "SELECT * FROM ". $tablename ." WHERE id = '". $rand ."'"; 
$r = mysql_query($q, $link);
$num_rows = mysql_num_rows($r);

echo mysql_error();


if($num_rows > 0) { **THIS IS LINE 41** 
    continue; 
} else { 
    $success = TRUE; 
} 
} 

Cheers!

Unknown column 'id' in 'where clause'

I ran into this error too, I discovered I was calling a different table that doesn't have id attributes. So I suggest you check your table name

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