Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2004

    Unanswered: variables in sql queries: a very easy question


    these are two examples of a book ive been reading:

    $query = "SELECT * FROM region WHERE " . "region_id = $regionID";


    $insertQuery = "INSERT INTO region VALUES (NULL, " . "\"" . $regionName . "\")";

    ive got a little confusion nibbling at me - in the first example the PHP variable is included in the query string as is (im assuming the string concats exist because of the space limitations on the printed page of the book?)... yet in the second its placed outside the query string. the only difference i can tell is that one is a numeric value and the other a string - is this the difference? could someone please briefly explain the rules to me? it would be much appreciated as my text is proving a little ambiguous.



  2. #2
    Join Date
    Feb 2004
    when inserting a string or char() values into a querie the text must be surrounded with single quote. When inserting a integer this is not needed.

    In the first query the variable is a int so the variable is replaced by the int values. If you would do the same for a string you will get the following query

    $query = "SELECT * FROM region WHERE " . "region_id = '$regionID'";

    which causes the $regionID to be a constant string "$regionID" and not the value of $regionID

Posting Permissions

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