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 5 of 5
  1. #1
    New Coder
    Join Date
    Jul 2011
    Location
    Pennsylvania
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Error while trying to display contents of database with PHP

    I've been searching for answers to this problem for a little while now to no avail. Im hesitant to post a uqestion that may have been answered, but here it goes.

    I'm learning how to use MySQL and PHP. I have created the database, but when I try to display it I get the following error.

    Code:
    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/bail/public_html/bailfiles/index.php on line 15
    I have edited the syntax over and over and over and over but I either get a new error until I reverse the tweak, or I get the same message.

    Here is the database information
    Code:
    DatabaseName: bail_bail
    TableName: bailfiles
    contains the following data fields:
    id = 4 digit integer, auto-increment
    fname = 30 digits varchar
    lname = 30 digit varchar
    phon = 12 digit varchar
    Here is the code in my index.php file
    Code:
    <html>
    <head>
    <title>
    Smith Bail Files Database by Justin
    </title>
    </head>
    <body bgcolor="000000" text="00ff00" link="00ff00" vlink="00ff00">
    
    
    <?php
    
    mysql_connect ("localhost", "DATABASEUSERNAME", "DATABASEPASSWORD") or die ('Could not connect to the MySQL Database because : ' . mysql_error());
    mysql_select_db ("bail_bail ");
    $query = mysql_query ("SELECT * FROM bailfiles ");
    while ($row = mysql_fetch_array(query))
    
    {
    echo "<br />ID: ".$row[id];
    echo "<br />First Name: ".$row[fname];
    echo "<br />Last Name: ".$row[lname];
    echo "<br />Phone: ".$row[phon];
    }
    
    ?>
    
    
    </body>
    </html>
    I have verified numerous times I am using the proper variable names and database information. I am completely stuck. I wouldn't be asking for help if I thought I could hack it until I figured it out, but Im getting burnt on the topic.

    Any ideas?

  • #2
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Just a little typo, you have written query instead of $query inside the mysql_fetch_array() function Everything else looks alright, although you might want to encase your array keys in the echo statements in quotation marks.

  • #3
    New Coder
    Join Date
    Jul 2011
    Location
    Pennsylvania
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Unfortunately that did not solve my problem. Same error message. I'm wondering if I should use another method of displaying the data.

  • #4
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    After looking at your code again, I noticed you had nothing to catch mysql errors in the query. Use this instead:
    PHP Code:
    mysql_select_db ("bail_bail") or die(mysql_error());
    $query mysql_query ("SELECT * FROM bailfiles") or die(mysql_error());
    while (
    $row mysql_fetch_array($query)) 
    I suspect your DB isn't connecting or there's no table called bailfiles, or no database called bail_bail. I also noticed that there was a space after bail_bail and the quotation mark which could have been causing the problem and again, you weren't catching errors there.

    It's important to account for every single error to ensure that you not only know what's going on on your code, but you prevent potential bugs that create security risks and various other unexpected results.

  • #5
    New Coder
    Join Date
    Jul 2011
    Location
    Pennsylvania
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    your corrected code worked like a charm. You are the man, thank you very much.


  •  

    Posting Permissions

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