473,385 Members | 1,337 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Nested mySQL queries create issue with validity of result index?

Hi all,
I am iterating through a result set to generate a second set of queries but
no matter what I do I get the error

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

even though if I echo the query to the browser and cut and paste it into the
command line I get valid results.

Do I have to store the first result in an array before doing the second set?

TIA,
jg

while ($i < mysql_num_rows($result)) {
$PIDs[$id]['ClientName'] = $client;
$PIDs[$id]['ProjectName'] = mysql_result($result, $i, 'Name');
$PIDs[$id]['PID'] = mysql_result($result, $i, 'PID');
$thisPID = $PIDs[$id]['PID'];
$PIDresult = ("SELECT * from hours WHERE PID = '$thisPID'") or
die(mysql_error());
$j=0;
while ($j < mysql_num_rows($PIDresult)) {
$PIDs[$id]['PID']['Comments'] = mysql_result($PIDresult, $j,
'Comments');
$j++;
}
$i++;
}
}
Jul 16 '05 #1
6 3653
jerrygarciuh <de*****@no.spam.nolaflash.com> wrote:
Hi all,
I am iterating through a result set to generate a second set of queries
but no matter what I do I get the error

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result
resource
Hi Jerry,

You should call mysql_num_rows() only once.

while ($i < mysql_num_rows($result)) {
$NumRows_1 = mysql_num_rows($result);
while ($i < $NumRows_1) {
$PIDs[$id]['ClientName'] = $client;
$PIDs[$id]['ProjectName'] = mysql_result($result, $i, 'Name');
$PIDs[$id]['PID'] = mysql_result($result, $i, 'PID');
$thisPID = $PIDs[$id]['PID'];
$PIDresult = ("SELECT * from hours WHERE PID = '$thisPID'") or
die(mysql_error());
$j=0;
while ($j < mysql_num_rows($PIDresult)) {


$NumRows_2 = mysql_num_rows($PIDresult)
while ($j < $NumRows_2) {

[snip]

HTH;
JOn
Jul 16 '05 #2
Jon,

Thanks for the reply. Calling mysql_num_rows in the way you suggest still
produces the error

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

My adjusted code is below. I am thinking the Good way must be to store what
I need in a throwaway array and not nest the queries.
Thanks for any advice!

jg

foreach ($clients as $id => $client) {
$i = 0;
$result = mysql_query("SELECT PID, Name FROM projects WHERE ID = '$id'");
$NumRows_1 = mysql_num_rows($result);
while ($i < $NumRows_1) {
$PIDs[$id]['ClientName'] = $client;
$PIDs[$id]['ProjectName'] = mysql_result($result, $i, 'Name');
$PIDs[$id]['PID'] = mysql_result($result, $i, 'PID');
$thisPID = $PIDs[$id]['PID'];
$PIDresult = ("SELECT * from hours WHERE PID = '$thisPID'") or
die(mysql_error());
$j=0;
$NumRows_2 = mysql_num_rows($PIDresult);
while ($j < $NumRows_2) {
$PIDs[$id]['PID']['Comments'] = mysql_result($PIDresult, $j,
'Comments');
//make an array of Session info and add to $PIDs
$j++;
}
$i++;
}
}

"Jon Kraft" <jo*@jonux.co.uk> wrote in message
news:bj************@ID-175424.news.uni-berlin.de...
jerrygarciuh <de*****@no.spam.nolaflash.com> wrote:
Hi all,
I am iterating through a result set to generate a second set of queries
but no matter what I do I get the error

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


Hi Jerry,

You should call mysql_num_rows() only once.

while ($i < mysql_num_rows($result)) {


$NumRows_1 = mysql_num_rows($result);
while ($i < $NumRows_1) {
$PIDs[$id]['ClientName'] = $client;
$PIDs[$id]['ProjectName'] = mysql_result($result, $i, 'Name');
$PIDs[$id]['PID'] = mysql_result($result, $i, 'PID');
$thisPID = $PIDs[$id]['PID'];
$PIDresult = ("SELECT * from hours WHERE PID = '$thisPID'") or
die(mysql_error());
$j=0;
while ($j < mysql_num_rows($PIDresult)) {


$NumRows_2 = mysql_num_rows($PIDresult)
while ($j < $NumRows_2) {

[snip]

HTH;
JOn

Jul 16 '05 #3
jerrygarciuh <de*****@no.spam.nolaflash.com> wrote:
Jon,

Thanks for the reply. Calling mysql_num_rows in the way you suggest still
produces the error

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

My adjusted code is below. I am thinking the Good way must be to store
what I need in a throwaway array and not nest the queries.

foreach ($clients as $id => $client) {
$i = 0;
$result = mysql_query("SELECT PID, Name FROM projects WHERE ID =
'$id'"); $NumRows_1 = mysql_num_rows($result);


Then the error message means your query fails and there is no valid result
set. Try this to check on the SQL error:

$sql = "SELECT PID, Name FROM projects WHERE ID = '$id'";
$result = mysql_query($sql) or die (mysql_error()."".$sql);

HTH;
JOn
Jul 16 '05 #4
Jon,

Well a very odd thing happened. I had been echoing the query and running it
from the command line and getting results but I went ahead and changed to
your format of setting up the query string as a scalar and inserting it as
such and lo and behold the error disappeared.

Thanks!

jg
"Jon Kraft" <jo*@jonux.co.uk> wrote in message
news:bj************@ID-175424.news.uni-berlin.de...
jerrygarciuh <de*****@no.spam.nolaflash.com> wrote:
Jon,

Thanks for the reply. Calling mysql_num_rows in the way you suggest still produces the error

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

My adjusted code is below. I am thinking the Good way must be to store
what I need in a throwaway array and not nest the queries.

foreach ($clients as $id => $client) {
$i = 0;
$result = mysql_query("SELECT PID, Name FROM projects WHERE ID =
'$id'"); $NumRows_1 = mysql_num_rows($result);


Then the error message means your query fails and there is no valid result
set. Try this to check on the SQL error:

$sql = "SELECT PID, Name FROM projects WHERE ID = '$id'";
$result = mysql_query($sql) or die (mysql_error()."".$sql);

HTH;
JOn

Jul 16 '05 #5
On Sun, 7 Sep 2003 20:50:58 -0500, "jerrygarciuh"
<de*****@no.spam.nolaflash.com> wrote:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result
resource $PIDresult = ("SELECT * from hours WHERE PID = '$thisPID'") or
die(mysql_error());


You're missing a mysql_query() around the SQL string.

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 16 '05 #6
OMG, laughing very hard at myself just now.

Thanks!

jg

"Andy Hassall" <an**@andyh.co.uk> wrote in message
news:du********************************@4ax.com...
On Sun, 7 Sep 2003 20:50:58 -0500, "jerrygarciuh"
<de*****@no.spam.nolaflash.com> wrote:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result
resource

$PIDresult = ("SELECT * from hours WHERE PID = '$thisPID'") or
die(mysql_error());


You're missing a mysql_query() around the SQL string.

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)

Jul 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: StinkFinger | last post by:
All, I am in the process of tweaking and tuning my PHP scripts, and am starting to look in tweaking my indexes in my MySQL tables. I have found many, many articles on the topic and have applied...
0
by: Lenz Grimmer | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, MySQL 4.0.14, a new version of the popular Open Source/Free Software Database, has been released. It is now available in source and binary...
0
by: Mike Chirico | last post by:
Interesting Things to Know about MySQL Mike Chirico (mchirico@users.sourceforge.net) Copyright (GPU Free Documentation License) 2004 Last Updated: Mon Jun 7 10:37:28 EDT 2004 The latest...
6
by: Matt Liverance | last post by:
I REALLY dont want to switch to oracle :( but I cant get these tables working any faster. I've got 2 dedicated servers, each with a slave, all run 32gig 15k rpm raid 5 on u320 perc raid...
39
by: Mairhtin O'Feannag | last post by:
Hello, I have a client (customer) who asked the question : "Why would I buy and use UDB, when MySql is free?" I had to say I was stunned. I have no experience with MySql, so I was left sort...
1
by: Good Man | last post by:
Hi there I've noticed some very weird things happening with my current MySQL setup on my XP Laptop, a development machine. For a while, I have been trying to get the MySQL cache to work....
1
by: PowerLifter1450 | last post by:
I've been having a very rough time installinig mySQL on Linux. I have been following the instructions form here: http://www.hostlibrary.com/installing_apache_mysql_php_on_linux Everytime I get to...
6
Atli
by: Atli | last post by:
This is an easy to digest 12 step guide on basics of using MySQL. It's a great refresher for those who need it and it work's great for first time MySQL users. Anyone should be able to get...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.