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!

$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>";
}

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

:) Cool!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.