Display values from table

Right now I have:

[php]
$page_id = 0;
$page_id = $_GET[‘id’];
if ($page_id==0)
{
echo “
Error: Page ID Not specified. Comments disabled.”;
}
else
{
echo “
Comments:
”;
$con = mysql_connect(“localhost”,“dbname”,“dbass”);
if (!$con)
{
die('Could not connect: ’ . mysql_error());
}
mysql_select_db(“dbname”, $con);
$comments = mysql_query (“SELECT * FROM comments WHERE Page==$page_id”);
echo $comments[‘Content’];
mysql_close ($con);
}
[/php]

to try and display all content values from a table where the value for “Page” is equal to $page_id specified in the url. However, I have to records in the table that fit this specification, and the content value isn’t visible.

When you run a SELECT query, mysql_query will return FALSE if it fails or a MySQL resource.

To use the MySQL resource, you can:

Check if any rows where returned (mysql_num_rows):
[php]$num_rows = mysql_num_rows($comments);

if($num_rows >= 1) {
echo ‘Do Something’;
}[/php]

Then, we can fetch records from the MySQL resource - using mysql_fetch_array:

[php]if($num_rows >= 1) {
while($row = mysql_fetch_array($comments)) {
echo $comments[‘Content’];
}
}[/php]

Also, your SQL query is invalid. In SQL, you use 1 equals sign for comparison:

[php]$comments = mysql_query (“SELECT * FROM comments WHERE Page==$page_id”);[/php]

Should be:

[php]$comments = mysql_query(“SELECT * FROM comments WHERE Page=$page_id”);[/php]

As well as this, if you’re getting input from the user, you MUST escape it first to prevent SQL injection:

[php]$page_id = mysql_real_escape_string($page_id);[/php]

I figured this out some time ago, but thanks anyways for the help.

Sponsor our Newsletter | Privacy Policy | Terms of Service