PHP code defining variable sqlshowvalue
$sqlshowvalue = 5;
if(isset($_POST['showmore'])) {
$sqlshowvalue += 5;
}
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'");
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 ..
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");
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.
Have you tried making
$result = mysqli_query($conn,"SELECT * FROM comments ORDER BY id DESC limit '$sqlshowvalue'");
to
$result = mysqli_query($conn,"SELECT * FROM comments ORDER BY id DESC limit ".$sqlshowvalue);
Remove '' use only $sqlshowvalue:-
$result = mysqli_query($conn,"SELECT * FROM comments
ORDER BY id DESC limit $sqlshowvalue");
for integer value no need of ''
Related
I thought this would be simple but I'm having a tough time figuring out why this won't populate the the data array.
This simple query works fine:
$queryPrice = "SELECT price FROM price_chart ORDER BY id ASC LIMIT 50";
$resultPrice = mysqli_query($conn, $queryPrice);
$data = array();
while ($row = mysqli_fetch_array($resultPrice)) {
$data[] = $row[0];
}
But instead I want it to choose the last 10 results in Ascending order. I found on other SO questions to use a subquery but every example I try gives no output and no error ??
Tried the below, DOESN'T WORK:
$queryPrice = "SELECT * FROM (SELECT price FROM price_chart ORDER BY id DESC LIMIT 10) ORDER BY id ASC";
$resultPrice = mysqli_query($conn, $queryPrice);
$data = array();
while ($row = mysqli_fetch_array($resultPrice)) {
$data[] = $row[0];
}
I also tried specifying the table name again and using the IN, also doesn't work:
$queryPrice = "SELECT price FROM price_chart IN (SELECT price FROM price_chart ORDER BY id DESC LIMIT 10) ORDER BY id";
$resultPrice = mysqli_query($conn, $queryPrice);
$data = array();
while ($row = mysqli_fetch_array($resultPrice)) {
$data[] = $row[0];
}
In both examples my array is blank instead of returning the last 10 results and there are no errors, so I must be doing the subquery wrong and it is returning 0 rows.
The subquery doesn't select the id column, so you can't order by it in the outer query. Also, MySQL requires that you assign an alias when you use a subquery in a FROM or JOIN clause.
$queryPrice = "SELECT *
FROM (SELECT id, price
FROM price_chart
ORDER BY id DESC LIMIT 10
) x ORDER BY id ASC";
$resultPrice = mysqli_query($conn, $queryPrice) or die (mysqli_error($conn));
$data = array();
while ($row = mysqli_fetch_assoc($resultPrice)) {
$data[] = $row['price'];
}
You would have been notified of these errors if you called mysqli_error() when the query fails.
Your second query is the closest. However you need a table alias. (You would have seen this if you were kicking out errors in your sql. Note you will need to add any field that you wish to order by in your subquery. In this case it is id.
Try this:
SELECT * FROM (SELECT price, id
FROM price_chart ORDER BY id DESC LIMIT 10) as prices
ORDER BY id ASC
You must have errors, because your SQL queries are in fact incorrect.
First, how to tell you have errors:
$resultPrice = mysqli_query (whatever);
if ( !$resultprice ) echo mysqli_error($conn);
Second: subqueries in MySQL need aliases. So you need this:
SELECT * FROM (
SELECT id, price
FROM price_chart
ORDER BY id DESC LIMIT 10
) AS a
ORDER BY id ASC";
See the ) AS a? That's the table alias.
for($nr = 0; $nr < 2; $nr++){
print $nr; print(gettype($nr)); // prints 0integer
$result = mysqli_query($con,"SELECT * FROM phcdl_files
ORDER BY file_id DESC LIMIT '$nr',1")
or die(mysqli_error($con));
}
Trying to run the query above but I'm having troubles because of syntax.
Running it on PhpMyAdmin with Limit 0,1 works good however
Any idea what's the problem?
Try with -
"SELECT * FROM phcdl_files ORDER BY file_id DESC LIMIT $nr,1"
I think the issue is that you're adding quote around the 0.
Your SQL query should look like:
"SELECT * FROM phcdl_files ORDER BY file_id DESC LIMIT $nr, 1"
remove single quotation of $nr veriable from query
QUERY = "select * from tb_name order by id desc limit $nr , 1"
I'm using the following query to display some information:
$result = mysqli_query ($con,"SELECT * FROM files,members,member_group WHERE files.member_id = members.member_id AND members.member_id = member_group.member_id AND group_id='$id' ORDER BY count DESC ");
My issue is it works fine when I leave out ORDER BY count DESC but when it is there I get the following error:
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /proj/co600/project/repo/public_html/select_field3.php on line 227
Count is a column in my database which records the number of times a publication is downloaded.
count is an aggregate function, so you need to surround it with backticks.
To get a clear cut picture of your error.. You need to change your code like..
$result = mysqli_query ($con,"SELECT * FROM files,members,member_group WHERE files.member_id = members.member_id AND members.member_id = member_group.member_id AND group_id='$id' ORDER BY count DESC ");
if(!$result)
{
die(mysqli_error($con));
}
You are having MySQL reserved keyword as column name in table.
Use Below query:
$result = mysqli_query ($con,"SELECT * FROM files,members,member_group WHERE files.member_id = members.member_id AND members.member_id = member_group.member_id AND group_id='$id' ORDER BY `count` DESC ");
I have a general understanding of what I would like to do but not sure how to write the SQL.
Users have the ability from changing the sort from ASC to DESC and increase the query limit from 5 to 10.
$result=$mysqli->query("SELECT * FROM table WHERE status = 3 ORDER BY name ASC LIMIT $start_from, 5");
the option box for asc/desc will end up being $sort_order
the option box for limit will be $limita
I tried to write it like
$result = "SELECT * FROM wp_pod_tbl_bars WHERE status = 3";
if(!empty($limita)){$result.="LIMIT $limita, 5";}
if ($result = $mysqli->query($result)) {
while($row = $result->fetch_object()){
but the query ended up empty due to the query being split into different lines.
Any idea what I am doing wrong? ill post more code if needed but this is generally where my issue is.
Put space between status and LIMIT.
$result = "SELECT * FROM wp_pod_tbl_bars WHERE status = 3";
if(!empty($limita)){$result.=" LIMIT '".$limita."', 5";}
---------------^
I have this query below:
$msgg = mysql_query("SELECT *
FROM mytable
WHERE time>$time
AND id='someid'
ORDER BY id ASC
LIMIT $display_num",$myconn);
see this line: AND id='someid' <-- someid ...
OK, the query above returns 2 results as expected...
Now for the problem....
-- I have a variable myVar and it's content is "someid" (without quotes)...same as the string 'someid'
When I do this:
$msgg = mysql_query("SELECT *
FROM mytable
WHERE time>$time
AND id=myVar
ORDER BY id ASC
LIMIT $display_num",$myconn);
See: myVar <-- this variable contans .. someid
The second query returns no results.
Update: When using ... AND id='$myVar' it sees $myVar as empty for some reason.
Put a $ in front of myVar:
$msgg = mysql_query(
"SELECT *
FROM mytable
WHERE time > $time
AND id = '$myVar'
ORDER BY id ASC
LIMIT $display_num", $myconn
);
You forgot the dollar sign and the single quotations:
AND id='$myVar'
Additionally, you may want to consider using heredoc:
$query = <<<MYSQL
SELECT *
FROM mytable
WHERE time>$time
AND id='$myVar'
ORDER BY id ASC
LIMIT $display_num
MYSQL;
$msgg = mysql_query($query, $myconn);