Jump to content

mysql_fetch_array error


matthew9090

Recommended Posts

Help i don't under stand why this doen't work

 

it says:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in (file location) on line 14

 

This is the script:

 

<?php
$con = mysql_connect("localhost","usr","Pass");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("db_name", $con);

$result = mysql_query("SELECT * FROM tablename");

while($row = mysql_fetch_array($result))
  {
  echo $row['row'] . " " . $row['email'];
  echo "<br />";
  }

mysql_close($con);
?>

 

while($row = mysql_fetch_array($result)) is line 14

Link to comment
https://forums.phpfreaks.com/topic/202992-mysql_fetch_array-error/
Share on other sites

Okay starting from scratch this is how I would do it of the top of my head:

$con = mysql_connect('localhost','user','pass,') or die (mysql_error));

mysql_select_db('db_name');

$query = "SELECT * FROM tablename";
$mysql = mysql_query($query);

while($row = mysql_fetch_array($mysql, MYSQL_ASSOC))

    echo $row['field'];

endwhile;



 

I don't usually close the connection, no point really.

 

i used this example of the internet first:

 

<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("my_db", $con);

$result = mysql_query("SELECT * FROM Persons");

echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['FirstName'] . "</td>";
  echo "<td>" . $row['LastName'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);
?>

 

Then i used this one:

<?php
// Make a MySQL Connection
mysql_connect("localhost", "admin", "1admin") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());

// Get all the data from the "example" table
$result = mysql_query("SELECT * FROM example") 
or die(mysql_error());  

echo "<table border='1'>";
echo "<tr> <th>Name</th> <th>Age</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td>"; 
echo $row['name'];
echo "</td><td>"; 
echo $row['age'];
echo "</td></tr>"; 
} 

echo "</table>";
?>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.