php - php中的mysql错误

标签 php sql database mysql

我正在尝试运行此 php 代码,该代码应显示来自 mysql 的引用,但无法弄清楚哪里出错了。结果变量为 null 或空。有人可以帮我吗。谢谢!

<?php
include 'config.php';

// 'text' is the name of your table that contains
// the information you want to pull from
$rowcount = mysql_query("select count(*) as rows from quotes");

// Gets the total number of items pulled from database.
while ($row = mysql_fetch_assoc($rowcount))
{
$max = $row["rows"];
//print_r ($max);
}

// Selects an item's index at random 
$rand = rand(1,$max)-1;
print_r ($rand);
$result = mysql_query("select * from quotes limit $rand, 1") or die ('Error: '.mysql_error());
if (!$result or mysql_num_rows($result))
{
echo "Empty";
}
else{
while ($row = mysql_fetch_array($result)) {

$randomOutput = $row['cQuotes'];
echo '<p>' . $randomOutput . '</p>';
}
}

最佳答案

$result = mysql_query("SELECT * FROM quotes ORDER BY rand() LIMIT 1") or die ('Error: '.mysql_error());
if (!$result || mysql_num_rows($result) == 0)
    echo "Empty";
else {
    while ($row = mysql_fetch_array($result)) {
        $randomOutput = $row['cQuotes'];
        echo '<p>' . $randomOutput . '</p>';
    }
}

关于php - php中的mysql错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2594090/

相关文章:

Javascript 复选框将另一个表中的值添加到价格中

php - PHP\Laravel 中的符号链接(symbolic link)问题

python-3.x - 连接 Cassandra 和 Python 的有效方法(抑制警告)

java - Hibernate:使用相同连接列的多个关系

sql - Postgres 和部分日期

android - Stetho,没看到检查链接

php - Codeigniter V 其他 PHP 框架的可扩展性如何?

php - 将命名空间从 SOAP 信封根移动到 xml 负载

MySQL - 查询输出合并数据

sql - 将R Vector传递给Sql查询