Jump to content

Please Help: mysql_fetch_assoc looses a SQL row


vincej

Recommended Posts

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']; }   

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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>

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.