+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: PHP mysql_query failure

  1. #1
    lostcommander's Avatar
    lostcommander is offline x10Hosting Member lostcommander is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    United States of America
    Posts
    52

    Red face PHP mysql_query failure

    Okay, I've been beating my head against this for too long and I don't see anything either wrong with my code or of use in searching the forums. I'm sorry as I'm sure this is either a typo or programmer error (e.g. mixing string concatenation . vs + earlier - wrong language, dur...) on my part.

    $query = "SELECT * FROM table";
    $dbconn = mysql_connect(localhost,$user,$password);
    if (!$dbconn) {
    die('DB connection failed: ' . mysql_error());
    }
    $selection = @mysql_select_db($database);
    if (!$selection) {
    die('DB selection failed: ' . mysql_error());
    }
    $result = mysql_query($query);
    if (!$result) {
    die('Query failed: ' . mysql_error());
    }
    When I do this, I get "Query failed"; however, there is no mysql error attached. The $database, $user, and $password are all correct, and correctly capitalized. :dunno:
    Last edited by lostcommander; 06-01-2009 at 11:56 AM. Reason: Put a box on code.

  2. #2
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member garrettroyce is a glorious beacon of lightgarrettroyce is a glorious beacon of light
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,746

    Re: PHP mysql_query failure

    localhost needs to be in quotes: 'localhost'

    Is your table named 'table" ? This is probably a problem. You're using a reserved word for a table name. You can probably get around it by quoting 'table' but table is a bad name regardless. It's not descriptive of what's in the table and it's already defined as something else.
    gjr.gr - coming soon: secrets of OCD coding from a self taught tinkerer

  3. #3
    lostcommander's Avatar
    lostcommander is offline x10Hosting Member lostcommander is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    United States of America
    Posts
    52

    Re: PHP mysql_query failure

    Nope, my table is not "table". My user and password aren't named after themselves either. I know those weren't the trouble though.

    Now, putting localhost in quotes. I mean, it makes sense in hindsight, but I never considered it, lol... *sigh* unfortunately, that does not seem to have fixed it. I am still getting that third error.

  4. #4
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member garrettroyce is a glorious beacon of lightgarrettroyce is a glorious beacon of light
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,746

    Re: PHP mysql_query failure

    I'm sure you thought of this, but just to make sure, does "table" exist in your database? Does "user" have select permissions on "table"? Is there any data in "table"?
    gjr.gr - coming soon: secrets of OCD coding from a self taught tinkerer

  5. #5
    lostcommander's Avatar
    lostcommander is offline x10Hosting Member lostcommander is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    United States of America
    Posts
    52

    Re: PHP mysql_query failure

    This is TOTALLY the time to start asking me "is your computer plugged in?" questions, because I think it's pretty obvious I've forgotten something pretty basic...

    Also, the last time I did this kind of stuff was about 3 years ago in PHP4, so if there were any major changes relevant to this particular section of code, that might be an issue. I've been reading up on PHP5 and have tried to reread the PHP5 manual section for each function I'm calling so as to make sure I find any crucial differences, but I might have missed something.

    Using PHPMyAdmin from the cPanel page, I have verified that 'table' does indeed exist in my database and that it has 2 rows of data in it. The user, at present, is the original superuser with all privileges. For security I'll change that later to read-only, but for now I wanted to make debugging easier.

  6. #6
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member garrettroyce is a glorious beacon of lightgarrettroyce is a glorious beacon of light
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,746

    Re: PHP mysql_query failure

    Just to be sure, try doing this:
    Code:
    if($result === false) {
    ....
    I don't think resources should ever evaluate to "false" but I know that strings will evaluate to either true or false based on their contents, so maybe this is a similar type of situation?
    Edit:
    Try this too, put an actual error into your query and see if that at least has output.
    Last edited by garrettroyce; 06-01-2009 at 11:50 AM. Reason: Automerged Doublepost
    gjr.gr - coming soon: secrets of OCD coding from a self taught tinkerer

  7. #7
    lostcommander's Avatar
    lostcommander is offline x10Hosting Member lostcommander is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    United States of America
    Posts
    52

    Re: PHP mysql_query failure

    Okay, if I use:
    if($result === false) {
    die('Query failed: ' . mysql_error());
    }
    then the die statement does NOT get triggered and the page loads (albeit without the data it should have from MySQL).

    Also, if I add:
    echo($result);
    echo(mysql_fetch_row($result));
    then it produces: "Resource id #5" "Array"

    BTW, thank you VERY much for your help!
    Last edited by lostcommander; 06-01-2009 at 12:01 PM. Reason: Put a box on code.

  8. #8
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member garrettroyce is a glorious beacon of lightgarrettroyce is a glorious beacon of light
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,746

    Re: PHP mysql_query failure

    Awesome
    Last edited by garrettroyce; 06-01-2009 at 11:56 AM.
    gjr.gr - coming soon: secrets of OCD coding from a self taught tinkerer

  9. #9
    lostcommander's Avatar
    lostcommander is offline x10Hosting Member lostcommander is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    United States of America
    Posts
    52

    Thumbs up Re: PHP mysql_query failure

    And as soon as I took out some extra /* */ blocks, I get the data that IS supposed to be there! Spectacular, thank you very much!

  10. #10
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member garrettroyce is a glorious beacon of lightgarrettroyce is a glorious beacon of light
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,746

    Re: PHP mysql_query failure

    You're welcome
    gjr.gr - coming soon: secrets of OCD coding from a self taught tinkerer

+ Reply to Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. Ever Been Suspended For Using PHP?
    By dragoneye_xp in forum Off Topic
    Replies: 26
    Last Post: 08-16-2009, 07:17 PM
  2. currently have an application pending php
    By biomasti in forum Free Hosting
    Replies: 1
    Last Post: 09-03-2008, 01:58 PM
  3. Important PHP Information
    By Bryon in forum News and Announcements
    Replies: 0
    Last Post: 11-21-2007, 02:08 PM
  4. PHP Easter Eggs
    By dragoneye_xp in forum Off Topic
    Replies: 3
    Last Post: 06-14-2006, 05:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers