Jump to content

SELECT statement errors


MjM8082

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

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

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.