質問

My problem is when I want to return all rows in a table I get this error:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in
/home/a2943462/public_html/index.php on line 20

I also tried array, assoc, row but none of them works, this is my code:

<?php
$con=mysql_connect('mysql11.000webhost.com', 'db_username', 'db_password','Table');
// Check connection
if (!$con) {
  echo "Failed to connect to MySQL: " . mysql_connect_error();
}

$result = mysql_query("SELECT * FROM PageInfo",$con);

    echo $result;
    echo $row['ID'];
    echo "<table border='1'>
    <tr>
        <th>ID</th>
        <th>Title</th>
        <th>Video</th>
        <th>Posted</th>
    </tr>";

while ($row = mysql_fetch_array($result)) {   // Line 20
  echo "<tr>";
  echo "<td>".$row["ID"]."</td>";
  echo "<td>".$row["Title"]."</td>";
  echo "<td>".$row["Video"]."</td>";
  echo "<td>".$row["Posted"]."</td>";
  echo "</tr>";
}
echo "</table>";

mysql_free_result($result);

mysql_close($con);
?>

heres the website so you can see whats the error is

http://wirechat.net16.net/

役に立ちましたか?

解決 2

you have syntax error to connect database

$con=mysql_connect('mysql11.000webhost.com', 'db_username', 'db_password','Table');

change to 

$con=mysql_connect('mysql11.000webhost.com', 'db_username', 'db_password','databasename');

他のヒント

I can see a couple of errors with your example script, hopefully this will explain them.

$con=mysql_connect('mysql11.000webhost.com', 'db_username', 'db_password','Table');

Should be:

$con=mysql_connect('mysql11.000webhost.com', 'db_username', 'db_password');

As per http://uk1.php.net/mysql_connect the fourth parameter should be a link connector, not the table select as you've specified.

You also need to specify the database that the table you're getting the data from is in, this can be done by running after you've done the 'mysql_connect'

$db = mysql_select_db('database_name', $con);

I notice you're also attempting to access the '$row' parameter before it's assigned (line 11), i.e.

echo $row['ID'];

Whereas you only assign it on line 20 in the while loop.

Also, once you've run:

$result = mysql_query("SELECT * FROM PageInfo",$con);

Then you'll want to check the value of result before attempting to access it, according to http://uk1.php.net/manual/en/function.mysql-query.php the result of 'mysql_query' can be boolean false if there is a problem with the query you've entered.

This can be because the user you've connected with doesn't have permissions to the table/db you've specified, what's more likely is the error has occurred because you missed the 'mysql_select_db'.

Either way you're best of doing a check on $result and exiting/throwing an exception if there's a problem i.e.

if ($result === false)
{
    echo "Error with result variable";
    exit();
}

Hopefully that should sort most of the problems with your script anyway, and with any luck go some way to explaining why the problems were there

The $con should be the first parameter in

$result = mysql_query("SELECT * FROM PageInfo",$con);

Try like,

$result = mysql_query($con,"SELECT * FROM PageInfo");

Try to add this

$result = mysql_query("SELECT * FROM PageInfo",$con) or die(mysql_error());

and find out the exact cause.

You have not selected the database, also i don't believe you should be setting a fourth param to mysql_connect for "Table". Try this instead, making sure to replace "database_name" with your actual database name.

$con=mysql_connect('mysql11.000webhost.com', 'db_username', 'db_password');
$db = mysql_select_db('database_name');

$result = mysql_query("SELECT * FROM PageInfo");
var_dump(  mysql_fetch_array($result)  );
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top