Closed Thread
Results 1 to 2 of 2

Thread: Problem pulling data from a database table using PHP

  1. #1
    kiacovin71 is offline x10Hosting Member kiacovin71 is an unknown quantity at this point
    Join Date
    Apr 2011
    Posts
    1

    Problem pulling data from a database table using PHP

    this code:


    <html>
    <head>
    <title>php</title>
    </head>
    <body>
    <h1>Show Database </h1>
    <?
    //make the database connection
    $conn = mysql_connect("localhost", "clocky_time", "********");
    mysql_select_db("clocky_clock", $conn);

    //create a query
    $sql = "SELECT * FROM clock_clock";
    $result = mysql_query($sql, $conn);

    print "<table border = 1>\n";

    //get field names
    print "<tr>\n";
    while ($field = mysql_fetch_field($result)){
    print " <th>$field->name</th>\n";
    } // end while
    print "</tr>\n\n";

    //get row data as an associative array
    while ($row = mysql_fetch_assoc($result)){
    print "<tr>\n";
    //look at each field
    foreach ($row as $col=>$val){
    print " <td>$val</td>\n";
    } // end foreach
    print "</tr>\n\n";
    }// end while

    print "</table>\n";
    ?>
    </body>
    </html>

    which I changed from some code that works on my friends godaddy account is giving me the error:

    "Show Database


    Warning: mysql_fetch_field(): supplied argument is not a valid MySQL result resource in /home/clocky/public_html/showalldata.php on line 20

    Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/clocky/public_html/showalldata.php on line 26
    "

    Any ideas? Besides that I really like x10hosting
    Last edited by descalzo; 04-17-2011 at 11:18 AM.

  2. #2
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,635

    Re: Problem pulling data from a database table using PHP

    Warning: mysql_fetch_field(): supplied argument is not a valid MySQL result resource in /home/clocky/public_html/showalldata.php on line 20
    Look at what it is telling you. Count lines or use the Code Editor in cPanel or note the place you call mysql_fetch_field is

    Code:
    while ($field = mysql_fetch_field($result))
    It is saying that $result is not a valid result. Where did it come from?

    Code:
    $result = mysql_query($sql, $conn);
    mysql_query returns a result object on SELECT queries (even if there are no matches), unless there is an error. Hence you have an error.

    To display the problem, always test the return value of the query.

    Code:
    $result = mysql_query($sql, $conn);
    if (!$result) {
        echo('MySQL error: ' . mysql_error());
    }
    Nothing is always absolutely so.

Closed Thread

Similar Threads

  1. Replies: 4
    Last Post: 03-14-2010, 09:20 AM
  2. HTML Table from SQL Data
    By azamkandy in forum Programming Help
    Replies: 4
    Last Post: 10-13-2009, 01:39 PM
  3. Suggestions for best way to build a MySQL table from raw data?
    By ParallelLogic in forum Programming Help
    Replies: 5
    Last Post: 07-05-2009, 02:39 AM
  4. Database table gone
    By uptime2 in forum Free Hosting
    Replies: 5
    Last Post: 11-27-2007, 12:39 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