Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

HOw to use a numeric value PHP variable in MySQL Query

$
0
0
Hello,
I am having trouble getting a query to execute correctly.

I am passing a numeric variable from a previous page using POST. The variable contains the number 3. Here is my code.

// Select the specific database
@mysql_select_db("soundrm_Website", $mySQL) or die("Unable to connect to database");

$Variable = $_POST['SpeakerMenu'];

$sql = "Select * from Sermons where Speaker_ID =$Variable";

//echo("<P>" . $sql . "</P>");

// execute query
$Speaker = mysql_query($sql);
if (!$Speaker)
{
	echo("<P>Error performing query: " .
		mysql_error() . "</P>");
	exit();
}
while ($row = mysql_fetch_array($Speaker))
{
	// Set Value
	echo("<P>" . $row["Title"] . "</P>"); 
}



When using Echo to check the Query I get this result: Select * from Sermons where Speaker_ID = 3

The query works if I replace the variable with 3 also.

When I execute with variable I get the following error: Error performing query: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '

3
' at line 2

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>