Hi All,

I am developing a website in which i am selecting some values form database by a query When i am running query on PHPMyAdmin i am getting desired results but when i am using this query in actual PHP code, i am getting value of Last row. Can any one tell me , whats happening wrong?

Recommended Answers

All 6 Replies

<?php 
$projectname = $_GET['type'];
echo $projectname;

include('config.php');
            $sql = "SELECT 
            fld_projectname,fld_projectline,fld_projectpunchline,fld_projectbestimage,fld_projecttype,fld_projectdesp,
            fld_projectfeature1,fld_projectfeature2,fld_projectfeature3,fld_projectfeature4,fld_locationmap,fld_googlemap,
            fld_sitemap,fld_projectimage1,fld_projectimage2,fld_projectimage3,fld_projectimage4,fld_projectimage5,fld_projecttype,fld_projectlocation

         FROM tbl_allproject WHERE fld_encryptname = '$projectname'";

        // echo $sql; exit;



            $rs = mysql_query($sql);
            if(mysql_affected_rows()>0)
            {
            while($row = mysql_fetch_array($rs))
            {
                $projectname = $row['fld_projectname'];
                $projectline = $row['fld_projectline'];
                $projectpunchline = $row['fld_projectpunchline'];
                $propertbestimage = $row['fld_projectbestimage'];

                $projectdesp = $row['fld_projectdesp'];
                $projectfeature1 = $row['fld_projectfeature1'];
                $projectfeature2 = $row['fld_projectfeature2'];
                $projectfeature3 = $row['fld_projectfeature3'];
                $projectfeature4 = $row['fld_projectfeature4'];

                @$projetlocationmap = $row['fld_locatonmap'];
                $projectgooglemap = $row['fld_googlemap'];
                $projectsitemap = $row['fld_sitemap'];
                $projectimage1 = $row['fld_projectimage1'];
                $projectimage2 = $row['fld_projectimage2'];
                $projectimage3 = $row['fld_projectimage3'];
                $projectimage4 = $row['fld_projectimage4'];
                $projectimage5 = $row['fld_projectimage5'];
            }   

            }
            else
            {
                header('location: 404.php');
            }

?>

I am always getting values of last row..plz guide me.

You set your variables in the loop, but you do nothing with them. So what's remembered after the loop are the last results. Either use the variables inside the loop, or store them in an array (probably not necessary).

SELECT fld_projectname,fld_projectline,fld_projectpunchline,fld_projectbestimage,fld_projecttype,fld_projectdesp, fld_projectfeature1,fld_projectfeature2,fld_projectfeature3,fld_projectfeature4,fld_locationmap,fld_googlemap, fld_sitemap,fld_projectimage1,fld_projectimage2,fld_projectimage3,fld_projectimage4,fld_projectimage5,fld_projecttype,fld_projectlocation FROM tbl_allproject WHERE fld_encryptname = '84db7b5d4ddff5fb9afffc07d5fe6669'

If i run this query i get desired results, but when i used the varialbes i m getting values of last entry of database.

What @pritaeas is saying to you, is that you are setting the results in the while loop to variable names, but you are not printing out the results.

So for instance you would echo out the row in the while loop:

 while($row = mysql_fetch_array($rs))
{
    echo $row['fld_projectname'];
    echo $row['fld_projectline'];
    echo $row['fld_projectpunchline'];
    echo $row['fld_projectbestimage'];
    echo $row['fld_projectdesp'];
    echo $row['fld_projectfeature1'];
    echo $row['fld_projectfeature2'];
    echo $row['fld_projectfeature3'];
    echo $row['fld_projectfeature4'];
    echo $row['fld_locatonmap'];
    echo $row['fld_googlemap'];
    echo $row['fld_sitemap'];
    echo $row['fld_projectimage1'];
    echo $row['fld_projectimage2'];
    echo $row['fld_projectimage3'];
    echo $row['fld_projectimage4'];
    echo $row['fld_projectimage5'];
} 

This will print out the results on to the screen. Or if you wanted to use outside the loop you could store them in a array()

$results = array();

 while($row = mysql_fetch_array($rs))
    {
        $results[] = $row;
    } 
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.