(PHP/MySQL) What am I doing wrong?

  • Thread starter Thread starter skinnypspplayer
  • Start date Start date
S

skinnypspplayer

Guest
<?php include('session.php');?>
<?php

$_GET['id']='id';

$result = mysql_query("SELECT * FROM comments WHERE id='$id'");

while($row = mysql_fetch_array($result))
{
echo $row['id'];
}
?>

..the $_GET['id'] is simply just pulling a certain comment ID off another page. I just want it to display (to make sure it works) the ID on the page.
 
The other answer is almost correct (he just missed a single quote).

In any programming language, assignments go from right to left. Right side is the value you're assigning to the left side.

So X = 1 is right. 1 = X is wrong.

In this case, you want to put the value of $_GET['id'] in to the variable $id. You need to put the target of the assignment on the left side, and the object that has the value on the right.

$id = $_GET['id'];
 
The superglobal $_GET is populated by a query string variable.

For example, if your page is comment.php?id=1

Then $_GET['id'] will be 1.

So, if someone clicks o a link that sends them to comment.php?id=1, your code should be:

<?php include('session.php');?>
<?php
// I'll assume you already have a connection

if( !$result = mysql_query("SELECT * FROM comments WHERE id = '" . mysql_real_escape_string( $_GET['id'] ) . "'") ) {
die(mysql_error());
}
elseif( mysql_num_rows($result) == 0) {
die("no such record found");
else {
while($row = mysql_fetch_array($result)) {
echo $row['id'];
}
}
?>

UPDATE:

I did have some improper syntax; I forgot to concatenate my strings in the query. Should work now.
 
Back
Top