SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    variable in where clause

    Code:
    $query="select id from myTable1 where title='title1' " ;
    The code above works fine.

    when I change "where clause" from the code above like the code below, I met
    Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in dbAction1.php .

    Code:
    $query="select id from myTable1 where title=$myVar " ;
    How can I put a variale in where clause without Warning?

  2. #2
    SitePoint Zealot zalucius's Avatar
    Join Date
    Jul 2007
    Location
    Denmark
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    $query="select id from myTable1 where title='$myVar' " ;
    It's always a good idea to echo a query, to see what it looks like.
    You had removed the apostophes around the value.
    zalucius

  3. #3
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, some quick and simple debugging.

    add the red code and that will display the actual sql query that will be run and hopefully the error in your line of code will become obvious.

    Code:
     
    $query="select id from myTable1 where title=$myVar " ;
     
    echo $query;
    
    die();
    if you still have a problem, post back the output you get from

    Code:
     
    echo $query;

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Assuming you're using mysql_*, remember to always escape any dynamic elements of your query.
    PHP Code:
    <?php
    $query 
    sprintf(
      
    "SELECT id FROM table WHERE name = '%s';",
      
    mysql_real_escape_string($var)
    );
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  5. #5
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by zalucius View Post
    Code:
    $query="select id from myTable1 where title='$myVar' " ;
    Thank you, it works fine.

    What about session variable in where clause?
    The following code seems not to work correctly.
    Code:
    code1
    $query="select id from myTable1 where title='$_SESSION("$myVar")' " ;
    
    result1
    Parse error: syntax error, unexpected T_VARIABLE 
    
    code2
    $query="select id from myTable1 where title='$_SESSION('$myVar')' " ;
    
    result2
    // It doesn't call the value of the id column.

  6. #6
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Maybe here or here would be better suited for you, a quick brush up on a few fundamentals is never wasted time.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  7. #7
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kalon View Post
    echo $query;
    Thank you, it's good tools for confirmation.

    I'll check the code above with your code "echo $query."


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
  •