Page 1 of 1

Getting Results Twice - SOLVED

Posted: Tue Jun 17, 2008 8:21 am
by psurrena
Hello...

My code below is returning the results twice. Not like a,b,c,a,b,c but a,a,b,b,c,c. Any ideas?

Code: Select all

<?php
    class NewProject{
        protected $pid;
        protected $query;
        protected $errorMsg="";
        
        public function __construct(){
            $this->pid=$_GET['id']; 
        }
            
        public function projectQuery(){
            $this->query="SELECT project_id, project_name, project_des
                FROM project 
                WHERE project_id='$this->pid'";
                
            return $this->query;
        }
        
        public function queryResults(){
            
            $result=mysql_query($this->query) or die (mysql_error());
            if($result){
                $row=mysql_fetch_array($result);
            }else{
                $this->errorMsg="Sorry, this project does not exist";
            }       
            return $row;
        }
    }
 
    //Test Class
    $project=new NewProject();
    $project->projectQuery();
    $thing=$project->queryResults();
    
    foreach ($thing as $key => $value) {
        echo $value."<br />";
    }
?>

Re: Getting Results Twice

Posted: Tue Jun 17, 2008 8:48 am
by Dj_Lord
try debugging with print_r() and locating the exact place of the wrong code.

Re: Getting Results Twice

Posted: Tue Jun 17, 2008 8:50 am
by WebbieDave
mysql_fetch_array returns a combination array (an array that uses both associative and numerical indexes). To see what I mean, do a print_r($thing); after $thing=$project->queryResults();

To get only an associative array, you can replace:

Code: Select all

$row=mysql_fetch_array($result);
with:

Code: Select all

$row=mysql_fetch_assoc($result);
or:

Code: Select all

$row=mysql_fetch_array($result, MYSQL_ASSOC);
To get a numerical only indexed array, change MYSQL_ASSOC to MYSQL_NUM.

Re: Getting Results Twice - SOLVED

Posted: Tue Jun 17, 2008 9:27 am
by psurrena
Thanks, mysql_fetch_assoc worked.