Jump to content

Problems with mysql_fetch_array


cgm225

Recommended Posts

I am using the mysql_fetch_array() function in one of my classes, but when I use the results of the mysql_fetch_array() in a while statement (a result which is not empty, and contains more than on entry (I checked it by simply print_r 'ing the array)), it only echos one row/entry, instead of looping through all of them as I want.

 

Any ideas?

 

Here is the part of my class using mysql_fetch_array()...

    public function Query($sql){
        $this->sql = $sql;
        $this->result = mysql_query($this->sql,$this->connection) or die(mysql_error());
        return $this->result;
    }

    public function FetchArray($query){
        $rows = mysql_fetch_array($this->Query($query));
        return $rows;
    }

 

Here is the while statement in my script (which only echos a single entry from the $row array even though there are multiple entries in the array)...

 

    $query = "SELECT * FROM albums";

    while($row = $gallery->FetchArray($query)) {
       echo $row['short_title']; 
    }

Link to comment
Share on other sites

Using the FetchArray() method like that doesn't work, as it outputs a single static row. You have to have you while loop set up like this:

 

while($row = mysql_fetch_array($sql)) {

  echo $row['attribute'];

}

 

I believe that the mysql_fetch_array() executes the query each time through the loop, accessing the next row until there are none left. So the way you are doing it only retrieves the first row of data.

Link to comment
Share on other sites

You are passing mysql_fetch_array() an sql query. It expects a results resource. This should work.

 

<?php

public function Query($sql) {
  $this->sql = $sql;
  $this->result = mysql_query($this->sql,$this->connection) or die(mysql_error());
}

public function FetchArray() {
  return mysql_fetch_array($this->result);
}

?>

 

then....

 

<?php

$query = "SELECT * FROM albums";
$gallery->Query($query);
while($row = $gallery->FetchArray()) {
  echo $row['short_title']; 
}

?>

 

However, this class does look pretty sloppy and is qoing to be pretty difficult to debug when you do hit an error.

Link to comment
Share on other sites

Thank you both for your help.. it is working now.

 

@Thorpe - I really appreciate your feedback on the class. 

 

With that being said, I have posted my full class below and wanted to know if you could give me some feedback on making it better for debugging?  I am VERY new to OOP, and want to do this the right way.  Any other feedback on naming conventions, syntax, making it more efficient, etc. would be greatly appreciated!

 

class mysql {
    private $connection;   
    protected $result;     
    protected $sql;

    public function Connect($host, $name, $pass, $db) {
        $this->connection = mysql_connect($host,$name,$pass);
        mysql_select_db($db, $this->connection);        
        return true; 
    }

    public function Close(){
        mysql_close($this->connection);
        return true;
    }

    public function Query($sql) {
        $this->sql = $sql;
        $this->result = mysql_query($this->sql,$this->connection) or die(mysql_error());
    }

    public function FetchArray() {
        return mysql_fetch_array($this->result);
    }

    public function FetchNum(){
        return mysql_num_rows($this->result);
    }
}

 

Thanks again so much!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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