Getting Results Twice - SOLVED

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Getting Results Twice - SOLVED

Post 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 />";
    }
?>
Last edited by psurrena on Tue Jun 17, 2008 9:27 am, edited 1 time in total.
Dj_Lord
Forum Newbie
Posts: 6
Joined: Mon Jun 16, 2008 11:51 am

Re: Getting Results Twice

Post by Dj_Lord »

try debugging with print_r() and locating the exact place of the wrong code.
WebbieDave
Forum Contributor
Posts: 213
Joined: Sun Jul 15, 2007 7:07 am

Re: Getting Results Twice

Post 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.
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Re: Getting Results Twice - SOLVED

Post by psurrena »

Thanks, mysql_fetch_assoc worked.
Post Reply