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 9 of 9
  1. #1
    New Coder
    Join Date
    Sep 2005
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts

    mysql_fetch_array Problem

    When I run this script It says Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in blah blah....
    PHP Code:
    <?
    session_start
    ();
    include 
    "db.php";
    $username $_SESSION['username'];
    $result mysql_query("SELECT * FROM users WHERE to='$username'");
    while (
    $row mysql_fetch_array($result)) { ?>

    From: <? $row['from'?><br>
    Subject: <? $row['subject'?><br>
    Message: <? $row['message'?><br>
    <? ?>

  • #2
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Your query is failing, so it's returning FALSE. Read more about mysql_query() return values here and how to get the database's error message with the mysql_error() function here.

  • #3
    Regular Coder
    Join Date
    Jan 2006
    Location
    Preston, Lancashire, England
    Posts
    285
    Thanks
    0
    Thanked 0 Times in 0 Posts
    to help us find the problem could you do this.
    PHP Code:
    $result mysql_query("SELECT * FROM users WHERE to='$username'") or die(mysql_error()); 
    and post the results.

    the mysql_error will show you if you are getting an error from the query.

    you could even try this
    PHP Code:
    $query "SELECT * FROM users WHERE to='$username'";
    print 
    $query;
    $result mysql_query($query) or die(mysql_error()); 
    this will then let us see if $username is being set.

    Cheers

    MRMAN

  • #4
    New Coder
    Join Date
    Sep 2005
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts
    When I try the below it I get 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'to=''' at line 1'
    PHP Code:
    $result mysql_query("SELECT * FROM users WHERE to='$username'") or die(mysql_error()); 

  • #5
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    TO is a reserved word in MySQL. You can continue to use it as a column name, but you'll need to enclose it in backticks (`to`='$username') when you refer to it. I would recommend renaming it, though.

  • #6
    New Coder
    Join Date
    Sep 2005
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Now it says Unknown column 'to' in 'where clause'

  • #7
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Are you sure there's a column called 'to' in the users table?

  • #8
    New Coder
    Join Date
    Sep 2005
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Is column the same thing as a field? If so there is one called that.

  • #9
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Please run a SHOW CREATE TABLE statement on your users table and post the result, along with the exact query you're sending.


  •  

    Posting Permissions

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