Jump to content

mysql_fetch_row used after mysql_result does not seem to work


phunder

Recommended Posts

Hi there

 

I've been teaching myself php and sql the last couple of weeks. I'm currently trying to combine the two, and generally it is going well.

 

I have come across something that I do not understand though. It seems that when I use the "mysql_result" command in my program the "mysql_fetch_row" command stops working. If I use "mysql_fetch_row" before using "mysql_result" it returns valid data, but otherwise it returns an empty string (I think?).

 

Here is a code snippet of what I am doing:

 

$row = mysql_fetch_row($result); 
echo "<br/><br/>".$row[2]."<br/><br/>"; // returns valid data

$rows = mysql_num_rows($result);
for ($j = 0 ; $j < $rows ; ++$j)
{
	echo 'Author: ' . mysql_result($result,$j,'author') . '<br />';
}

$row = mysql_fetch_row($result);
echo "<br/><br/>".$row[2]."<br/><br/>"; // returns empty string

 

I know that I probably wont use it like this, but I'm just trying to understand how it works and why it happens. Perhaps there is something else in my code causing the problem.

 

Here is the full code:

 

<?php
require_once 'login.php';
$db_server = mysql_connect($db_hostname, $db_username, $db_password);
if (!$db_server) {
	die ("Database access failed: " . mysql_error());
}

mysql_select_db($db_database);
$query = "SELECT * FROM classics";
$result = mysql_query($query);
if (!$result) {
	die ("Database access failed: " . mysql_error());
}

$row = mysql_fetch_row($result);
echo "<br/><br/>".$row[2]."<br/><br/>";

$rows = mysql_num_rows($result);
for ($j = 0 ; $j < $rows ; ++$j)
{
	echo 'Author: ' . mysql_result($result,$j,'author') . '<br />';
}

$row = mysql_fetch_row($result);
echo "<br/><br/>".$row[2]."<br/><br/>";

for ($j = 0 ; $j < $rows ; ++$j)
{
	echo 'Author: ' . mysql_result($result,$j,'author') . '<br />';
}	

$row = mysql_fetch_row($result);
echo "<br/><br/>".$row[2]."<br/><br/>";

mysql_close($db_server);	
?>

 

Id appreciate any suggestions about what is happening here. Thanks :)

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.