dongyuxiao6295 2013-07-23 19:33
浏览 32

mysql查询返回null时不应该

I use this PHP code to get value from my DB.

My problem is that i got null result.

I check for !empty($result) and for mysql_num_rows($result) but still i got null result.

I tested exactly the same query i use in my code on my phpmyadmin and it working.

The echo from $response["SQL"] is "good".

Here is my PHP code:

$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);

Solved

I added this line to fix it:

$row = mysql_fetch_array($result);
  • 写回答

4条回答 默认 最新

  • duanniu4106 2013-07-23 19:40
    关注

    You can't just return the mysql_query results. You must fetch the data.

    $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);
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度