phunder Posted July 6, 2010 Share Posted July 6, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/206871-mysql_fetch_row-used-after-mysql_result-does-not-seem-to-work/ Share on other sites More sharing options...
fenway Posted July 6, 2010 Share Posted July 6, 2010 That's probably because they share a pointer. Never use mysql_result(), and you'll be just fine. Quote Link to comment https://forums.phpfreaks.com/topic/206871-mysql_fetch_row-used-after-mysql_result-does-not-seem-to-work/#findComment-1081889 Share on other sites More sharing options...
phunder Posted July 7, 2010 Author Share Posted July 7, 2010 Thanks for the answer. I'll do as you suggest. Quote Link to comment https://forums.phpfreaks.com/topic/206871-mysql_fetch_row-used-after-mysql_result-does-not-seem-to-work/#findComment-1082479 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.