Warning mysql_num_rows(): supplied argument is not a valid MySQL result - php

Why i have this error and how to fix this, I've double checked everything and all is okay
Warning: mysql_num_rows(): supplied argument is not a valid
MySQL result resource in
/home/sharinga/public_html/ccccc.com/app/like/like.php on
line 15 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 'like WHERE postID='81' AND
userIP='2x2.2x0.x5.xxx'' at line 1
Here is sql
$ip_sql = mysql_query("SELECT userIP FROM like WHERE postID='$id' AND userIP='$ip'");
$count = mysql_num_rows($ip_sql) or die(mysql_error());
if($count==0)
{...

LIKE is a reserved word - escape it
$ip_sql = mysql_query("SELECT userIP FROM `like` WHERE postID='$id' AND userIP='$ip'");

LIKE is a keyword in SQL, use ยด
SELECT userIP FROM `like` WHERE postID='$id' AND userIP='$ip

Try connecting first. Are you looking for the null case? If so you have to search a certain row not $count as a whole.
$conn = mysql_connect("localhost", "user", "pass");
$ip_sql = mysql_query("SELECT userIP FROM like WHERE postID='$id' AND userIP='$ip'",$conn);
$count = mysql_num_rows($ip_sql) or die(mysql_error());
if ($count['postID'}==""){
}

Related

What is Wrong with this SQL Syntax that checks if a table exists?

I'm querying a server and iterating through multiple databases using PHP, but for some reason this $sql2 query (which I have read works in countless threads) is returning a syntax error:
$res = mysqli_query($conn,"SHOW DATABASES");
if (!$res){
// Deal with error
}
while ($d = mysqli_fetch_array($res)){
$db = $d['Database'];
$sql1 = "USE $db";
$query1 = mysqli_query($conn, $sql1);
if (!$query1){
// Deal with error
}
$sql2 = "IF (EXISTS (SELECT *
FROM INFORMATION_SCHEMA.TABLE
WHERE TABLE_SCHEMA = '$db'
AND TABLE_NAME = 'appusers'))
BEGIN
SELECT * FROM `appusers`
END";
$query2 = mysqli_query($conn, $sql2);
if (!$query2){
// Deal with error
}
}
This is the error I receive:
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 'IF (EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLE WHERE TABLE_S' at line 1
My MySQL Server version is 5.6.27 and my PHP interpreter is 5.6
You can't use an IF statement as a query, only in a stored procedure. You'll need to perform two separate queries.
$sql = "SELECT COUNT(*) AS count
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = '$db'
AND TABLE_NAME = 'appusers'";
$result = mysqli_query($conn, $sql) or die(mysqli_error($conn);
$row = mysqli_fetch_assoc($result);
if ($row['count'] != 0) {
$sql2 = "SELECT * FROM appusers";
$query2 = mysqli_query($conn, $sql2);
...
} else {
// deal with error
}
There is no if in SQL (although mysql has a function called if)
Apparently, you want to run a query if the table exists. The more common way would be to try running the query and check whether the error you get says that the table doesn't exist.
E.g. if you run the query in the mysql command line application, you might get this:
mysql> SELECT * FROM appusers;
ERROR 1146 (42S02): Table 'test.appusers' doesn't exist
You see two codes:
1146: internal mysql code (obtain this via mysqli_errno)
42S02: the SQL standard error state (obtain this via mysqli_sqlstate)
In your case, checking the SQL state might be better because it covers more cases. E.g., all of these mysql error codes map to SQL state 42S02:
1051 - Unknown table '%s'
1109 - Unknown table '%s' in %s
1146 - Table '%s.%s' doesn't exist

SELECT * FROM issue

I have been searching around for about 30 minutes, and I just can't find the issue here...
if($middle == 'garage'){
$result = mysql_query("SELECT * FROM character WHERE username = '$username'") or die(mysql_error());
$row = mysql_fetch_array($result);
$return['middle'] = $row['username'];
}
For some reason this is returning
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 'character WHERE username = 'Alcapwn'' at line 1
I have tried so many things, other SELECT * FROM query's are working fine, it's just this one. The table and row containing it is there. If I switch
$result = mysql_query("SELECT * FROM character WHERE username = '$username'") or die(mysql_error());
to
$result = mysql_query("SELECT * FROM users WHERE username = '$username'") or die(mysql_error());
the second one will work, but not the first.
character is a reserved mysql word, so you have to enclose it in backticks
FROM `character` ...
It seems that character is a MySQL Reserved Keyword, which means you cannot use it as table name.
"Character" is a reserved word in MySQL.
http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html

Warning: mysql_num_rows(): supplied argument is not a valid MySQL

I am using the following code
$rows = mysql_num_rows($query);
And I am getting this error:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/....../public_html/test/basic.rar Folder/global.php on line 355
I'm assuming that $query is your actual query (such as a select string).
You don't pass the query to mysql_num_rows, you pass the result set that you get back from executing the query, something like:
$dbconn = mysql_connect ("pax_db_box", "pax", "never_you_mind");
mysql_select_db ("main_db", $dbconn);
$query = "SELECT balance FROM accounts where account_id = '42'";
$result_set = $mysql_query ($query, $dbconn);
$numrows = mysql_num_rows ($result_set);
You may also want to look into using the newer mysqli functions at some point.

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in

Hi can someone explain why I'm receiving this error?
Warning: mysql_result(): supplied argument is not a valid MySQL result resource in
if (mysql_result(mysql_query("SELECT count(*) FROM load_test WHERE batch_id=UCASE('".$batchid."')
AND word='".$data[2]."',
type='".$data[3]."',
language = '".$data[4]."',
rgender = '".$data[5]."'
"), 0) == 0) {
Hey! You're missing AND between conditions! Don't use commas!
try this:
$query = "SELECT count(*) FROM load_test
WHERE batch_id=UCASE('".$batchid."')
AND word='".$data[2]."'
AND type='".$data[3]."'
AND language = '".$data[4]."'
AND rgender = '".$data[5]."'";
$result = mysql_query($query) or die(mysql_error());
In this way you'll can catch the mysql error you are getting when you execute the query.
Solved the problem was the commas i had at the end.

MYSQL Query Error

I'm trying to use this query
$page_set = mysql_query("SELECT * FROM pages WHERE subject_id =
{$subject["id"]}", $connection);
but i keep getting this error when loading my page .
Database query failed: 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 '' at line 1
Try it without the complex syntax:
$query = 'SELECT * FROM pages WHERE subject_id = ' . $subject['id'];
$page_set = mysql_query($query, $connection);
Incidentally, I loathe variable parsing in strings, and prefer concatenation.
you're experiencing a quote mismatch. try replacing the double quotes around your array key with single quotes.
$page_set = mysql_query("SELECT * FROM pages WHERE subject_id =
{$subject['id']}", $connection);
$sql = "SELECT * FROM pages WHERE subject_id = '".$subject["id"]."'";
$page_set = mysql_query($sql, $connection);
Make sure you escape the subject_id also.
use single quote

Categories