When do I put a quote in a variable if I am to include that variable in a query string?
Here is a sample code:
It echoes out the value of $query1 and $query2...PHP Code:<?php
$con = mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("quote",$con) or die(mysql_error());
$age = 30;
$query1 = "insert into tryquote values('$age')";
$query2 = "insert into tryquote values($age)";
if(mysql_query($query1))
echo $query1;
if(mysql_query($query2))
echo $query2;
?>
Another example, using the select statement in MySql:
Still, it echoes out the value of $query1 and $query2...PHP Code:<?php
$con = mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("quote",$con) or die(mysql_error());
$age = 30;
$query1 = "select * from tryquote where age = '$age'";
$query2 = "select * from tryquote where age = $age";
if(mysql_query($query1))
echo $query1;
if(mysql_query($query2))
echo $query2;
?>
Another example using the select statement with the limit statement:
In this example, it only echoes the $query1.... why does the execution of $query2 string failed when the previous 2 example didn't fail?PHP Code:<?php
$con = mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("quote",$con) or die(mysql_error());
$start = 0;
$query1 = "select * from tryquote limit $start,3";
$query2 = "select * from tryquote limit '$start',3";
if(mysql_query($query1))
echo $query1;
if(mysql_query($query2))
echo $query2;
?>



Reply With Quote


Bookmarks