Variable in SQL query
Posted: Mon Jan 11, 2010 3:33 pm
Hello,
I'm building my first PHP site and have run into the following problem. I would like to place a variable within an SQL Request statement but am not certain that I am doing it correctly. The following is the code I was attempting to use.
<?
$sql = "SELECT * FROM `Catalog` WHERE title = '$article['alsolike']'";
$result = mysql_query($sql);
while ( $article = mysql_fetch_array($result) ){
echo '<li class="box"';
echo '>';
echo "<a href=\"detail_pop-up.php?id=" . $article['id']. "\">";
echo '<img src="images/medium/';
echo $article['image'];
echo '.jpg" align="middle" /><br/>';
echo $article['title'];
echo "</li>"; }
?>
In this instance $article['alsolike'] is "Boston". So if it came out as...
$sql = "SELECT * FROM `Catalog` WHERE title = 'Boston'";
the information following it would echo correctly.
Any advice on how to do this correctly would be appreciated.
Thanks!
I'm building my first PHP site and have run into the following problem. I would like to place a variable within an SQL Request statement but am not certain that I am doing it correctly. The following is the code I was attempting to use.
<?
$sql = "SELECT * FROM `Catalog` WHERE title = '$article['alsolike']'";
$result = mysql_query($sql);
while ( $article = mysql_fetch_array($result) ){
echo '<li class="box"';
echo '>';
echo "<a href=\"detail_pop-up.php?id=" . $article['id']. "\">";
echo '<img src="images/medium/';
echo $article['image'];
echo '.jpg" align="middle" /><br/>';
echo $article['title'];
echo "</li>"; }
?>
In this instance $article['alsolike'] is "Boston". So if it came out as...
$sql = "SELECT * FROM `Catalog` WHERE title = 'Boston'";
the information following it would echo correctly.
Any advice on how to do this correctly would be appreciated.
Thanks!