Jump to content
Nathan

PHP MYSQL Select Not Returning Data

Recommended Posts

My below code isn't returning data for

<h1><?php echo($query['id']); ?><h1>

 

What am I missing?

 

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

Share this post


Link to post
Share on other sites

Geez, learning a new language is tough, been on this for about 2 hours now. It was as simple as the lower design code wasn't included in the PHP while loop.

 

My code doesn't look as pretty now but it works :)

 

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

Share this post


Link to post
Share on other sites

Hello Nathan, good effort!

 

After reading your post, I was going to suggestion improvements. However it prompted me to write up a tutorial instead:

 

https://forums.prodjex.com/topic/3115-writing-clean-robust-php-database-code-tutorial/

 

In the above tutorial, I have shown a cleaner approach to database interactions in php. This would be my recommendation, hopefully this will help others too! :)

Share this post


Link to post
Share on other sites

Nice thanks for that, should help me clean up my code then :)

 

I did a little bit of code clean-up and formatting. It does the same thing, however is a bit cleaner.

<?php
define ( "_VALID_PHP", true );
require_once ( "init.php" );

// Validate that the user is logged in.
if ( !$user->logged_in )
{
 redirect_to ( "index.php" );
 exit;
}

// Establish the database connection.
if ( !( $link = @mysql_connect ( 'localhost', 'db_user', 'password' ) ) )
{
 // Throw connection error.
 die ( 'Could not connect: ' . mysql_error ( ) );
}
// Select database schema.
@mysql_select_db("db_pm", $link);


// Query and display information.
$query = @mysql_query ( "SELECT `id`, `userid`, `title`, `description` FROM `projects` where `id` = '1';" );
while ( ( $row = @mysql_fetch_array ( $query ) ) != null )
{
 echo '<link href="../assets/front.css" rel="stylesheet" type="text/css" />';
 include ( "header.php" );
 echo '<div class="workplace"><div class="newprojectbox"><h1>' . $row [ 'title' ] . '<h1></div></div>';
 include ( "footer.php" );
}

// Close mysql connection.
@mysql_close ( $link );
?>

Share this post


Link to post
Share on other sites

Looking at your original code at the top, it seems your trying to return data from a string.

 

ie.

 

 

<h1><?php echo($query['id']); ?><h1>

 

 

$query is just the string that you passed in. Your result set is $result. Since $query is not an array I am assuming that you will have been getting nothing from the echo statement

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...