Hi all,

I've looked through this forum for some hours and am absolutely unable to fix my problem; no matter what I do.

I'm trying to pass a PHP variable as part of a MYSQL query like so:

$id = $_GET["bodyid"];

$blog = getEntries("SELECT `Articlebody` FROM `Blog` WHERE id ='$id'");

function getEntries($query) {
  $connection = mysql_open();
    
	echo $query;
  
  $result = @ mysql_query($query, $connection)
      or showerror();
  $blog = array();
  while ($row = mysql_fetch_array($result)) {
      $blog[] = $row;
  }
  mysql_close($connection)
      or showerror();
  return $blog;
}

What is happening is that the query is returning no results because the variable is not being recognized for whatever reason.

The echo statement in the getEntries method is returning :

SELECT `Articlebody` FROM `Blog` WHERE id =''

Could somebody point me in the right direction?

Thanks.

Print and check whether your page is getting the values properly or not, by keeping following code in the begining of your page.

echo "<pre>";
print_r($_GET);
echo "</pre>";

Thanks for the speedy reply.

When I do that, it echos

Array
(
[blogid] => 3
)

Why do you have quotes around the table name and column name? I've never done that.

Change your code from
Your code should be changed from

$blog = getEntries("SELECT `Articlebody` FROM `Blog` WHERE id ='$id'");

to

$blog = getEntries("SELECT `Articlebody` FROM `Blog` WHERE id ='".$id."'");
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.