vincej Posted June 2, 2010 Share Posted June 2, 2010 Hi - I am a bit of a noob ... I have searched high and low and examined my code for 2 days and I just can't figure this out. I run a mysql_fetch_assoc ( or mysql_fetch_array ) inside a 'while' function to extract row data from MySQL. It works and echo's the result BUT no matter what I do, it leaves off the first row of data, id=1 and is the primary. What is even crazier is that when I look at the table through Dreamweaver it comes up fine. Also if I do an 'insert dynamic table' with Dreamweaver row 1 is there. But if I use the code I want ( below) row 1 disappears. It's driving me crazy - Many thanks in advance Vincej ! $query_rsSubjects = "SELECT * FROM subjects"; $rsSubjects = mysql_query($query_rsSubjects, $WidgetCorp) or die(mysql_error()); while($row = mysql_fetch_assoc($rsSubjects)) {echo $row['menu_name']." ".$row['position']; } Quote Link to comment https://forums.phpfreaks.com/topic/203672-please-help-mysql_fetch_assoc-looses-a-sql-row/ Share on other sites More sharing options...
Psycho Posted June 2, 2010 Share Posted June 2, 2010 I suspect the problem is one of two things: 1. You did not post ALL of the code between the line on which the query is run and the WHILE loop. Pehaps there is a mysql_fetch_assoc() run before the loop? I've seen it many times. 2. The values in the first record of data contains characters that are being interpreted as HTML code. Post a little more of the code as I suspect the error is in the code and not the value. Quote Link to comment https://forums.phpfreaks.com/topic/203672-please-help-mysql_fetch_assoc-looses-a-sql-row/#findComment-1066803 Share on other sites More sharing options...
vincej Posted June 2, 2010 Author Share Posted June 2, 2010 Thank you for your help - here is all the code. I didn't want to bore everyone with it. mysql_select_db($database_WidgetCorp, $WidgetCorp); $query_rsSubjects = "SELECT * FROM subjects"; $rsSubjects = mysql_query($query_rsSubjects, $WidgetCorp) or die(mysql_error()); $row_rsSubjects = mysql_fetch_assoc($rsSubjects); $totalRows_rsSubjects = mysql_num_rows($rsSubjects); ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Test1</title> <link href="public2.css" rel="stylesheet" type="text/css" /> <style type="text/css"> <!-- body { background-color: #FFC; list-style: circle inside; } a { font-size: medium; list-style: circle outside; } a:link { color: #900; } --> </style> </head> <body> <div id="header"> <h1>Header Widget Corp</h1></div> <div id="Main_Body"> <h1 class="h1MainBody">Staff Menu</h1> <p class="pMainBody">Welcome to our Staff area</p> <p class="pMainBody"><a href="content.php" title="Manage Website content">Manage Website Content</a></p> <p class="pMainBody"><a href="new_user.php" title="new_user">Add Staff User</a></p> <p class="pMainBody"><a href="Logout.php" title="logout">Logout</a></p> </div> <div id="left"> <?php echo "<p class = navbar>"; while($row = mysql_fetch_assoc($rsSubjects)) { echo $row['menu_name']." ".$row['position'] ; } echo "</p>"; ?> <!-- "<br/>" --> </div> Quote Link to comment https://forums.phpfreaks.com/topic/203672-please-help-mysql_fetch_assoc-looses-a-sql-row/#findComment-1066817 Share on other sites More sharing options...
sspoke Posted June 2, 2010 Share Posted June 2, 2010 of course it wont work whats $row_rsSubjects = mysql_fetch_assoc($rsSubjects); haha it has no significance all else fails nothing u can do about it just start using http://www.php.net/manual/en/function.mysql-fetch-row.php haha Quote Link to comment https://forums.phpfreaks.com/topic/203672-please-help-mysql_fetch_assoc-looses-a-sql-row/#findComment-1066825 Share on other sites More sharing options...
vincej Posted June 2, 2010 Author Share Posted June 2, 2010 You are super star !!! Many many thanks ! And I am a "stupid star" ! vincej Quote Link to comment https://forums.phpfreaks.com/topic/203672-please-help-mysql_fetch_assoc-looses-a-sql-row/#findComment-1066829 Share on other sites More sharing options...
Psycho Posted June 2, 2010 Share Posted June 2, 2010 Just a tip. If you put all your logic (i.e. PHP) at the top of the page and the presentation (HTML) at the bottom you can avoid these types of errors by giving the script a more logic structure. Also, this line seems to serve no purpose $totalRows_rsSubjects = mysql_num_rows($rsSubjects); Here is how I would contrcut that page: <?php mysql_select_db($database_WidgetCorp, $WidgetCorp); $query_rsSubjects = "SELECT * FROM subjects"; $rsSubjects = mysql_query($query_rsSubjects, $WidgetCorp) or die(mysql_error()); $row_rsSubjects = mysql_fetch_assoc($rsSubjects); $menuHTML = ''; while($row = mysql_fetch_assoc($rsSubjects)) { $menuHTML = "{$row['menu_name']} {$row['position']}\n"; } ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Test1</title> <link href="public2.css" rel="stylesheet" type="text/css" /> <style type="text/css"> <!-- body { background-color: #FFC; list-style: circle inside; } a { font-size: medium; list-style: circle outside; } a:link { color: #900; } --> </style> </head> <body> <div id="header"> <h1>Header Widget Corp</h1></div> <div id="Main_Body"> <h1 class="h1MainBody">Staff Menu</h1> <p class="pMainBody">Welcome to our Staff area</p> <p class="pMainBody"><a href="content.php" title="Manage Website content">Manage Website Content</a></p> <p class="pMainBody"><a href="new_user.php" title="new_user">Add Staff User</a></p> <p class="pMainBody"><a href="Logout.php" title="logout">Logout</a></p> </div> <div id="left"> <p class = "navbar"> <?php echo $menuHTML; ?> </p> <!-- "<br/>" --> </div> Quote Link to comment https://forums.phpfreaks.com/topic/203672-please-help-mysql_fetch_assoc-looses-a-sql-row/#findComment-1066864 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.