Nathan Riley
asked on
PHP MYSQL Select
I'm not getting any results returned, can't figure out why. The query works and returns the result in phpmyadmin.
<?php
//Connect to the Database
$host = localhost;
$username = db_user;
$password = 'pass';
$connect = mysql_connect($host,$username,$password);
//Select the Database
$db = 'db_pm';
mysql_select_db($db);
//Query the needed data
$query = "select id,userid,title,description from projects where id=1";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
?>
<link href="../assets/front.css" rel="stylesheet" type="text/css" />
<?php include("header.php");?>
<div class="workplace">
<div class="newprojectbox">
<h1><?php echo($query['id']); ?><h1>
</div>
</div>
<?php include("footer.php");?>
not really a php coder but shouldn't it be $result['id'], not $query['id'] in line 20?
$result is not the data but the 'resource id'. You need to use 'mysql_fetch_array' or one of the other 'mysql_fetch_...' functions to actually get the data. See here: http://us3.php.net/manual/en/function.mysql-fetch-array.php
ah DaveBaldwin is here now so I can stop guessing PHP code and stick to MySQL =)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
And Ray_Paseur too! Gallitin, you're in good hands. I'll leave this question now.
I missed some quotes.
$host = "localhost";
$username = "db_user";
$password = 'pass';
In this query...
$query = "select id,userid,title,descriptio n from projects where id=1";
... I expect that "id=1" is an AUTO_INCREMENT KEY for the table. Since it is almost certainly unique and since you apparently want only one row, you might want to add LIMIT 1 to the query. Without that your query will cause a table scan, and a table scan is like dog poop - it's out of place almost anywhere you find it.
$query = "select id,userid,title,descriptio
... I expect that "id=1" is an AUTO_INCREMENT KEY for the table. Since it is almost certainly unique and since you apparently want only one row, you might want to add LIMIT 1 to the query. Without that your query will cause a table scan, and a table scan is like dog poop - it's out of place almost anywhere you find it.
ASKER
Thanks guys, ended up getting it with.
Sucks you have too keep that loop open and all code in it to use it. I don't like echoing out all the other html code. But oh well.
<?php
//Is the user logged in?
define("_VALID_PHP", true);
require_once("init.php");
if (!$user->logged_in)
redirect_to("index.php");
$row = $user->getUserData();
//Connect to the Database
$host = 'localhost';
$username = 'db_user';
$password = 'password';
$connect = mysql_connect($host,$username,$password);
if (!$connect) { die('Could not connect: ' . mysql_error()); }
//Select the Database
mysql_select_db("db_pm", $connect);
//Query the needed data
$result = mysql_query("SELECT id,userid,title,description FROM projects where id=1");
while($row = mysql_fetch_array($result))
{
echo '<link href="../assets/front.css" rel="stylesheet" type="text/css" />';
include("header.php");
echo '<div class="workplace"><div class="newprojectbox"><h1>';
echo $row['title'];
echo '<h1></div></div>';
include("footer.php");
}
?>
Sucks you have too keep that loop open and all code in it to use it. I don't like echoing out all the other html code. But oh well.
Sucks you have too keep that loop open and all...Are you referring to the while() ? If so, you might want to add the LIMIT 1 clause, omit the while() and just replace lines 17 through the end with something like this...
//CONSTRUCT QUERY STRING
$sql = "SELECT id,userid,title,description FROM projects WHERE id=1 LIMIT 1";
// RUN QUERY OR SHOW ERROR INFO
$res = mysql_query($sql) or die("FAIL: $sql <br/>" . mysql_error() );
// IF QUERY FOUND NOTHING
if (!mysql_num_rows($res)) die("NONE: $sql");
// RETRIEVE RESULTS SET
$row = mysql_fetch_assoc($res);
// CREATE WEB PAGE
echo '<link href="../assets/front.css" rel="stylesheet" type="text/css" />';
include("header.php");
echo '<div class="workplace"><div class="newprojectbox"><h1>';
echo $row['title'];
echo '<h1></div></div>';
include("footer.php");
ASKER