SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Zealot NZ Joe's Avatar
    Join Date
    Apr 2001
    Location
    Kapiti, New Zealand
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    MySQL query not working

    I've read the manual and the books and still can't get this to work. Is it something obvious? I'm trying to get it to get the url to an image (prodimage) that is stored in a database table (productlist). I get the error warning for the line $query_data = mysql_fetch_array($result); and it does not echo any result. Any help appreciated. Thanks. Here's the query:

    mysql_connect($dbhost, $dbuser, $dbpass);
    $query = "select * from productlist where product = 'tights'";
    $result = mysql_query($query);
    if(!result) error_message(sql_error());
    $query_data = mysql_fetch_array($result);
    $prodimage = $query_data["prodimage"];
    echo $prodimage;
    Gravity always wins

  2. #2
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Two things, what is the error message you are getting, and shouldn't your if statement have some braces around it, can't remember if they don't need them when inline or not, so:

    mysql_connect($dbhost, $dbuser, $dbpass);
    $query = "select * from productlist where product = 'tights'";
    $result = mysql_query($query);
    if(!result) {
    error_message(sql_error());
    exit();
    }
    $query_data = mysql_fetch_array($result);
    $prodimage = $query_data["prodimage"];
    echo $prodimage;
    Last edited by timnz; Jun 30, 2001 at 21:23.

  3. #3
    midnight coder
    Join Date
    Dec 2000
    Location
    The flat edge of the world
    Posts
    838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there such a function call error_message()?

    Try this and see if you can catch the error:

    PHP Code:
    mysql_connect($dbhost$dbuser$dbpass); 

    $query mysql_query("select * from productlist where product = 'tights'") or die (mysql_error()); 

    $query_data mysql_fetch_array($query); 
    $prodimage $query_data["prodimage"]; 
    echo 
    $prodimage

  4. #4
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There might be if NZ Joe has already defined it Robo. But mysql_error() is certainly viable.

  5. #5
    SitePoint Zealot NZ Joe's Avatar
    Join Date
    Apr 2001
    Location
    Kapiti, New Zealand
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've modified the code as follows:
    mysql_connect($dbhost, $dbuser, $dbpass) or die ("Could not Connect");
    mysql_select_db (testss);
    $query = mysql_query("select * from productlist where product = 'tights'");
    $result = mysql_query($query) or die ("Query Failed");
    if(!$result) {
    echo "error";
    }
    $query_data = mysql_fetch_array($result);
    $productimage = $query_data["prodimage"];
    echo $productimage;

    This returns 'Query Failed'. I don't understand why my query fails as it works when typed directly into mysql as it appears above
    Gravity always wins

  6. #6
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by NZ Joe
    This returns 'Query Failed'. I don't understand why my query fails as it works when typed directly into mysql as it appears above
    it's failing cuz you have:

    PHP Code:
    $query mysql_query("select * from productlist where product = 'tights'"); 
    $result mysql_query($query) or die ("Query Failed"); 
    in the first statement you're DOING the query and assigning the result id to $query and then in the second statement you're trying to run a query with the result id from the first query! you need to change it to this and it'll work:

    PHP Code:
    $query "select * from productlist where product = 'tights'"
    $result mysql_query($query) or die ("Query Failed"); 

  7. #7
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    mysql_select_db (testss);
    That should have caused an error(?). You should use quotes around your string:
    mysql_select_db ("testss");

    Anyway, there are other problems with your code. This is the way I would write it:
    PHP Code:
    mysql_connect($dbhost$dbuser$dbpass) or die ("Could not Connect"); 
    mysql_select_db ("testss"); 
    $query "select * from productlist where product = 'tights'";
    $result mysql_query($query) or die ("Query Failed"); 

    if(!
    $result) { 
       echo 
    "error";


    $query_data mysql_fetch_array($result); 
    $productimage $query_data["prodimage"]; 
    echo 
    $productimage

  8. #8
    SitePoint Zealot NZ Joe's Avatar
    Join Date
    Apr 2001
    Location
    Kapiti, New Zealand
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks everyone. works perfect now. I'll put that one down as a good lesson learnt and hopefully not screw it up so badly next time.
    Gravity always wins

  9. #9
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Side note sid, you don't need the quotes around the db name.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  10. #10
    SitePoint Evangelist =X='s Avatar
    Join Date
    Jun 2001
    Posts
    487
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    and
    if(!$result)*{
    ***echo*"error";
    }

    can be written as

    if(!$result)*echo*"error";

  11. #11
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by freddydoesphp
    Side note sid, you don't need the quotes around the db name.
    Thanks for the clarification. However, developing such sloppy coding habits is not going to assist a person when they go to use other languages.

  12. #12
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It amazes me you haven't been flamed for bringing up coding-standards. Seems to be a hot flammable topic these days. Yes PHP is very forgiving like that, whether that is good or bad, I suppose which side of the fence you stand on. I agree, that one should pay attention to detail, however, my intention wasn't to say don't use quotes more to say, if there was a problem with why his code dodn't work, it wasn't that, in the realm of PHP.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.


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
  •