Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mysql_num_rows() error 2

Status
Not open for further replies.

tty0

Programmer
Nov 13, 2002
108
GB
ok chaps and chapasses, heres the error:

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

and after the asterix is the code that is generating the error.

what I am doing is removing records from a table then selecting data from another table to see if that user has any more records with different names in the database. Now I know that SELECT is the only query that will return a result set, which is why its looking for affected rows.

I have a close after this for the simple reason I thought it might work if I close then re-open the connection as I have been getting the error without the close and re-open.

I have looked all over for the past five yours and its too late to keep lookin so I thought I would throw it open o the floor to see what you all thought so here goes:
********************************************************

<?
@$db = mysql_connect(&quot;servername&quot;, &quot;username&quot;, &quot;password&quot; );
if (!$db)
{
/* redirect back to the admin panel page */
header(&quot;Location: ../adminpanel.php&quot;);
exit;
}
mysql_select_db(&quot;databasename&quot;);
list($name,$pet) = split (&quot;/&quot;, $_POST['DELETE_ITEM_NAME']);
?>
<html>
<head>
<title>User Deletion</title>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;>
<META http-equiv=&quot;refresh&quot; content=&quot;3;URL=../adminpanel.php&quot;>

</head>

<body>
<?
echo &quot;<h1> Deleting...</h1><BR><BR>&quot;;
$query = &quot;DELETE FROM pets WHERE user='$name' AND name='$pet'&quot;;
$result = mysql_query($query);
echo mysql_affected_rows().&quot; Records deleted, Stage 1 clear...<BR><BR>&quot;;
//now check for them having another animal left in the table
mysql_close();
@$db = mysql_connect(&quot;servername&quot;, &quot;user&quot;, &quot;password&quot; );
if (!$db)
{
/* redirect back to the admin panel page */
header(&quot;Location: ../adminpanel.php&quot;);
exit;
}
mysql_select_db(&quot;databasename&quot;);
$query2 = &quot;SELECT * FROM pets WHERE username='&quot;.$name.&quot;'&quot;;
$result2 = mysql_query($query2);
$num_results2 = mysql_num_rows($result2);
echo $result2.&quot;Checking for other pets under this user...<BR><BR>&quot;;
if ($num_results2 == 0)
**********************************************

the script then goes on to inform the user if there is another record which at the moment it doesnt do as the number of results isnt set. And finally because there are no more results it clears the user out of the security table which then stops them loggin in for the other pets. Which totaly buggers up the system.

Heres hoping

cheers all and any who can help




'mi casa es su casa'
]-=tty0=-[
 
Why are you closing and then reopening your database connection?
Try replacing
[tt]$query2 = &quot;SELECT * FROM pets WHERE username='&quot;.$name.&quot;'&quot;;[/tt]
with
[tt]$query2 = &quot;SELECT * FROM pets WHERE username='&quot;.mysql_escape_string($name).&quot;'&quot;;[/tt]

//Daniel
 
nope just tried that one, still giving me the error.

and the reason for closing and reopening is none other than it was the only thing that i hadnt tried. Theoretically it wouldnt matter a bit whether it was done this way or by just letting the script use the exsisting connection apart from excess overheads on the server.

But I am at a lost end as of what to do so tried it the way I have it above.

PS now reverted back to the old way of using the remaining connection as I cant afford any overheads at the moment.

cheers

'mi casa es su casa'
]-=tty0=-[
 
Add error checking to your code ([tt]or die(mysql_error());[/tt]). It will make debugging so much easier.

//Daniel
 
a da man,

cheers bro, FYI my fieldname was incorrect, but I only started php two days ago so I was struggling debugging as so far all my contracts have been in VB and ASP.

I thought that the error i was getting WAS the only debug error i could get from the script.

cheers once again



'mi casa es su casa'
]-=tty0=-[
 
Uuuugh, Ok, I am getting the same warning. However I am not any smarter off this thread, so what was the problem?

I created my code in school while learning PHP and just enough SQL for the database. All was well till my host switched servers lately and now on the host server I get this: not a valid MySQL result resource

However when I run it on my PC Apache server it works fine, only on the NEW Unix server does this happen, old server was fine too.

So what was it that broke your code? Here is the three lines that are getting the warning:
$rows = mysql_num_rows($resultID);
$setup= mysql_fetch_array($setupID);
$price = mysql_fetch_array($priceID);

And any ideas on why my apache and the old host server accepted it, but the new host server doesn't?

Kyle Lamson
 
The warning is caused when the mysql_query() functions fails. The reason for that failure can be in any step of the MySQL query process.
Error checking at each of the following steps is not only helpful but will save you from experiencing an error much later than the causing failure:

1. Connecting mysql_connect(....) OR die(&quot;Can't connect &quot;.mysql_error());
2. Selecting the database mysql_select_db(...) OR die(&quot;Can't select database &quot;.mysql_error());
3. The query mysql_query(...) OR die(&quot;Query Error: &quot;.mysql_error());

LSWstudio
Why would it not when moving the code among hosts?
Differen database user, different password?
Maybe you assume register_globals is ON but it's off on the UNIX machine. Many causes.

Without more detailed error messages it's almost impossible to find the exact cause.
 
@ DRJ478,
ok, The DB name and password and user have been changed and I get no connection error. Here is my connection call:

$dbh=mysql_connect (&quot;localhost&quot;, &quot;replaced&quot;, &quot;replaced&quot;) or die ('I cannot connect to the database.');
mysql_select_db (&quot;replaced&quot;);

Then:************************

// $task calls up the neccessary information from the DB tables
$task= &quot;SELECT name, service, value FROM packet LEFT JOIN intersect1 ON (packet.pid=intersect1.pid) LEFT JOIN services ON (intersect1.sid=services.sid) LEFT JOIN content ON (intersect1.cid=content.cid) where packet.pid=5&quot;;

$resultID = mysql_query($task); // calls out the query

$rows = mysql_num_rows($resultID); //calls up the number of rows in the DB table


$suf = &quot;SELECT setup FROM packet WHERE packet.pid=5&quot;;

$setupID= mysql_query($suf); // Responsible for calling up and saving the information for the setup fee

$setup= mysql_fetch_array($setupID); // Calls up result of MySQL Query


$mp = &quot;SELECT price FROM packet WHERE packet.pid=5&quot;;

$priceID = mysql_query($mp); // Responsible for calling up and saving the information for the monthly fee

$price= mysql_fetch_array($priceID); // Calls up result of MySQL Query


$price6 = $price[0] * 6;
$priceyr = $price[0] * 12;

$total = $price[0] + $setup[0];

$totalsixmon = $price6 + $setup[0];

$totaloneyr = $priceyr+ $setup[0];


$color[0] = &quot;td1&quot;; // CSS tag 1
$color[1] = &quot;td2&quot;; // CSS tag 2

for($i=0; $i<$rows; $i++) {

$result = mysql_fetch_array($resultID); // Calls up result of MySQL Query


Then my HTML stuff which does show and the closeing mysql_close();

Both old and new servers are Unix, I use PHP MyAdmin to work with the databases.

Don't know if this helps any, Thanks for your reply, I am looking into it, Cheers.
 
@ DRJ478,
Ok, I follow now. I didn't have enough error checks and not detailed enough. Tried those you wrote and recieved only one, there does seem to be a accress problem with the database, which would explain why it works on the PC.

Thanks for the help, just the nudge in the right direction I needed. I just couldn't quite follow what the problem was in the other fellows question. Guess I am having a slow day. Really appreciate Mate. Thanks.
 
bit too late now but just for clarity I had a fieldname incorrect.

This:

$query = &quot;DELETE FROM pets WHERE user='$name' AND name='$pet'&quot;;


Should of read:

$query = &quot;DELETE FROM pets WHERE user='$name' AND petname='$pet'&quot;;

'mi casa es su casa'
]-=tty0=-[
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top