954,157 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

mysql_fetch_array failure?

Hi there!

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

function getMachines($domain){
		if (! is_resource ( $this->connection )) {
			return false;
		} else {
			if (get_magic_quotes_gpc ()) {
				$domain = stripslashes ( $domain );
			}
			$query = sprintf ( "select machines from `domains`.`%s`", $this->escape ( $domain ) );
			$result = $this->query ( $query );
			if (empty ( $result ) || ! $result) {
				return false;
			} else {
				return $result;
			}
		}
}

$result = getMachines($domain);
echo mysql_num_rows($result);
$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!

naekur
Newbie Poster
3 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

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

while($row = mysql_fetch_array($result,MYSQL_NUM)) {
print "<pre>";
 print_r($row);
print "</pre>";
}
nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

Thanks! that seems to have fixed it, I guess I misunderstoond how mysql_fetch_array worked.

naekur
Newbie Poster
3 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

:) Cool!

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You