Log in

View Full Version : Clarification on putting a quote in a query string



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?

bluewalrus
04-22-2010, 09:11 PM
You use the quotes for a string. The first two examples don't care about the type. The third does because of the limit operator, it is looking for a numerical limiter and you gave it a string which it can't evaluate (I think anyway).

traq
04-23-2010, 01:11 AM
unrelated to the "quote" question, using the code you have, you will never see your query result. It will always echo your original SQL query because that's what echo $query1; tells it to do.

If you want to see your results, you need to assign the results to a variable that you can then access.

for example:

<?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'";

// this outputs "select * from tryquote where age = '$age'"
// - not what we want!
echo $query1;

// fetch the result instead and print each row
$result = mysql_query($query1);
while($row = mysql_fetch_array($result)){
print_r($row);
}

?>

bluewalrus
04-23-2010, 02:48 PM
I'd say because the sql doesn't need it to be a number for the insert, it just needs it to be any sort of value. The column actually evaluates if it is acceptable by the type (to test this theory try putting in a letter). The other though must be a value or it will fail for example try counting from "m" to 10, can't be done. This is just what I think though I know little of sql try googling around.

heavensgate15
04-23-2010, 02:51 PM
ahhh ok... I got a little grasp of the concept... I'm experimenting this concept now... tnx (bluwalrus and traq):)