Take the 2-minute tour ×

I have a very simple script that pulls data from a mysql db, However when I run it there is no output, I am setting the get request to the correct value related to the row and when i run the SQL query in PHPmyadmin it runs as expected.

my code;

require_once 'includes/sessions.inc.php';
require_once 'includes/config.inc.php';

   echo "not logged in";
//Connect to DB

$id = $_SESSION['userid'];
$contactid = $_GET['contactid'];

//Get Data from DB
$query = "SELECT id, First_name, Last_name, email_addres, phone_number, photo, owner FROM tbl_contcats where id = '$contactid';";

echo '<img src="./contacts/'.$result['photo'].'"/><br>';
echo 'First name:'.$result['First_name'].'<br>';
echo 'Second name:'.$result['First_name'].'<br>';
echo 'Email Address:'.$result['First_name'].'<br>';
echo 'phone_number'.$result['First_name'].'<br>';



ps; any feed back on how I can improve my code/clean it up is apericated

share|improve this question
I've found that when this happens, if I ECHO the actual $query to the browser, and copy and paste that into PHPMyAdmin or Workbench, that it helps me see the answer. If the query still returns correctly, then you know that part can be eliminated, and look at your code more closely. –  Mikecito Apr 22 '11 at 3:36
you should make sure your input is clean too. php.net/manual/en/function.mysql-real-escape-string.php –  Matt Apr 22 '11 at 4:18
Where's your SQL Injection prevention? –  Lightness Races in Orbit Apr 22 '11 at 4:29
mysql_query returns a resultset, not a row. You have to iterate through the resultset to get your rows, using functions like mysql_fetch_assoc. –  Lightness Races in Orbit Apr 22 '11 at 4:30
I'v added SQLi prevention now... –  aaaa Apr 22 '11 at 14:55

1 Answer 1

up vote 5 down vote accepted

If you want to access the variables the way you're doing it, you'll first need to fetch an associative array of your results:

$rows = mysql_fetch_assoc($result);

Then you'll access the entries via

while ($row = mysql_fetch_assoc($result)) {
    echo '<img src="./contacts/'.$row['photo'].'"/><br />';
    echo 'First name:'.$row['First_name'].'<br />';
    echo 'Second name:'.$row['First_name'].'<br />';
    echo 'Email Address:'.$row['First_name'].'<br />';
    echo 'phone_number'.$row['First_name'].'<br />';

What you're doing right now is trying to access variables from a MySQL resource that is returned by the query. The mysql_query itself doesn't return a raw result set. ;)

share|improve this answer
Yes it does. What it doesn't do is return a row from that resultset. (Your answer is otherwise correct.) –  Lightness Races in Orbit Apr 22 '11 at 4:31
I know it returns a result set, but not "raw" in a form where you can access row entries directly. It's simply a resource. –  BraedenP Apr 22 '11 at 5:08

Your Answer


