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

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.