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
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.

Link to comment
Share on other sites

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");
   
?>                     

Link to comment
Share on other sites

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");
   
?>                     

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.