Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8

Thread: PHP error?

  1. #1
    Regular Coder
    Join Date
    Dec 2007
    Posts
    145
    Thanks
    5
    Thanked 5 Times in 5 Posts

    PHP error?

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource


    Does ^ this mean I have to upgrade my php version?

  • #2
    Regular Coder Iszak's Avatar
    Join Date
    Jun 2007
    Location
    Perth, Western Australia
    Posts
    332
    Thanks
    2
    Thanked 58 Times in 57 Posts
    It generally means that the link resources that was put in was wrong.

    eg.

    $sql = "SELECT * FROM table LIMIT 1";
    $result = mysql_query($sql);
    $row = mysql_fetch_array($result);

    The $result could be a bad query would would be related to the SQL, or that the actual database isn't connected.

  • #3
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Quote Originally Posted by nobackseat88 View Post
    Does ^ this mean I have to upgrade my php version?
    It means you tried to use mysql_fetch_array() on a variable that wasn't a valid mysql result and/or your query failed to execute due to a sytax error. Iszak's code above shows an example of how it should be used. To see the reason why it failed change your query line to this:
    PHP Code:
    $result mysql_query($sql) or die(mysql_error()); 

  • #4
    Regular Coder
    Join Date
    Dec 2007
    Location
    Nebraska
    Posts
    113
    Thanks
    0
    Thanked 2 Times in 2 Posts
    When connecting to and using a database, you are connecting to an external application. All kinds of things can go wrong between connecting to the database and closing the connection. That includes a query failing due to a syntax error or other condition.

    Never assume that a database connection is going to succeed or that selection of a database to use is going to succeed, or that a query will execute.

    All of the MySQL functions return a value that evaluates to false if there is a problem. Use that to provide proper flow to your application. Each step along the way should be checked and control the flow of your code accordingly. Provide friendly but not technically revealing messages to the user that indicates that something went wrong.

  • #5
    Regular Coder
    Join Date
    Dec 2007
    Posts
    145
    Thanks
    5
    Thanked 5 Times in 5 Posts
    Thanks guys! Another one tho:

    Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING




    ?????

  • #6
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    You're probably missing a quote or colon on or before the line it tells you. Post the code if you can't figure it out. If you use the [php][/php] tags you'll often be able to tell just by looking at the syntax highlighting. Also, if you use an editor with syntax highlighting/error checking you can usually avoid this problem altogether.

  • #7
    Regular Coder
    Join Date
    Dec 2007
    Posts
    145
    Thanks
    5
    Thanked 5 Times in 5 Posts
    The red line indicates the line the error pointed out, I believe. Okay, code:
    Code:
    <?php
    $mysite_username = $HTTP_COOKIE_VARS["mysite_username"]; 
    $con = mysql_connect("localhost","database","password");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }mysql_select_db("database", $con);
    mysql_query("UPDATE users SET profile = '$_REQUEST['pcoding']'");
    echo "Success!";
    ?>
    Last edited by nobackseat88; 12-27-2007 at 12:37 AM.

  • #8
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Change your mysql_query line to this:
    PHP Code:
    mysql_query("UPDATE users SET profile = '{$_REQUEST['pcoding']}'"); 
    You should be using mysql_real_escape_string() or similar on all user input, too.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •