PDA

View Full Version : mysql_fetch_array with while loops infinite


fimi
01-22-2004, 04:12 PM
i've made a database class and when i call the sql_array() function in the class it gives me the array but when i try to print
the data in the array it prints an infinite loop and nothing else.
i tested sql_numrows() tells me that there are 9 records in that table.

i don't understand why its not working!

here's the code.




<?

$test = new query("select * from [TABLE]");
$numrows = $test->sql_numrows();
echo "<br>how many records found: $numrows<br>";

while ($row = $test->sql_array()){
echo "-field A: ".$row['a_field']."<br>";
}

class query{
var $server;
var $db;
var $user;
var $password ;
var $error;
var $query;


function query($value){
//initialize variables
$this->server = "localhost";
$this->db = "adatabse";
$this->user = "ausername";
$this->password = "apassword";
$this->query = $value;

//connect to server and select database
$db_con = @mysql_connect($this->server,$this->user,$this->password) or die( $this->error = "Database connection failure!");
$db_sel = @mysql_select_db($this->db) or die ( $this->error ="Could not connect to specified database, db may not exist");

//check if errors occured.
if (!$db_con){
echo $this->error;
exit;
}elseif (!$db_sel){
echo $this->error ;
exit;
}

}
function sql_query(){
//exec query...
$sql_result = mysql_query($this->query) or die($this->error ="Error:&nbsp;<b>".mysql_error()."</b>.");

//if any errors occur?
if(!$result_sql){
echo $this->error;
exit;
}else{
return $result_sql;
}
}function sql_array(){
$sql_result = mysql_query($this->query);
$sql_arrays = mysql_fetch_array($sql_result);
return ($sql_arrays);

}function sql_numrows(){
$sql_result = mysql_query($this->query) or die($this->error ="Error:&nbsp;<b>".mysql_error()."</b>.");
$sql_numrows = mysql_num_rows($sql_result);
return $sql_numrows;

}

}

?>



output
how many records found: 9
-field A:
-field A:
-field A:
-field A:
-field A:
........ and so on

forum search!( mysql_fetch_array , infinte loop, while infinite)

Dylan Leblanc
01-22-2004, 07:42 PM
Calling mysql_query() multiple times doesn't seem right to me. When you want to make a query, do it once and then get the rows. Don't make the query over and over again.

fimi
01-23-2004, 02:35 AM
can you guys try it out? i'm using php 4.2.3
i just want to see if the problem is with php...
its easy just save the file and change the values of database username pass and db. Then change the query...

I know its right(at least that's what i know).

thank you,
fimi

Dylan Leblanc
01-23-2004, 08:23 AM
Looking at your class, you should probably put mysql_query() in the constructor, and take it out of all the other functions.

raf
01-23-2004, 12:03 PM
Originally posted by fimi
can you guys try it out? i'm using php 4.2.3
i just want to see if the problem is with php...
its easy just save the file and change the values of database username pass and db. Then change the query...

I know its right(at least that's what i know).

thank you,
fimi
I could and would, if i would see the point of your code.
I don't see any added value over existing, build in PHP functions so i'm not gonna bother trying it out. In fact, the datamanipulation will be less performant + you'll open a connection and do a db-select for each query which will probably have an additional performance drop

I frankly don't understand why you bother writing it .:confused:

ConfusedOfLife
01-23-2004, 06:01 PM
Hi all!

Well, let's first take a look at your sql_query method, even though it doesn't have anything to do with your problem!


function sql_query(){
//exec query...
$sql_result = mysql_query($this->query) or die($this->error ="Error:&nbsp;<b>".mysql_error()."</b>.");

//if any errors occur?
if(!$result_sql){
echo $this->error;
exit;
}else{
return $result_sql;
}


You are putting the results of mysql_query into $sql_result and then in some lines below it, exactly in that if/else thing, you check for $result_sql, why is that?!

Now let's go to your problem. Hey, just imagine that you have an array like $whatever, so, if you wana iterate it by a while, you should do something like this:


$whatever = array( 5, 10, 7, 32, 83 );
while ( list($index, $value) = each($whatever) )
{
print "whatever[$index] = $value";
}


As you see you are not allowed to use that code for a while! Don't mistake it when you normally write mysql_fetch_array for example and it works! You know, when you write:

$myData = mysql_fetch_row($res);


$myData only holds one record of your specified table! Well, if it's the first time that you are using mysql_fetch_row() on your resource identifier ($res) then you get the first record, the 2nd time you get the 2nd record and so on. As you see mySql itself has a sort of pointer that remembers where the pointer is standing and updates it whenever you use these functions. Also it seems that PHP has changed its syntax for these mysql functions to make it easier for us to use them. So, I would recommend you change myslq_fetch_array() to mysql_fetch_row() or something better like mysql_fetch_assoc(), because mysql_fetch_array() returns both the numeric and associative arrays. Now that you're using a class, it's better that you use your sql_query() method inside sql_array(), else I see no reason you wrote that method.

You raf, you're right!! I really think that PHP functions are OO enough that we don't wana make our job harder! I really think there is nothing more object oriented than a simple query used inside mysql_query()! But well, ppl have different ways, you know that.

fimi
01-25-2004, 06:46 AM
all i'm trying to do is make a simple class that would reduce script size and increase performance. I do alot of comparing, listing, counting rows of each and i think it would make a difference if i use a class like this(similar what i've had before):

<?
class query{
var $server;
var $db;
var $user;
var $password ;
var $error;
var $result;
var $conID;


// CONNECT and run the query then store the result into $this->result
function query($sql){
$this->server = "localhost";
$this->db = "databse";
$this->user = "user";
$this->password = "pass";

//connect to server and select database
$db_con = @mysql_connect($this->server,$this->user,$this->password) or die( $this->error = "Database connection failure!");
$db_sel = @mysql_select_db($this->db) or die ( $this->error ="Could not connect to specified database, db may not exist");

$this->conID = $db_con;

// run query
$this->result = @mysql_query($sql) or die($this->error ="Error:&nbsp;<b>".mysql_error()."</b>.");

//if errors then prints the error msg.
if (!$db_con){
echo $this->error;
exit;
}elseif (!$db_sel){
echo $this->error ;
exit;
}
}function putin_array(){
$sql_arrays = mysql_fetch_array($this->result);
return ($sql_arrays);
}function numrows(){
$sql_numrows = mysql_num_rows($this->result);
return $sql_numrows;
}function affectedrows(){
$sql_affrows = mysql_affected_rows($this->result);
return $sql_affrows;
}function close(){
@mysql_free_result($this->conID);
@mysql_close($this->conID);
}

}

$dbtest = new query("Select * from atable");
echo "rows: ".$dbtest->numrows()."<br>";

while($row=$dbtest->putin_array()){
echo $row['arow']."<br>";
} // while
$dbtest->close();



?>
there, i figured out....very simple

tank you all for replying and trying to help, i really appreciate it.