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
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.

 

Link to comment
Share on other sites

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>";
?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.