heavensgate15
04-22-2010, 02:13 PM
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:
<?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;
?>
It echoes out the value of $query1 and $query2...
Another example, using the select statement in MySql:
<?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;
?>
Still, it echoes out the value of $query1 and $query2...
Another example using the select statement with the limit statement:
<?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;
?>
In this example, it only echoes the $query1.... why does the execution of $query2 string failed when the previous 2 example didn't fail?
Here is a sample 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;
?>
It echoes out the value of $query1 and $query2...
Another example, using the select statement in MySql:
<?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;
?>
Still, it echoes out the value of $query1 and $query2...
Another example using the select statement with the limit statement:
<?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;
?>
In this example, it only echoes the $query1.... why does the execution of $query2 string failed when the previous 2 example didn't fail?