Administrators Nathan Posted May 25, 2012 Administrators Posted May 25, 2012 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");?> Quote
Administrators Nathan Posted May 25, 2012 Author Administrators Posted May 25, 2012 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"); } ?> Quote
__Darknite Posted May 25, 2012 Posted May 25, 2012 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! Nathan 1 Quote
Administrators Nathan Posted May 25, 2012 Author Administrators Posted May 25, 2012 Nice thanks for that, should help me clean up my code then Quote
MrHappy Posted June 2, 2012 Posted June 2, 2012 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 ); ?> Nathan 1 Quote
Administrators Nathan Posted June 2, 2012 Author Administrators Posted June 2, 2012 Looks good thanks for the help! Quote
Marc Posted June 3, 2012 Posted June 3, 2012 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 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.