-1

The code is mean't to connect to my database. It did work but recently stopped working, I've tried reading the error code shown below but I can't seem to fix it. Here is the unworking code:

<?php
    mysql_connect("127.0.0.1", "goaerox_lkdb1", "~censored~"); //your parameters here
    mysql_select_db("goaerox_lkdb1"); //you DB name here

    $q = "SELECT id, channel, email, paypal, network FROM partners;";
    $result = mysql_query($q);

    echo "<table>";
    echo "<tr><td>id</td><td>Channel</td><td>Email</td><td>PayPal</td><td>Network </td><td>Actions</td></tr>";
    while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
        echo "<tr>";
        echo "<td>".$row[0]."</td>";
        echo "<td>".$row[1]."</td>";
        echo "<td>".$row[2]."</td>";
        echo "<td>".$row[3]."</td>";
        echo "<td>".$row[4]."</td>";
        echo "</tr>";
    }
    echo "</table>";

    mysql_free_result($result);

    ?>

Now you have seen that, you should have a better understanding on what it is meant to do. This is the current error I am getting:

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in public_html/test.php on line 10
id  Channel Email   PayPal  Network Actions
Warning: mysql_free_result() expects parameter 1 to be resource, boolean given in public_html/test.php on line 21

If anyone could please help me this will be much appreciated.

GoAerox
  • 47
  • 7

4 Answers4

1

Please note that mysql_ functions are deprecated. Aside from that, your problem can be explained via the following sentence from the documentation:

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

What is happening is that your SELECT statement is not successfully retrieving database results, so you are getting FALSE as the return value, which is a boolean value. Subsequently, your mysql_fetch_array() function is failing because the first parameter you are passing is not a resource, as expected, but the boolean value FALSE.

Alex W
  • 35,267
  • 10
  • 97
  • 106
  • I looked at the documentation, looked at everything you provided. I am still struggling to fix this. – GoAerox Apr 09 '14 at 05:06
0

First, there are plenty of similar questions here. Please search through these before asking a question.

Second, have a look here: http://au1.php.net/function.mysql-connect It's worth noting that mysql_connect will soon be phased out, and there are alternatives such as mysqli.

To get the error that occurred, use myqsl_error as detailed in the link I provided

FamiliarPie
  • 596
  • 3
  • 9
0

Result becomes false when the query couldn't produce a resultset. Just try executing the same query in phpmyadmin and check if it gives some error. If it gives some error then this case is quite certain

This is quite a generic problem. You should first consult the documentation for such things. You would definitely get a clear insight if u had already gone through the documentation.

Rajesh Paul
  • 6,364
  • 6
  • 37
  • 53
0

Is there any data in the database? A query will return false if there's an error or 0 results returned.

Read what @Alex W said and what @Mike posted.

$conn = mysql_connect($host, $username, $password);
$link = mysql_select_db($dbname, $conn);

// False is returned on error, otherwise a resource is returned and this will be true
if (!$link) {
    die('Could not connect to database');
}

$result = mysql_query('SELECT id, channel, email, paypal, network FROM partners', $link);

// If query error or no results
if (!$result) {
    die('Could not fetch query results');
}

If you get an error at any stage you need to

  1. Test the database connection
  2. Test that the query works
  3. Check that there's data in the database.

Also see

Always read the manuals

Justin Mitchell
  • 329
  • 1
  • 5