MjM8082 Posted July 26, 2012 Share Posted July 26, 2012 I'm trying to pull information out of my database and display it on my index.php page for my site. I know it is not my connection because my insert statement works just fine... not sure what the problem might be? Here is a look at my php code. include "connect_to_mysql.php"; $sql = mysql_query("SELECT * FROM posts WHERE title='$title' AND content='$content'"); $result = mysql_query ($query); while ($row = mysql_fetch_array($result)) { $title = stripslashes($row['title']); $display_block .= "<p><strong></strong> $title </p>"; } Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted July 26, 2012 Share Posted July 26, 2012 How can we tell you what the problem might be when you haven't described the symptoms? Quote Link to comment Share on other sites More sharing options...
NomadicJosh Posted July 26, 2012 Share Posted July 26, 2012 Change $query to $sql $result = mysql_query ($sql); Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 26, 2012 Share Posted July 26, 2012 A few things: 1. You do not have any error handling on your query. It would have told you the query was empty. 2. Create your queries as string variables. Then if there is an error you can echo the query to the page to inspect it for errors. 3. If you are only using the 'title' from the table, then don't use '*' in your select query. It is a waste of resources. 4. Why are you using stripslashes() on a value extracted from the database? Always understand what 'format' the values shoud be in and what processes should be used to escape the values based upon the usage. Quote Link to comment Share on other sites More sharing options...
MjM8082 Posted July 26, 2012 Author Share Posted July 26, 2012 Sorry, to be more specific my output just gives me this.... $title "; } ?> That is it. I messed around with my code multiple times and I have even tried the $sql instead of $query. Quote Link to comment Share on other sites More sharing options...
NomadicJosh Posted July 26, 2012 Share Posted July 26, 2012 Sorry, my bad. You are running mysql_query twice. Try changing your code to this: $sql = mysql_query("SELECT * FROM posts WHERE title='$title' AND content='$content'"); while ($row = mysql_fetch_array($sql)) { Quote Link to comment Share on other sites More sharing options...
MjM8082 Posted July 26, 2012 Author Share Posted July 26, 2012 Thanks for the reply, after trying that code I still came out with the same output ... $title "; } ?> That is all that appears on the page Quote Link to comment Share on other sites More sharing options...
NomadicJosh Posted July 26, 2012 Share Posted July 26, 2012 What does this give you? include "connect_to_mysql.php"; $sql = mysql_query("SELECT * FROM posts WHERE title='$title' AND content='$content'"); while ($row = mysql_fetch_array($sql)) { $title = $row['title']; $display_block .= "<p><strong></strong>" . $title . "</p>"; } You might want to take out the $display_block out of your loop. It would be better to try something like this to see if it works: include "connect_to_mysql.php"; $sql = mysql_query("SELECT * FROM posts WHERE title='$title' AND content='$content'"); while ($row = mysql_fetch_array($sql)) { $title = $row['title']; } echo $title; Quote Link to comment Share on other sites More sharing options...
MjM8082 Posted July 26, 2012 Author Share Posted July 26, 2012 Okay I tried the new code you posted and now I just get a blank page :-\ Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted July 26, 2012 Share Posted July 26, 2012 Since $title and $content don't exist in the code you are using, there's no way that the query will match any rows in your database table. What exactly are you trying to achieve by that query statement and where are you expecting $title and $content to be set from? Quote Link to comment Share on other sites More sharing options...
MjM8082 Posted July 26, 2012 Author Share Posted July 26, 2012 Sorry, I am new to PHP and trying out my first time with phpmyadmin and mysql, so this is one of my first select statements, so I'm just trying to work with the knowledge I have right now. Quote Link to comment Share on other sites More sharing options...
MjM8082 Posted July 26, 2012 Author Share Posted July 26, 2012 Notice: Undefined index: title in /home6/projeev1/public_html/maddykelley/blankpage.php on line 18 Notice: Undefined index: content in /home6/projeev1/public_html/maddykelley/blankpage.php on line 19 These are the errors I am getting Quote Link to comment Share on other sites More sharing options...
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.