PHP/MySQL - Error when using SELECT but no matching data - php

I want to get the ID of an image that is boostAmount-boostStart>total. Currently if an image exists that is appropriate it works. However, if there is nothing appropriate to show I get this error.
Invalid query: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY RAND() LIMIT 1' at line 1
$photoToGuess = mysql_query("SELECT photoID,id,total,boostStart,boostAmount,auth,photoUploadDate FROM photos WHERE (boostAmount !=0) AND ((boostAmount-boostStart)>total) AND (auth=2 OR auth=5 OR auth=7) ORDER BY RAND() LIMIT 1") or die("Invalid query: " . mysql_error());
$getphotoToGuess = mysql_fetch_array($photoToGuess);
//Yes
if(mysql_num_rows($photoToGuess) > 0)
{
//do something
}

Try this, I removed die condition you can achieve die condition using if statement...It is working fine chech once.
$photoToGuess = mysql_query("SELECT photoID,id,total,boostStart,boostAmount,auth,photoUploadDate FROM photos WHERE (boostAmount !=0) AND ((boostAmount-boostStart)>total) AND (auth=2 OR auth=5 OR auth=7) ORDER BY RAND() LIMIT 1");
$getphotoToGuess = mysql_fetch_array($photoToGuess);
//Yes
if(mysql_num_rows($photoToGuess) > 0)
{
//do something
}
You can refer this http://php.net/manual/en/function.mysql-query.php

Your if condition is wrong
why are you using $photoToGuess in your if conditioner code here
if(mysql_num_rows($getphotoToGuess ) > 0){
// do something
}

Related

MySQL "Convert" doesn't seem to be working

I'm creating a database to CSV export function.
I'm using MySQL convert function to convert the date, but it doesn't seem to be working.
The error that I get is :-
string(140) "SELECT userid,firstname,lastname,email,CONVERT(VARCHAR(10),registrationdate, 101) as registrationdate from users order by userid LIMIT 0,30" You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'VARCHAR(10),registrationdate, 101) as registrationdate from users order by user' at line 1
Code
<?php
include '../inc/inc.functions.php';
include '../dbconnector.php';
include '../dbpdo.php';
$query = "SELECT userid,firstname,lastname,email,CONVERT(VARCHAR(10),registrationdate, 101) as registrationdate from users order by userid LIMIT 0,30";
var_dump($query);
$result = mysql_query($query,$db) or die(mysql_error($db));
$array = array();
# Headers
$array[] = array("Serial Number","First Name","Last Name","Email","Registraion Date");
while($row = mysql_fetch_array($result)) {
// $array[] = $row;
$array[] = array($row['userid'],$row['firstname'],$row['lastname'],$row['email'],$row['registrationdate']);
}
array_to_csv_download($array,"records.csv",",");
?>
What might be the problem? Any suggestions would be helpful.
MySQL does not have CONVERT(..., 101), that's SQL Server's way of formatting datetimes.
In MySQL, you could use DATE_FORMAT('%m/%d/%Y', registrationdate);
SELECT userid,firstname,lastname,email,
DATE_FORMAT('%m/%d/%Y', registrationdate) as registrationdate
from users
order by userid
LIMIT 0,30

Mysql select where and order by

i got a point system that are like people can upgrade to [PRO1] user. everyones rights(pro1,pro2,user) are stored in my mysql users table. But i want to make a little feed, that shows the latest one that upgraded to [PRO1]. the upgrade code:
$insert = "UPDATE `users` SET `points` = (`points`-50) WHERE `username` = '".$username."' and points > 50";
mysql_query($insert);
if (mysql_affected_rows() > 0)
{
// other codes
$insert = "UPDATE users SET rights=' [PRO1]' WHERE `username` = '".$username."'";
mysql_query($insert);
header('location: succesupgrade.php');
}else{
echo "You don't have enough points";
}
?>
the upgrade code works fine(just incase i need to add a time/date. and tha code for where i want the"'username' wast the last to upgrade to [PRO1]" is in this code:
<?php
require("dbc.php");
$query = mysql_query("select * from users WHERE rights='[PRO1]' order by right DESC limit 1") or die(mysql_error());
while($array = mysql_fetch_array($query)) {
echo "{$array['username']}<br>";
}
?>was the last to upgrade to:
<?php
require("dbc.php");
$query = mysql_query("select * from users WHERE rights='[PRO1]' order by rights DESC limit 1") or die(mysql_error());
while($array = mysql_fetch_array($query)) {
echo "{$array['rights']}<br>";
}
?>
But that code gives me this error:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DESC limit 1' at line 1
order by right must be order by rights in the first query of the second code block.
That query is going to do nothing to tell you who the last user to upgrade to rights='[PRO1]'. That is just a string field. You would need some sort of datetime/timestamp field that is updated when the users rights change, by which you can make the sort.
You also don't need to do 2 queries. You have two queries doing the exact same thing.
Just do:
SELECT username FROM users WHERE rights='[PRO1]' ORDER BY update_timestamp DESC LIMIT 1
Where update_timestamp would be the field that is updated when the rights change.
The reason is because right is a used keyword, you need a back stroke to solve this :;
Like :
select * from `users` WHERE rights='[PRO1]' order by `rights` DESC limit 1

PHP SQL Pagination LIMIT clause

I am new to the whole php, sql, jquery. I am trying to paginate a sql gallery. I keep getting an error when using the sql LIMIT clause.
It either spits all the images out on one page but gives non functioning page number buttons OR only shows the selected number of items but doesnt create extra pages for the rest of the queries results.
I have searched for a solution and found that there was a problem with using variables after the LIMIT function. Can anyone either show me a better way to implement this or give me a solution to my problem.
$items = 8;
if(isset($_GET['page']) and is_numeric($_GET['page']) and $page = $_GET['page'])
$limit = " LIMIT ".(($page-1)*$items).",$items";
else
$limit = " LIMIT $items";
if(empty($clothing) && empty($price) && empty($uploaded)) {
$query = "SELECT path, description, filename FROM category $limit";
$result = mysql_query($query) or die(mysql_error());
I have also tried this below which gives an error.
$per_page = 8;
if(isset($_GET['page']))
{
$page=$_GET['page'];
}
$start = ($page-1)*$per_page;
if(empty($clothing) && empty($price) && empty($uploaded)) {
$query = "SELECT path, description, filename FROM category LIMIT $start, $per_page ";
$result = mysql_query($query) or die(mysql_error());
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-8, 8' at line 1.
To shed more light the 'if' is to filter the queries results. This is just the first of many sql queries.
Thanks
Offset value for LIMIT cannot be negative

how can i check if a variable is saved or not in the db?

I have this code:
$local_id = $_GET['id'];
$sql = dbquery("SELECT * FROM `videos` WHERE `id` = ".$local_id." LIMIT 0, 1");
while($row = mysql_fetch_array($sql)){
$video_id = $row["youtube_id"];
// the rest
}
how can i check if $local_id does not exist in the db and display an error?
mysql_num_rows
if(mysql_num_rows($sql) == 0) {
//Show error
}
$sql = dbquery("select count(*) from videos where id = ".$local_id." LIMIT 0, 1");
$row = mysql_fetch_row($sql);
if($row[0] == 0)
echo 'error';
You can use the following query:
"SELECT COUNT(*) FROM `videos` WHERE `id` = ".mysql_real_escape_string($local_id)
This query will return one number: how many records have matched your query. If this is zero, you surely know that there are no records with this ID.
This is more optimal than other solutions posted in case you only want to check for the existence of the ID, and don't need the data (if you use SELECT * ..., all the data will be unnecessarily sent from MySQL to you). Otherwise mysql_num_rows() is the best choice, as #Ryan Doherty correctly posted.
Be sure to ALWAYS escape data that came from the outside (this time GET) before you put it into a query (mysql_real_escape_string() for MySQL).
If you fail to do so, you are a possible victim for SQL Injection.
You could have a $count variable and increment it in the while loop. After the loop, check the count, if it is 0, then echo an error message.

Get number of rows from a select count MySQL statement

$How_Many_Manufacturers = "SELECT COUNT(manufacturer), manufacturer
FROM products
WHERE name LIKE '%$new_title%'
GROUP BY manufacturer";
$result2 = mysql_query($How_Many_Manufacturers, $connection) or die(mysql_error());
$num_rows = mysql_num_rows($result2);
if ($num_rows == 0)
{
echo "<div id=\"noMatches\">No Matches</div>";
}
else {
}
The if statement will not work.
How can I correct this script?
#Arjan You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-25,25' at line 4 -RPM
make sure you escape $new_title in the query.
$How_Many_Manufacturers = "SELECT COUNT(manufacturer), manufacturer
FROM products
WHERE name LIKE '%".mysql_real_escape_string($new_title)."%'
GROUP BY manufacturer";
SELECT COUNT will always return a row (even if the count is zero). Simply remove the COUNT, or fetch the row to see the count.

Categories