Avatar of Nathan Riley
Nathan Riley
Flag for United States of America 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");?>

Open in new window

PHPMySQL ServerWeb Development

Avatar of undefined
Last Comment
Ray Paseur

8/22/2022 - Mon
Nathan Riley

ASKER
Forgot to mention the result not being returned in in my code below at line 20.
johanntagle

not really a php coder but shouldn't it be $result['id'], not $query['id'] in line 20?
Dave Baldwin

$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
Your help has saved me hundreds of hours of internet surfing.
fblack61
johanntagle

ah DaveBaldwin is here now so I can stop guessing PHP code and stick to MySQL =)
ASKER CERTIFIED SOLUTION
Ray Paseur

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
johanntagle

And Ray_Paseur too!  Gallitin, you're in good hands.  I'll leave this question now.
Dave Baldwin

I missed some quotes.

$host = "localhost";
$username = "db_user";
$password = 'pass';

Open in new window

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Ray Paseur

In this query...

$query = "select id,userid,title,description 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.
Nathan Riley

ASKER
Thanks guys, ended up getting it with.

<?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");
}
?>

Open in new window


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.
Ray Paseur

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

Open in new window

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy