User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 426,442 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,196 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 5417 | Replies: 4
Reply
Join Date: May 2005
Posts: 26
Reputation: Yuki H. is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Yuki H.'s Avatar
Yuki H. Yuki H. is offline Offline
Light Poster

Mysql num rows(): Invalid argument

  #1  
Jul 4th, 2005
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/user/public_html/im2db/image.php on line 14

This is the error I am getting

Here is the code:
Image.php
Am I supposed to add a exit(); after
$result = mysql_query ($sql, $conn);
line?

[PHP]<?php
// database connection
$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"];

$result = mysql_query ($sql, $conn);

if (mysql_num_rows ($result)>0) {
$row = @mysql_fetch_array ($result) or die (mysql_error());
$image_type = $row["image_type"];
$image = $row["image"];
Header ("Content-type: $image_type");
print $image;
}
?>[/PHP]

Warning: fread(): supplied argument is not a valid stream resource in /home/user/public_html/im2db/index.php on line 18
This is the error I get when I submit a blank entry.

[PHP]<?php
// index.php - by Hermawan Haryanto <hermawan@dmonster.com>
// Example PHP Script, demonstrating Storing Image in Database
// Detailed Information can be found at http://www.codewalkers.com

// database connection
$conn = mysql_connect("localhost", "user", "pass") OR DIE (mysql_error());
@mysql_select_db ("user_images", $conn) OR DIE (mysql_error());

// Do this process if user has browse the file and click the submit button
if ($_FILES) {
$image_types = Array ("image/bmp",
"image/jpeg",
"image/pjpeg",
"image/gif",
"image/x-png");

$userfile = addslashes (fread (fopen ($_FILES["userfile"]["tmp_name"], "r"), filesize ($_FILES["userfile"]["tmp_name"])));
$file_name = $_FILES["userfile"]["name"];
$file_size = $_FILES["userfile"]["size"];
$file_type = $_FILES["userfile"]["type"];

if (in_array (strtolower ($file_type), $image_types)) {
$sql = "INSERT INTO images (image_type, image, image_size, image_name, image_date) ";
$sql.= "VALUES (";
$sql.= "'{$file_type}', '{$userfile}', '{$file_size}', '{$file_name}', NOW())";
@mysql_query ($sql, $conn);
Header("Location:".$_SERVER["PHP_SELF"]);
exit();
}
}

// Do this process of user has click a file name to view or remove
if ($_GET) {
$iid = $_GET["iid"];
$act = $_GET["act"];
switch ($act) {
case rem:
$sql = "DELETE FROM images WHERE image_id=$iid";
@mysql_query ($sql, $conn);
Header("Location:./index.php");
exit();
break;
default:
print "<img src=\"image.php?iid=$iid\">";
break;
}
}

?>
<html>
<head>
<title>Storing Images in DB</title>
</head>
<body>
<form method="post" enctype="multipart/form-data">
Select Image File: <input type="file" name="userfile" size="40"><input type="submit" value="submit">
</form>
<?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 .= "<a href=\"index.php?iid=".$row["image_id"]."\">".$row["image_name"]."</a> ";
$str .= "[".$row["image_date"]."] ";
$str .= "[".$row["image_size"]."] ";
$str .= "[<a href=\"index.php?act=rem&iid=".$row["image_id"]."\">Remove</a>]<br>";
}
print $str;
}
?>
</body>
</html>
[/PHP]
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2005
Location: Kansas City, Missouri, USA
Posts: 344
Reputation: Troy is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 4
Troy's Avatar
Troy Troy is offline Offline
Posting Whiz

Re: Mysql num rows(): Invalid argument

  #2  
Jul 4th, 2005
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 Wolf is the author of SnippetEdit. "Website editing as easy as it gets." IX Web Hosting
Reply With Quote  
Join Date: May 2005
Posts: 26
Reputation: Yuki H. is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Yuki H.'s Avatar
Yuki H. Yuki H. is offline Offline
Light Poster

Re: Mysql num rows(): Invalid argument

  #3  
Jul 5th, 2005
I do not get "Try running it directly against your database using phpMyAdmin or the command line client."
When I add
if ($_FILES['userfile']['error'] == 0) {
I get:
Warning: fread(): supplied argument is not a valid stream resource in /home/user/public_html/im2db/index.php on line 19
as an error but now it is visible once I go on the index page.
Plus the image does not show up, only as a red x
[PHP]<?php
// database connection
$conn = mysql_connect("localhost", "user", "pass") OR DIE (mysql_error());
@mysql_select_db ("user_images", $conn) OR DIE (mysql_error());

// Do this process if user has browse the file and click the submit button
if ($_FILES['userfile']['error'] == 0) {

$image_types = Array ("image/bmp",
"image/jpeg",
"image/pjpeg",
"image/gif",
"image/x-png");

$userfile = addslashes (fread (fopen ($_FILES["userfile"]["tmp_name"], "r"),

filesize ($_FILES["userfile"]["tmp_name"])));
$file_name = $_FILES["userfile"]["name"];
$file_size = $_FILES["userfile"]["size"];
$file_type = $_FILES["userfile"]["type"];

if (in_array (strtolower ($file_type), $image_types)) {
$sql = "INSERT INTO images (image_type, image, image_size, image_name, image_date)

";
$sql.= "VALUES (";
$sql.= "'{$file_type}', '{$userfile}', '{$file_size}', '{$file_name}', NOW())";
@mysql_query ($sql, $conn);
Header("Location:".$_SERVER["PHP_SELF"]);
exit();
}
}

// Do this process of user has click a file name to view or remove
if ($_GET) {
$iid = $_GET["iid"];
$act = $_GET["act"];
switch ($act) {
case rem:
$sql = "DELETE FROM images WHERE image_id=$iid";
@mysql_query ($sql, $conn);
Header("Location:./index.php");
exit();
break;
default:
print "<img src=\"image.php?iid=$iid\">";
break;
}
}

?>
<html>
<head>
<title>Storing Images in DB</title>
</head>
<body>
<form method="post" enctype="multipart/form-data">
Select Image File: <input type="file" name="userfile" size="40"><input type="submit"

value="submit">
</form>
<?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 .= "<a href=\"index.php?iid=".$row["image_id"]."\">".$row

["image_name"]."</a> ";
$str .= "[".$row["image_date"]."] ";
$str .= "[".$row["image_size"]."] ";
$str .= "[<a href=\"index.php?act=rem&iid=".$row["image_id"]."\">Remove</a>]

<br>";
}
print $str;
}
?>
</body>
</html>
[/PHP]

with images.php as a result: SELECT * FROM images WHERE image_id=
when I go to the page.

[PHP]<?php

// database connection
$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"];

$result = mysql_query ($sql, $conn);
echo $sql;
exit();
if (mysql_num_rows ($result)>0)

{
$row = @mysql_fetch_array ($result) or die (mysql_error());
$image_type = $row["image_type"];
$image = $row["image"];
Header ("Content-type: $image_type");
print $image;

}
?>
[/PHP]

Thank you a lot for helping me a lot though =O.
Reply With Quote  
Join Date: Jun 2005
Location: Kansas City, Missouri, USA
Posts: 344
Reputation: Troy is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 4
Troy's Avatar
Troy Troy is offline Offline
Posting Whiz

Re: Mysql num rows(): Invalid argument

  #4  
Jul 5th, 2005
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 "<br />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 Wolf is the author of SnippetEdit. "Website editing as easy as it gets." IX Web Hosting
Reply With Quote  
Join Date: Jul 2005
Posts: 10
Reputation: val542 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
val542 val542 is offline Offline
Newbie Poster

Re: Mysql num rows(): Invalid argument

  #5  
Jul 5th, 2005
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!!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 2:52 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC