Results 1 to 5 of 5

Thread: Clarification on putting a quote in a query string

  1. #1
    Join Date
    May 2009
    Posts
    62
    Thanks
    19
    Thanked 3 Times in 3 Posts

    Default Clarification on putting a quote in a query string

    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 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 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;

    ?>
    Still, it echoes out the value of $query1 and $query2...


    Another example using the select statement with the limit statement:
    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;
         
    ?>
    In this example, it only echoes the $query1.... why does the execution of $query2 string failed when the previous 2 example didn't fail?

  2. #2
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    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).
    Corrections to my coding/thoughts welcome.

  3. #3
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

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

      
    // 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);
      }

    ?>

  4. #4
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    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.
    Corrections to my coding/thoughts welcome.

  5. The Following User Says Thank You to bluewalrus For This Useful Post:

    heavensgate15 (04-23-2010)

  6. #5
    Join Date
    May 2009
    Posts
    62
    Thanks
    19
    Thanked 3 Times in 3 Posts

    Default

    ahhh ok... I got a little grasp of the concept... I'm experimenting this concept now... tnx (bluwalrus and traq)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •