Jump to content

mysql_fetch_assoc and echo problem


davt55

Recommended Posts

I cannot retrieve my records from my table, neither can I display them on my website.  I do not even get any errors when I run the script on my site. Any help or tips would be greatly appreciated.  My current php script is:

 

// connection to database server

$db_selected = mysql_select_db('database1', $dbc);

if (!$db_selected) {

    die ('Can\'t use database1 : ' . mysql_error());

}

$query = "SELECT * FROM chesstable ORDER BY chapter, lastname";

$result = mysql_query($query) or die('Could not connect' . mysql_error());

 

echo mysql_num_rows($result);

If (!$result) {

    $message = 'Invalid query: ' . mysql_error();

        $message = 'Whole query: ' . $query;

        die ($message);

  }

while($row = mysql_fetch_assoc($result)) {

  echo $row['firstname'] . " " . $row['lastname'] . $row['chapter'];

  }

mysql_free_result($result);

mysql_close($dbc);

?>

 

Link to comment
https://forums.phpfreaks.com/topic/227535-mysql_fetch_assoc-and-echo-problem/
Share on other sites

Well, I normally never use mysql_fetch_assoc, since I always get troubled with it.. I would simply do this:

 

$numOfRows = mysql_num_rows($result);

 

$i = 0;

while ( $i < $numOfRows )

{

mysql_result($result,$i,"firstname");

mysql_result($result,$i,"lastname");

mysql_result($result,$i,"chapter");

// echo stuff

}

 

Although it might seem inefficient, it always worked for me, so that's fine.

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.