计算MySQL行数

时间:2011-03-31 17:38:57

标签: php mysql html

我试图计算MySQL数据库中的行数并使用PHP报告该数字。我已经阅读了所有文档,我认为这段代码应该正常工作,但无论我尝试什么,都会失败。

<?php
session_start();
if ( isset($_SESSION['username']) ) {
    $username = $_SESSION['username'];
    $q = "SELECT * FROM messages WHERE recepiants='$username' AND readcheck='T'";
    $count = mysql_num_rows($q);
?>
<div class="user_info">Logged in as <?php echo $username; ?> | <a href="admin.php">Add a post</a> | <a href="experiment.php">View Posts</a> | <a href="inbox.php">Message Center <?php echo $count; ?> </a> | <a href="logout.php">Log Out</a></div>
<?php } else {?> 

脚本成功报告$username,但$count未返回数字。有任何想法吗?我在某个地方错过了支架或分叉吗?

9 个答案:

答案 0 :(得分:8)

mysql_num_rows需要查询资源 - 您提供的是字符串。您需要先执行查询,然后才能了解其结果。

$sql = "SELECT * FROM messages WHERE recepiants='$username' AND readcheck='T'";
if(!$query = mysql_query($sql))
  trigger_error(mysql_error().' SQL: '.$sql);
$count = mysql_num_rows($query);

我怀疑上面的代码也会根据您在查询中的“收件人”错误生成错误。请务必检查mysql_query的返回值。

编辑:正如jeroen所说,如果你粘贴的是该页面的整个代码,那么你就忽略了打开数据库连接。请参阅mysql_connectmysql_select_db

答案 1 :(得分:6)

您缺少的是运行实际查询:

<?php
session_start();
if ( isset($_SESSION['username']) ) {
    $username = $_SESSION['username'];
    $q = "SELECT * FROM messages WHERE recepiants='$username' AND readcheck='T'";
    $result = mysql_query($q);
    $count = mysql_num_rows($result);
?>
<div class="user_info">Logged in as <?php echo $username; ?> | <a href="admin.php">Add a post</a> | <a href="experiment.php">View Posts</a> | <a href="inbox.php">Message Center <?php echo $count; ?> </a> | <a href="logout.php">Log Out</a></div>
<?php } else {?> 

如果您需要的话,可能更好的方法是在查询中运行COUNT

<?php
session_start();
if ( isset($_SESSION['username']) ) {
    $username = $_SESSION['username'];
    $q = "SELECT COUNT(*) AS Count FROM messages WHERE recepiants='$username' AND readcheck='T'";
    $result = mysql_query($q);
    $result = mysql_fetch_assoc($result)
    $count = $result['Count'];
?>
<div class="user_info">Logged in as <?php echo $username; ?> | <a href="admin.php">Add a post</a> | <a href="experiment.php">View Posts</a> | <a href="inbox.php">Message Center <?php echo $count; ?> </a> | <a href="logout.php">Log Out</a></div>
<?php } else {?> 

而且,正如其他人所说,您需要一个活动的数据库连接。

http://www.php.net/manual/en/function.mysql-fetch-assoc.php

答案 2 :(得分:4)

您实际上没有执行过您的查询。

$q = "SELECT * FROM messages WHERE recepiants='$username' AND readcheck='T'";
$result = mysql_query($q);
if ($result) {
  $count = mysql_num_rows($result);
}

答案 3 :(得分:3)

这是错误的(需要实际查询):

$q = "SELECT * FROM messages WHERE recepiants='$username' AND readcheck='T'";
$count = mysql_num_rows($q);

它应该是:

$q = "SELECT * FROM messages WHERE recepiants='$username' AND readcheck='T'";
$result = mysql_query($q, $connection);
$count = mysql_num_rows($result);

答案 4 :(得分:2)

您正在评估$ q作为字符串,您在$ q上缺少mysql_query()

答案 5 :(得分:2)

您忘了运行查询。

$res = mysql_query($q);
$count = mysql_num_rows($res);

答案 6 :(得分:1)

除了运行实际查询之外还要检查一些事项:

  1. 是真正称为recepiants的列,还是recipients
  2. 你打开过数据库连接吗?

答案 7 :(得分:1)

查询是:

SELECT count(*) FROM table

答案 8 :(得分:-2)

这是简单的代码,我相信它对许多人都有效。当前代码将检查数据库中是否已存在vaule,如果已存在则不会将记录添加到数据库中,如果它从未存在,则会将记录添加到数据库中。

<?php
    $chkval =52364; 

    $con = mysql_connect("servername","username","password");

    if (!$con)
    {
        die('Could not connect: ' . mysql_error());
    }

    mysql_select_db("databasename", $con);

    $result = mysql_query("SELECT column_name FROM table_name where column_name = '$chkval'");

    if(mysql_num_rows($result)>0)
        {
            echo "There are records";

        }
        else
        {
            mysql_query("INSERT INTO tablename (column_name)
                VALUES ('$chkval')");

                                echo "record added";

        }
            mysql_close($con);

 ?>