954,600 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Passing mysql query with Php variables in it.

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.

python08
Newbie Poster
4 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

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>";
urtrivedi
Nearly a Posting Virtuoso
1,306 posts since Dec 2008
Reputation Points: 257
Solved Threads: 270
 

Thanks for the speedy reply.

When I do that, it echos

Array
(
[blogid] => 3
)

python08
Newbie Poster
4 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

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

drjohn
Posting Pro in Training
448 posts since Mar 2010
Reputation Points: 76
Solved Threads: 80
 

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."'");
warren88
Newbie Poster
1 post since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: