PHP,SQL限制通过php变量查询 [英] PHP, SQL limit query by php variable

查看:73
本文介绍了PHP,SQL限制通过php变量查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

定义变量sqlshowvalue的PHP代码

PHP code defining variable sqlshowvalue

$sqlshowvalue = 5;
if(isset($_POST['showmore'])) {
     $sqlshowvalue += 5;
}

所以我连接到数据库,然后当我使用上面刚刚定义的变量在下面运行此SQL查询时,

So I connect to my database and then when I run this SQL query below using the variable that I just defined above,

$result = mysqli_query($conn,"SELECT * FROM comments ORDER BY id DESC limit '$sqlshowvalue'");

所以我使用mysqli作为连接到数据库的方法,它给了我以下错误:

So I am using mysqli as the method to connect to my DB and it gives me the following error:

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in ..

之所以给我这个错误,是因为查询中有错误,它必须执行$ sqlshowvalue,因为如果我仅用数字5替换sqlshowvalue(如下所示),它就可以正常工作: /p>

The reason it gives me this error is because something in my query is wrong and what it has to do is $sqlshowvalue, because if I replace sqlshowvalue with with just the number 5 (like shown below), it works fine:

$result = mysqli_query($conn,"SELECT * FROM comments ORDER BY id DESC limit 5");

所以我只是想知道如何做才能使limit的值是一个可以更改并更新页面的PHP变量.

So I am just wondering what I can do to make it so that the value for the limit is a PHP variable that I can be changed and the page updated.

推荐答案

您尝试过制作

$result = mysqli_query($conn,"SELECT * FROM comments ORDER BY id DESC limit '$sqlshowvalue'");

$result = mysqli_query($conn,"SELECT * FROM comments ORDER BY id DESC limit ".$sqlshowvalue);

这篇关于PHP,SQL限制通过php变量查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆