943,767 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 570
  • PHP RSS
Mar 4th, 2009
0

mysql_fetch_array failure?

Expand Post »
Hi there!

I'm having a very odd problem with mysql_fetch_array my code looks like this:

PHP Syntax (Toggle Plain Text)
  1. function getMachines($domain){
  2. if (! is_resource ( $this->connection )) {
  3. return false;
  4. } else {
  5. if (get_magic_quotes_gpc ()) {
  6. $domain = stripslashes ( $domain );
  7. }
  8. $query = sprintf ( "select machines from `domains`.`%s`", $this->escape ( $domain ) );
  9. $result = $this->query ( $query );
  10. if (empty ( $result ) || ! $result) {
  11. return false;
  12. } else {
  13. return $result;
  14. }
  15. }
  16. }
  17.  
  18. $result = getMachines($domain);
  19. echo mysql_num_rows($result);
  20. $row = mysql_fetch_array($result, MYSQL_NUM);

Notes: $this->escape is a shortcut function to mysql_real_escape_string, $this->connection is a private variable holding the sql connection, $this->query is a shortcut to mysql_query.

The problem is that the mysql_num_rows echos out 27, which is correct, but when I do a print_r on $row it only displays only the first item that should be in the select result, and count($row) outputs 1.

Anyone have any ideas? I am using the same basic code other places with no problems.

Thanks!
Last edited by naekur; Mar 4th, 2009 at 4:26 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
naekur is offline Offline
3 posts
since Mar 2009
Mar 4th, 2009
0

Re: mysql_fetch_array failure?

$result will have a result resource. You need a loop to get all the result. mysql_fetch_array() will fetch only 1 record at a time.
php Syntax (Toggle Plain Text)
  1. while($row = mysql_fetch_array($result,MYSQL_NUM)) {
  2. print "<pre>";
  3. print_r($row);
  4. print "</pre>";
  5. }
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Mar 4th, 2009
0

Re: mysql_fetch_array failure?

Thanks! that seems to have fixed it, I guess I misunderstoond how mysql_fetch_array worked.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
naekur is offline Offline
3 posts
since Mar 2009
Mar 4th, 2009
0

Re: mysql_fetch_array failure?

Cool!
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Help! my script couldn't submit form data to mysql database
Next Thread in PHP Forum Timeline: Restarting Apps Using Php





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC