SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2002
    Location
    Nevada USA
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Any Good Reference

    Hi

    I'm a PHP brand newbie. I'm feeling my way thru the language, but I'm having a hard time with statement structure. For example:

    The statement
    $sql="select * from tablename where cat1 = '".$thing2search."'";

    works fine. But I don't understand the leading and trailing dots around the second variable. I also don't get why the single enclose the double quotes around the variable.

    also
    $sql="select * from tablename where(cat1 || cat2 || cat3) = '".$thing2search."'";

    Does not work. I don't see why not.

    Does anyone have a good reference site that explains these structures.

    Thanks
    Thatlouguy says "This is a dark ride"

  2. #2
    Your Lord and Master, Foamy gold trophy Hierophant's Avatar
    Join Date
    Aug 1999
    Location
    Lancaster, Ca. USA
    Posts
    12,305
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Well you have two different questions. The first relates to PHP and its operands. The second relates to forming a valid SQL query.

    When you use PHP to access a database (say MySQL for example) you are actually using two different langauges in one.

    Let's look at your questions.

    The statement
    $sql="select * from tablename where cat1 = '".$thing2search."'";

    works fine. But I don't understand the leading and trailing dots around the second variable. I also don't get why the single enclose the double quotes around the variable.
    In PHP you can use a period (.) to concatenate strings. This is what the above statement is doing. The thing with the quotes is that most SQL databases require strings to be enclosed in single quotes ('). This is where the languages overlap.

    The database only will see:
    select * from tablename where cat1='value'

    This is the result of the string built up with PHP.

    $sql="select * from tablename where(cat1 || cat2 || cat3) = '".$thing2search."'";

    Does not work. I don't see why not.
    This doesn't work because you cannot compare three fields to one value which is basically what you are trying to do.

    For the above query to work, you should write it like:
    $sql="select * from tablename where cat1 = '".$thing2search."' or cat2 = '".$thing2search."' or cat3 = '".$thing2search."'";

    If you need a reference you should read the articles located at www.webmasterbase.com. You can also look into buying Kevin Yank's Book on "Building a Database Driven Site". You can find the book at www.sitepoint.com/books/
    Wayne Luke
    ------------


  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2002
    Location
    Nevada USA
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. It looks like I have some homework to do. I'll try what you suggested. Thanks for the links.
    Thatlouguy says "This is a dark ride"


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
  •