1

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;

session_start();
require_once 'includes/sessions.inc.php';
require_once 'includes/config.inc.php';

if(!isLoggedIn())
{
   echo "not logged in";
}
else
{
//Connect to DB
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname);

$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';";
$result = mysql_query($query) or die(mysql_error());

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

5
  • 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. Commented Apr 22, 2011 at 3:36
  • you should make sure your input is clean too. php.net/manual/en/function.mysql-real-escape-string.php Commented Apr 22, 2011 at 4:18
  • Where's your SQL Injection prevention? Commented Apr 22, 2011 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. Commented Apr 22, 2011 at 4:30
  • I'v added SQLi prevention now... Commented Apr 22, 2011 at 14:55

1 Answer 1

5

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. ;)

Sign up to request clarification or add additional context in comments.

2 Comments

Yes it does. What it doesn't do is return a row from that resultset. (Your answer is otherwise correct.)
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.