954,587 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Mysql num rows(): Invalid argument



Select Image File:
<?php
$sql = "SELECT * FROM images ORDER BY image_date DESC";
$result = mysql_query ($sql, $conn);
if (mysql_num_rows($result)>0) {
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$i++;
$str .= $i.". ";
$str .= "".$row["image_name"]." ";
$str .= "[".$row["image_date"]."] ";
$str .= "[".$row["image_size"]."] ";
$str .= "[Remove]
";
}
print $str;
}
?>

Yuki H.
Light Poster
26 posts since May 2005
Reputation Points: 10
Solved Threads: 0
 

For your mysql error, test your SQL statement. Right after your line that builds your SQL statement, do this:
[PHP]
echo $sql;
exit();
[/PHP]
Examine the output to see if something is wrong with the SQL statement. Try running it directly against your database using phpMyAdmin or the command line client.

For your fopen error, as you know, it happens when you submit a blank form. Yet, the code says "Oh, a form was submitted, let's open the file the user uploaded." But you didn't select a file--you submitted a blank form. SO...you need to add some code to test the validity of the form. Change your "if ($_FILES) {" line with this:
[PHP]
if ($_FILES['userfile']['error'] == 0) {
[/PHP]

Troy
Posting Whiz
362 posts since Jun 2005
Reputation Points: 36
Solved Threads: 6
 



Select Image File:
<?php
$sql = "SELECT * FROM images ORDER BY image_date DESC";
$result = mysql_query ($sql, $conn);
if (mysql_num_rows($result)>0) {
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$i++;
$str .= $i.". ";
$str .= "".$row

["image_name"]."
";
$str .= "[".$row["image_date"]."] ";
$str .= "[".$row["image_size"]."] ";
$str .= "[Remove]


";
}
print $str;
}
?>

Yuki H.
Light Poster
26 posts since May 2005
Reputation Points: 10
Solved Threads: 0
 

Troubleshooting 101
When troubleshooting code problems, the secret is baby-steps. When you have an error, it is always a single statement (or line) that is producing the error. Create a new file, and put in only enough code to produce your problem, then work until you solve it--one line at a time. Baby-steps.

In keeping with the baby-steps philosophy, let's attack only one problem at a time. Let's look at your SQL problem. Create a new file with only this code:
[PHP]
$conn = mysql_connect("localhost", "user", "pass") OR DIE (mysql_error());
@mysql_select_db ("user_images", $conn) OR DIE (mysql_error());

$sql = "SELECT * FROM images WHERE image_id=".$_GET["iid"];
echo $sql;

if (!$result = mysql_query ($sql, $conn)) {
echo mysql_error();
} else {
echo "
query successful";
}
[/PHP]
Now, call this script directly within your browser using a URL such as http://myserver.com/myscript.php?iid=1

The script should display in your browser the SQL statement that it runs against the database. Examine that statement--do you see anything wrong with it? If not, then copy the statement from your browser window and run it directly against your database. What I mean by that is if you are working with a database, you must have some tool you use to create your tables--right? For many people with MySql, that tool is phpMyAdmin. Do you have access to use phpMyAdmin? If not, whatever tool you use to manage you database, should have a way to paste in a query and execute it. This way you can test that the SQL statement you are generating in your PHP code is actually a valid statement that your database can execute. Maybe it is a valid statement, but it does not return any rows because no rows match the query.

Once you work through this part, you can add more steps---one at a time--baby steps--until you have the whole script working.

Troy
Posting Whiz
362 posts since Jun 2005
Reputation Points: 36
Solved Threads: 6
 

always make a test condition before using
mysql_num_rows()
because if the table is empty so the process is stopped

$array[]=@mysql_num_rows(elements functions);

if (count($array)==0) //ur table is empty

that's all!!

val542
Newbie Poster
10 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You