SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Apr 2009
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question mysql_query() string?

    hi,

    I am a beginner site builder , I am reading Build Your Own Database Driven Website Using PHP & MySQL 3rd Edition by Kevin Yark.

    I have an obstacle, and if you guys look at this code really quick and give me some answers, I safe time and would be really appreciated.

    PHP Code:
    $jokelist = @mysql_query('SELECT joketext 
            FROM joke, author WHERE name=$name AND authorid=author.id'
    ); 
    this code does't read the variable "$name" how can I make it read it?
    it gives this error:" Unknown column '$name' in 'where clause' "
    it works if write the name manually.

    solution:
    Quote Originally Posted by oddz View Post
    PHP Code:
    $jokelist = @mysql_query("SELECT joketext 
            FROM joke, author WHERE name='
    $name' AND authorid=author.id"); 
    Use double quotes not single quotes when you like to embed variables and have them evaluated in a string. If name is a string then you must also quote that.

    Thanks
    Last edited by MertTheGreat; May 12, 2009 at 15:23.

  2. #2
    SitePoint Wizard lorenw's Avatar
    Join Date
    Feb 2005
    Location
    was rainy Oregon now sunny Florida
    Posts
    1,101
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I think you are missing something here, What are you expecting that to do?

    It is just asking the database to get information where name=Tom (That is if $name='Tom';

    With this in mind you need to set $name to someones name.

    Then you have to run the query to get your results using a fetch, you will see where fetch is used in the book and will be run in a while loop.
    What I lack in acuracy I make up for in misteaks

  3. #3
    SitePoint Addict joaquin_win's Avatar
    Join Date
    Jul 2005
    Location
    Venezuela
    Posts
    224
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use double quotes instead of single quotes:
    PHP Code:
    $jokelist = @mysql_query("SELECT joketext
        FROM joke, author WHERE name=
    $name AND authorid=author.id"); 

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,151
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    PHP Code:
    $jokelist = @mysql_query("SELECT joketext 
            FROM joke, author WHERE name='
    $name' AND authorid=author.id"); 
    Use double quotes not single quotes when you like to embed variables and have them evaluated in a string. If name is a string then you must also quote that.

  5. #5
    SitePoint Member
    Join Date
    Apr 2009
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks everyone it works sorry to bother

  6. #6
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    At some point the error suppression (@) will cause you problems. If the query fails it will do so silently and you'll have a hard time figuring out what happened.

    PHP Code:
    if(!$jokelist mysql_query("SELECT ....")) {
         echo 
    "Couldn't get jokes just now";
         
    //log mysql_error() for you to review

    mikehealy.com.au
    diigital.com art, design . Latest WorkSaturday Morning


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
  •