Jump to content

mysql_fetch_array Error


abjones116

Recommended Posts

Hello All,

 

I am trying to pull multiple transactions from the database. I used the code below and it worked with only one resource it was returning from the database meeting those parameters. When I added another row of data to the database with the same parameters I received the following error:

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/nabbpa/public_html/membership/member_home.php on line 122

 

The DB Connection is fine. I added the die mysql_error() however no error is be retrieved on the page. It also has looped the same data twice but only the first result and not the second as intended.

 

/* Get Transactions For Current User */

$select = "SELECT * FROM `transactions` WHERE `m_id` = '59' ";

$result = mysql_query($select) or die(mysql_error());

$row = mysql_fetch_array($result);

 

if (mysql_num_rows($result)== "0") {

echo "<p align=\"center\">No Transactions Available.</p>";

 

} else {

 

$txn_time = $row['time'];

$format = "<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>";

$date = date('m/d/Y', strtotime($txn_time));

 

do {

 

printf($format, $date, $row['transaction_id'], $row['method'], $row['amount'], $row['status']);

} while($result = mysql_fetch_array($result));

 

?>

</table> <? } ?>

<p> </p>

<p> </p>

<p> </p>

<p> </p>

<p> </p>

<p> </p>

<p> </p>

<p> </p>

 

<?

 

/* Include Footer */

include("../includes/footer.php");

 

?>

 

I appreciate any help

Link to comment
https://forums.phpfreaks.com/topic/267568-mysql_fetch_array-error/
Share on other sites

Thanks for the response. Nothing is being returned on the mysql_error() which is throwing me off. Also, before I added another row with m_id = "59" It returned the result correctly. It still returns two rows of the same data as well, but there is different data it should be returning.

1) you are calling mysql_fetch_array before your do...while loop, which is pulling the first row from the result source.  then you have it in your do...while loop

2) you are using  a do..while loop, which will iterate once before the mysql_fetch_array is actually called.

3) In your do..while loop, you are assigning the mysql_fetch_array returned value to $result, but use $row in your loop

4) $result is your result source.  You then overwrite it in your while condition.

 

 

This is what it should look like:

 

/*   Get Transactions For Current User   */
$select = "SELECT * FROM `transactions` WHERE `m_id` = '59' ";
$result   = mysql_query($select) or die(mysql_error());
if (mysql_num_rows($result)== "0")   {
  echo "<p align=\"center\">No Transactions Available.</p>";
}   else   {
  while($row = mysql_fetch_array($result)) {
    $txn_time   =   $row['time'];
    $format      =   "<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>";
    $date      =   date('m/d/Y', strtotime($txn_time));
    printf($format, $date, $row['transaction_id'], $row['method'], $row['amount'], $row['status']);
  }   

?>
                                    </table> <? }   ?>
                                    <p> </p>
                                    <p> </p>
                                    <p> </p>
                                    <p> </p>
                                    <p> </p>
                                    <p> </p>
                                    <p> </p>
                                    <p> </p>

<?

/*   Include Footer   */
   include("../includes/footer.php");
   
?>                     

I see. Your updates worked. I appreciate your help!

 

1) you are calling mysql_fetch_array before your do...while loop, which is pulling the first row from the result source.  then you have it in your do...while loop

2) you are using  a do..while loop, which will iterate once before the mysql_fetch_array is actually called.

3) In your do..while loop, you are assigning the mysql_fetch_array returned value to $result, but use $row in your loop

4) $result is your result source.  You then overwrite it in your while condition.

 

 

This is what it should look like:

 

/*   Get Transactions For Current User   */
$select = "SELECT * FROM `transactions` WHERE `m_id` = '59' ";
$result   = mysql_query($select) or die(mysql_error());
if (mysql_num_rows($result)== "0")   {
  echo "<p align=\"center\">No Transactions Available.</p>";
}   else   {
  while($row = mysql_fetch_array($result)) {
    $txn_time   =   $row['time'];
    $format      =   "<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>";
    $date      =   date('m/d/Y', strtotime($txn_time));
    printf($format, $date, $row['transaction_id'], $row['method'], $row['amount'], $row['status']);
  }   

?>
                                    </table> <? }   ?>
                                    <p> </p>
                                    <p> </p>
                                    <p> </p>
                                    <p> </p>
                                    <p> </p>
                                    <p> </p>
                                    <p> </p>
                                    <p> </p>

<?

/*   Include Footer   */
   include("../includes/footer.php");
   
?>                     

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.