开发者

Mysql query not returning any rows

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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜