Jump to content

Passing $string variable into mysql query


spangle1187

Recommended Posts

I have echoed the $query string and I was missing data so have reassembled the order:

 

	$input = "Sam";
		$string = "clientName == '$input'";


		$table_id = 'booking'; 	
		$query ="SELECT * 
				FROM booking
				WHERE. '$string' ";


		$test = mysql_query($query);	

		echo $query;

 

and this is now showing the following for the $query string:

 

SELECT * FROM booking WHERE. 'clientName == 'Sam''

 

which is correct but I still have a warning associated with

 

	$test = mysql_query($query);

so this is where I will look now

A query that looks like

SELECT * FROM booking WHERE. 'clientName == 'Sam'' 

is not correct. Notice the "." after the WHERE and the single quote before the column name clientName and the extra single quote at the end. It should look like:

SELECT * FROM booking WHERE clientName == 'Sam' 

 

You code to create the query is wrong. It should be

<?php
$input = "Sam";
$string = "clientName == '$input'";
$table_id = 'booking'; 	
$query ="SELECT * FROM booking WHERE $string";
$test = mysql_query($query) or die("Problem with the query: $query<br>" . mysql_error());
?>

 

I added the "or die" clause for debugging purposes.

 

Ken

Archived

This topic is now archived and is closed to further replies.

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