question about how PHP handles mySQL query results

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
hurdy gurdy
Forum Commoner
Posts: 40
Joined: Mon Jun 09, 2003 8:19 pm

question about how PHP handles mySQL query results

Post by hurdy gurdy »

I'm a newb, I admit it. :oops:

but I gotta' know why this doesn't work:

Code: Select all

mysql_connect ("host", "user", "pass");
mysql_select_db ("DB");
	
$qryImage = mysql_query ("SELECT strImageURL FROM TableImages WHERE intArtistID = 3 ORDER BY dateCreated DESC");

$numResults = mysql_num_rows ($qryImage);
echo $numResults; // echo is fine... prints the expected result

// the WHILE statement works fine too

while ($row = mysql_fetch_array($qryImage)) {
  echo '<pre>';
  echo $row['strImageURL'];
  echo '</pre>';
}

//  why can't I get the results of the query to work in the below example?

$row = mysql_fetch_row ($qryImage);
echo $row[0]['strImageURL'];
echo $row[1]['strImageURL'];
echo $row[2]['strImageURL'];

?>
Is there a means of getting the results by using PHP's ability to handle multidimensional arrays?

did this make any sense? lol

Thanks
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You need to output your query into a result using the mysql_query() function...

Code: Select all

<?php
$qryImage = mysql_query ("SELECT strImageURL FROM TableImages WHERE intArtistID = 3 ORDER BY dateCreated DESC"); 

$numResults = mysql_num_rows ($qryImage); 
echo $numResults; // echo is fine... prints the expected result 
//******************
// Added this part
// You may want to add some error checking in here also
//******************
$result = mysql_query($qryImage);

// the WHILE statement works fine too 

//******************
// Modified the next line
//******************

//while ($row = mysql_fetch_array($qryImage)) { 
while ($row = mysql_fetch_array($result)) {
  echo '<pre>'; 
  echo $row['strImageURL']; 
  echo '</pre>'; 
} 

//  why can't I get the results of the query to work in the below example? 

$row = mysql_fetch_row ($qryImage); 
echo $row[0]['strImageURL']; 
echo $row[1]['strImageURL']; 
echo $row[2]['strImageURL']; 

?>
Now you have a query, a query result and an array of result data in the dataset. This should work.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

uh... I'm pretty sure that won't work Everah. This should though:

Code: Select all

<?php

mysql_connect ("host", "user", "pass"); 
mysql_select_db ("DB"); 
    
$qryImage = mysql_query ("SELECT strImageURL FROM TableImages WHERE intArtistID = 3 ORDER BY dateCreated DESC"); 

$numResults = mysql_num_rows ($qryImage); 
echo $numResults; // echo is fine... prints the expected result 

// the WHILE statement works fine too 

while ($row = $data[] = mysql_fetch_assoc($qryImage)) { 
  echo '<pre>'; 
  echo $row['strImageURL']; 
  echo '</pre>'; 
} 

echo $data[0]['strImageURL']; 
echo $data[1]['strImageURL']; 
echo $data[2]['strImageURL']; 

?>
User avatar
brewmiser
Forum Commoner
Posts: 74
Joined: Mon Aug 18, 2003 12:50 pm
Location: Dallas, TEXAS

Post by brewmiser »

Out of curosity, does your table really have lower and uppercase letters for the description of the data? i.e. 'intArtistID'. 8O
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

feyd wrote:uh... I'm pretty sure that won't work Everah.
You are so right. I hadn't seen that. My fault, I suppose I should read through the code s-l-o-w-e-r. Sorry for the bad advice.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

It's all good, bud.
Post Reply