abjones116 Posted August 25, 2012 Share Posted August 25, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/267568-mysql_fetch_array-error/ Share on other sites More sharing options...
darkfreaks Posted August 25, 2012 Share Posted August 25, 2012 your query is returning a boolean of FALSE make sure there are no errors being returned via mysql_error . and make sure you are spelling the fields correctly and there are no typos. Quote Link to comment https://forums.phpfreaks.com/topic/267568-mysql_fetch_array-error/#findComment-1372399 Share on other sites More sharing options...
abjones116 Posted August 25, 2012 Author Share Posted August 25, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/267568-mysql_fetch_array-error/#findComment-1372400 Share on other sites More sharing options...
darkfreaks Posted August 25, 2012 Share Posted August 25, 2012 try this... if($result===FALSE) { die(mysql_error()); } tell me what happens if the query fails because it is a boolean. Quote Link to comment https://forums.phpfreaks.com/topic/267568-mysql_fetch_array-error/#findComment-1372401 Share on other sites More sharing options...
.josh Posted August 25, 2012 Share Posted August 25, 2012 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"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/267568-mysql_fetch_array-error/#findComment-1372402 Share on other sites More sharing options...
abjones116 Posted August 26, 2012 Author Share Posted August 26, 2012 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"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/267568-mysql_fetch_array-error/#findComment-1372407 Share on other sites More sharing options...
darkfreaks Posted August 26, 2012 Share Posted August 26, 2012 don't forget to mark as solved! Quote Link to comment https://forums.phpfreaks.com/topic/267568-mysql_fetch_array-error/#findComment-1372411 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.