mysql_num_rows每次都返回null

时间:2016-05-30 05:49:52

标签: php

if (isset($_GET["val"])) {
    $val = $_GET["val"];
    $sql1 = "select count(*) as count from staff_log l where l.time_in is null and l.time_out is not null and l.date_today = curdate() and l.staff_id = ".$val.";";

    $result1 = mysql_num_rows($sql1);
}

4 个答案:

答案 0 :(得分:2)

添加此行...

$result = mysql_query( $sql1);
$result1 = mysql_num_rows($result);

您必须在mysql_query()函数中定义查询;

答案 1 :(得分:0)

mysql_num_rows()返回,如PHP manual

所示
  

成功时结果集中的行数,如果失败则为FALSE。

这意味着执行它时出错,这显然是因为您没有运行任何查询(mysql_query())。

请注意,php手册在页面顶部也有以下警告:

  

此扩展在PHP 5.5.0中已弃用,并已在PHP 7.0.0中删除。相反,应该使用MySQLi或PDO_MySQL扩展。

答案 2 :(得分:0)

首先执行查询,然后执行num_rows

  

现在不推荐使用原始MySQL扩展,并且在连接到数据库时会生成E_DEPRECATED错误。而是使用MYSQLi或PDO_MySQL扩展。

试试这个

if (isset($_GET["val"]))
{
$val = $_GET["val"];
$sql1 = "select count(*) as count from staff_log l where l.time_in is null and l.time_out is not null and l.date_today = curdate() and l.staff_id = ".$val.";";

$result = mysql_query($sql1);
$count = mysql_num_rows($result);

}

答案 3 :(得分:0)

您几乎是对的,请查看以下代码。您需要使用 mysql_query() mysql_query()用于执行默认数据库的查询。

if (isset($_GET["val"])) 
{
    $val = $_GET["val"];
    $sql1 = "select count(*) as count from staff_log l where l.time_in is null and l.time_out is not null and l.date_today = curdate() and l.staff_id ='$val'";
    $sql2 = mysql_query($sql1); // Execute the query first 
    $result1 = mysql_num_rows($sql2); // Get the number of rows from executed query result.
    echo "The count is $result1";
}