Page 1 of 1

question about how PHP handles mySQL query results

Posted: Mon Jul 12, 2004 4:53 pm
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

Posted: Mon Jul 12, 2004 6:38 pm
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.

Posted: Mon Jul 12, 2004 6:49 pm
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']; 

?>

Posted: Mon Jul 12, 2004 8:52 pm
by brewmiser
Out of curosity, does your table really have lower and uppercase letters for the description of the data? i.e. 'intArtistID'. 8O

Posted: Tue Jul 13, 2004 2:24 pm
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.

Posted: Tue Jul 13, 2004 2:28 pm
by feyd
It's all good, bud.