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'
         )
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 求图像处理的matlab方案
  • ¥50 winform中使用edge的Kiosk模式
  • ¥15 关于#python#的问题:功能监听网页
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信