Jump to content

[SOLVED] Infinite Array Class Loop Problem


FVxSF

Recommended Posts

I've been working on a basic database class and so far every thing works just fine except for fetching an array. When I try to fetch an array I get caught in an infinite loop. But once I take the code out of the class, it works as expected. Can some one help me?

 

<?php
class DB {

	var $ReportError;

	function Connect() {
		$cnRes = mysql_connect(cSERVER, cUSER, cPASSWORD);
		if (!$cnRes) {
			$this->ReportError = mysql_error();
			return false;
		} else {
			$cnSRes = mysql_select_db(cSELECTED);
			if (!$cnSRes) {
				$this->ReportError = mysql_error();
				return false;
			}
		}
		return true;
	}

	function FetchArray($SQL) {
		$reRes = mysql_query($SQL);
		if (!$reRes) { return false; }

		return mysql_fetch_array($reRes);
	}
}


$DB = new DB;

if (!$DB->Connect()) { die($DB->ReportError); }

$SQL = "SELECT * FROM node_files LIMIT 0,1";
while( $Row = $DB->FetchArray($SQL) ) {
	echo $Row[file_name] ." - ". $Row[file_location] ."<br>\n";
}

 

I'm probably doing a noobish thing. Thanks for any help.

You don't need the 'fetcharray()' function, it just keeps making queries over and over..

$DB = new DB;

if (!$DB->Connect()) { die($DB->ReportError); }

$SQL = "SELECT * FROM node_files LIMIT 0,1";
$reRes = mysql_query($SQL);
if ($reRes)
{
	while( $Row = mysql_fetch_array($reRes) ) {
		echo $Row[file_name] ." - ". $Row[file_location] ."<br>\n";
	}
}

Ok. I'll move the mysql_fetch_array out of the class. I'll be using this class on my site and my buddies site. Saves time.

 

<?php

$SQL = "SELECT * FROM node_files LIMIT 0,1";
$reRes = $DB->RunQuery($SQL);

while( $Row = mysql_fetch_array($reRes) ) {
	echo $Row[file_name] ." - ". $Row[file_location] ."<br>\n";
}
?>

Archived

This topic is now archived and is closed to further replies.

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