douxiajiao8445 2018-07-07 17:09
浏览 66
已采纳

为什么我不能在mysql_fetch_array的select查询中使用where子句? [重复]

This question already has an answer here:

It may sound like noob question but I think ive tried everything and ive been stuck for weeks with this not making any progress.

This is my working code:

//$connect holds my db login details so ill hide it
$query = '
SELECT *,UNIX_TIMESTAMP(CONCAT_WS(" ", ranking_data_date, ranking_data_time)) AS datetime
FROM tbl_ranking_data
ORDER BY ranking_data_date ASC, ranking_data_time ASC LIMIT 60
';

$result = mysqli_query($connect, $query);
$rows = array();
$table = array();
$table['cols'] = array(
 array(
  'label' => 'Date Time',
  'type' => 'datetime'
 ),
 array(
  'label' => 'Position',
  'type' => 'number'
 )
);

while($row = mysqli_fetch_array($result))

My problem is that it always selects same data (and all of it) from database.

To fix it, ideally it would be to insert where clause for example:

WHERE user_id=$id_of_current_user

But I can't enter that or anything or i will get error.

Basically code same as above but with this difference:

//$connect holds my db login details so ill hide it
$query = '
SELECT *,UNIX_TIMESTAMP(CONCAT_WS(" ", ranking_data_date, ranking_data_time)) AS datetime
FROM tbl_ranking_data
WHERE user_id=$id_of_current_user
ORDER BY ranking_data_date ASC, ranking_data_time ASC LIMIT 60
';

Shows this error:

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /files/phpfile.php on line 32

My understanding: query fails and it returns false (thats why error talks about boolean) instead of array ($result) but it shouldnt fail!

it only fails if I add WHERE condition.

Any help appreciated.

How can I insert where clause to this code?

</div>
  • 写回答

1条回答 默认 最新

  • doulu7258 2018-07-07 17:24
    关注

    The problem with your query isn't the where clause, it's the variable in your where clause. If you echo your query when having an issue like this, you'll be able to see that the query doesn't have the correct syntax.

    PHP doesn't substitude variable values in string that use single quotes. Use double quotes instead.

    Also, use backticks to quote column names and table names. This will avoid problems with reserved words.

    Lastly, use parameterized queries to avoid data quoting issues as well as SQL Injection.

    //  $connect holds my db login details so I'll hide it
    $query = "
    SELECT 
        *,
        UNIX_TIMESTAMP(CONCAT_WS(' ', `ranking_data_date`, `ranking_data_time`)) AS `datetime`
    FROM `tbl_ranking_data`
    WHERE `user_id` = $id_of_current_user
    ORDER BY `ranking_data_date` ASC, `ranking_data_time` ASC 
    LIMIT 60;
    ";
    
    $result = mysqli_query($connect, $query);
    $rows = array();
    $table = array();
    $table['cols'] = array(
        array(
            'label' => 'Date Time',
            'type' => 'datetime'
         ),
        array(
            'label' => 'Position',
            'type' => 'number'
         )
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示