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

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.

Related

How do I store the results of an SQL call in a php array

I have been trying to create a sign up form for a website and I keep struggling with trying to check if a user's username had been used by someone else who signed up for the website in the past. I am using a Mysql database to store information and am trying to access past usernames through it using php. This specific part of my code keeps returning an error.
$query = "SELECT username FROM users";
$records = mysql_query("SELECT username FROM users");
$result = mysql_query($records);
$result_array = array();
while($row = mysql_fetch_assoc($result))
{
$result_array[] = $row['username'];
}
The error messages I am receiving are:
Warning: mysql_query() expects parameter 1 to be string, resource given in ...
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, null given in...
thank you so much!
The problem is you are doing mysql_query twice one on the $records variable and then on resultlike so:
$records = mysql_query("SELECT username FROM users");
$result = mysql_query($records);
What you need to do is this:
$records = "SELECT username FROM users";
$result = mysql_query($records);
I hope the answer by Jack Smith was helpful. I'll just add that try to use mysqli_query as mysql is deprecated and may be removed in a newer version of php. You would also need to use mysqli_connect('host','usrname','password','database') instead of mysql_connect.

MySQLi Query will not execute

I am playing around with this trying to pull some articles from a database.
Here is the code i have so far: ($host etc are defined in config)
include ("admin/config.php");
$mysqli = mysqli_connect($host, $user , $pass , $database);
$query = "SELECT * FROM articles ORDER BY date LIMIT 5";
$result = mysqli_query($mysqli,$query);
while($row = mysqli_fetch_array($result))
{
Some code to display results
}
This is the error that gets generated but i just cant see what i am doing wrong. Any help will be much appreciated.
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /website....
there is error in your query.
to get it always run all your queries this way
$result = mysqli_query($mysqli,$query) or trigger_error($mysqli->error."[$query]");

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

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'}==""){
}

mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result
I' really stuck on this , I'm gettiing this error:
mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in "filename"
Here is the code:
$sql = "SELECT * FROM $tbl_name WHERE....
$result=mysql_query($sql);
$row = mysql_fetch_assoc($result);
The wierd thing is that I've used the exact same code before and it worked fine
Any ideas??
That means the query failed. Usually it's a SQL syntax error. To find out, just insert this right before the _fetch_assoc line:
print mysql_error();
To prevent the error message, structure your code like this to check the $result beforehand:
$sql = "SELECT * FROM $tbl_name WHERE....";
if ($result = mysql_query($sql)) {
$row = mysql_fetch_assoc($result);
}
else print mysql_error();
Always run all your queries this way
$sql = "SELECT * FROM $tbl_name WHERE....";
$res = mysql_query($sql) or trigger_error(mysql_error()." in ".$sql);
$row = mysql_fetch_assoc($result);
And you will be notified of the cause of error.
But never print or let die() output any errors, as it's security flaw.
This error usually occurs because no data is returned from the query. Make sure the data is being returned by going into something like PHPMyAdmin and making sure the query returns some rows. You should also add the
or die(mysql_error());
At the end of your query.

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.

Categories