0

我使用这个 PHP 代码从我的数据库中获取价值。

我的问题是我得到了空结果。

我检查 !empty($result) 和 mysql_num_rows($result) 但我仍然得到空结果。

我测试了我在 phpmyadmin 上的代码中使用的完全相同的查询,并且它可以正常工作。

来自的回声$response["SQL"]是“好”的。

这是我的PHP代码:

$result = mysql_query("SELECT * FROM workouts_wall WHERE workout_name = 'WO13' AND user = 'tomer2'") or die (mysql_error());


// mysql inserting a new row
 if (!empty($result))
 {
    if (mysql_num_rows($result) > 0) 
    {
        $response["SQL"] = "good";
    }
    else
    {
        $response["SQL"] = "bad";
    }
}

else    
{
    $response["SQL"] = "bad";
}   


$response["is null?"] = $result;
    // echoing JSON response
    echo json_encode($response);

解决了

我添加了这一行来修复它:

$row = mysql_fetch_array($result);
4

4 回答 4

1

你不能只返回mysql_query结果。您必须获取数据。

$data = array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
    $data[] = $row;

echo(json_encode($data));

//EDIT: Also a good idea when you are done...
mysql_free_result($result);
于 2013-07-23T19:40:47.963 回答
0

mysql_query()函数返回资源或布尔值。如果您的查询成功(在这种情况下可能是这样),您仍然只是将 MySQL 资源分配给您的$response变量。您需要实际使用mysql_fetch_array()mysql_fetch_assoc()提取返回的数据。

关于不使用mysql_*()函数:为什么我不应该在 PHP 中使用 mysql_* 函数?

于 2013-07-23T19:43:27.273 回答
0

尝试这个。希望它有效:

$result = mysql_query("SELECT * FROM workouts_wall WHERE workout_name = 'WO13' AND user = 'tomer2'") or die (mysql_error());


    // mysql inserting a new row
     if (!empty(mysql_fetch_array($result)))
     {
        if (mysql_num_rows(($result) > 0) 
        {
            $response["SQL"] = "good";
        }
        else
        {
              $response["SQL"] = "bad";
        }
    }

    else    
    {
        $response["SQL"] = "bad";
    }   


    $response["is null?"] = $result;
        // echoing JSON response
        echo json_encode($response);
于 2013-07-23T19:44:05.923 回答
0

没有行的 mysql 结果不为。这是一个标准的 mysql 结果句柄,只是碰巧实际上不包含任何行。这不是一个错误,它不是一个空字符串,它不是一个空数组......它是一个标准的结果句柄,就像任何其他结果句柄一样。

你的代码应该是

$result = mysql_query($sql) or die(mysql_error()); // catch the REAL errors
if (mysql_num_rows($result) == 0) { 
   ... result set is empty
}
于 2013-07-23T19:49:39.827 回答