尝试从随机选择的ID中显示mySQL数据

时间:2014-03-21 16:03:51

标签: php mysql variables

我有一个包含3个字段的表:ID,标题和正文。我正在尝试随机选择一个id,然后显示该记录的标题和正文。我预览页面时没有出现任何错误,但我在页面上也看不到任何内容。我是PHP和mySQL的新手,我正在试图找出导致这种情况的原因。感谢您的任何见解。

`$con = mysql_connect("x","x","x");
    if (!$con) {
      die ("Can not connect: " . mysql_error());
    }

mysql_select_db("haipoos", $con);

//select a random id
$randHaipoo = "SELECT 'id' FROM `haikus` ORDER BY RAND() LIMIT 0,1;";

//select the entire record based on random id
$selectHaipoo = "SELECT * FROM 'haikus' WHERE 'id' = '$randHaipoo'";

//store query results
$haipoo = mysql_query($selectHaipoo, $con);

//display query results
while($record = mysql_fetch_array($haipoo)){
    echo $record['title'];
    echo $record['body'];
}`

1 个答案:

答案 0 :(得分:0)

尝试这种方式:

//select a random id
$selectHaipoo = "SELECT * FROM `haikus` ORDER BY RAND() LIMIT 0,1;";

//select the entire record based on random id
//$selectHaipoo = "SELECT * FROM 'haikus' WHERE 'id' = '$randHaipoo'";

@ spencer7593绝对正确,在你的下一步知识中遵循他的建议