Jump to content

while($row = mysql_fetch_array($result, MYSQL_ASSOC))


jr8rdt

Recommended Posts

do you know why when I run this code below I get several blank lines before the first record is printed?

I strongly suspect that this has something to do  with

while($row = mysql_fetch_array($result, MYSQL_ASSOC))

but can't figure it out.

is there an alternative?

help

 

----------

include 'db_connect.php';

$query  = "SELECT * FROM server ORDER BY server_id";

$result = mysql_query($query);

 

while($row = mysql_fetch_array($result, MYSQL_ASSOC))

{

   

$id1 = "{$row['server_id']}";

echo "<tr>";

  echo "<form name='myform' action='modify.php' method='post'>";

  echo " <td><a href='modify.php?id1=$id1'> {$row['server_id']}</a></td>";

  echo "</form>";

echo "<td>{$row['server_hostname']} </td>";

echo "<td>{$row['server_ip']} </td>";

echo "<td>{$row['server_status']} </td>";

echo "<td>{$row['server_os']} </td>";

echo "<td>{$row['server_hw']} </td>";

echo "<td>{$row['server_sw']} </td>";

echo "<td>{$row['server_user']} </td>";

echo "<td>{$row['server_date']} </td>";

echo "<td>{$row['server_desc']} </td>";

echo "</tr><br>";

 

}

 

Link to comment
Share on other sites

     echo "<form name='myform' action='modify.php' method='post'>";
     echo " <td><a href='modify.php?id1=$id1'> {$row['server_id']}[/url]</td>";
     echo "</form>";

 

That doesn't make much sense. Remove the opening and closing form statements completely - they're doing nothing useful.  As to the blank lines, that sounds like empty rows in your database.

Link to comment
Share on other sites

Use [ code ] tags, they help everyone out.

 

<?php
include 'db_connect.php';
$query  = "SELECT * FROM server ORDER BY server_id";
$result = mysql_query($query);

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
   echo '<pre>',print_r($row),'</pre><br />';
   continue;

   $id1 = "{$row['server_id']}";
   echo "<tr>";
     echo "<form name='myform' action='modify.php' method='post'>";
     echo " <td><a href='modify.php?id1=$id1'> {$row['server_id']}[/url]</td>";
     echo "</form>";
   echo "<td>{$row['server_hostname']} </td>";
   echo "<td>{$row['server_ip']} </td>";
   echo "<td>{$row['server_status']} </td>";
   echo "<td>{$row['server_os']} </td>";
   echo "<td>{$row['server_hw']} </td>";
   echo "<td>{$row['server_sw']} </td>";
   echo "<td>{$row['server_user']} </td>";
   echo "<td>{$row['server_date']} </td>";
   echo "<td>{$row['server_desc']} </td>";
   echo "</tr>
";
   
   }
?>

 

try the above and see what is being printed out in your $row array.

Link to comment
Share on other sites

Do you not need to define $row and then extract it?

 

eg:

 

<?php
$query  = "SELECT * FROM server ORDER BY server_id";
$result = mysql_query($query);
$row = mysql_fetch_array($result, MYSQL_ASSOC);
extract ($row); 

 

this may not actually be necessary but that is how I have done it in the past

Link to comment
Share on other sites

Do you not need to define $row and then extract it?

 

eg:

 

<?php
$query  = "SELECT * FROM server ORDER BY server_id";
$result = mysql_query($query);
$row = mysql_fetch_array($result, MYSQL_ASSOC);
extract ($row); 

 

this may not actually be necessary but that is how I have done it in the past

 

No with the code he has that is not necessary and is probably not a good idea. The extract function is nice sometimes but yea, if there is more than 1 row in that database the code you just posted would not go any good as that would only get the first row.

 

$row is returned as an array as such the variables are accessed by the name of the column which is the index.

 

The real question is, is he calling the column names correctly.

Link to comment
Share on other sites

Do you not need to define $row and then extract it?

 

eg:

 

<?php
$query  = "SELECT * FROM server ORDER BY server_id";
$result = mysql_query($query);
$row = mysql_fetch_array($result, MYSQL_ASSOC);
extract ($row); 

 

this may not actually be necessary but that is how I have done it in the past

 

No with the code he has that is not necessary and is probably not a good idea. The extract function is nice sometimes but yea, if there is more than 1 row in that database the code you just posted would not go any good as that would only get the first row.

 

$row is returned as an array as such the variables are accessed by the name of the column which is the index.

 

The real question is, is he calling the column names correctly.

 

Thanks for that! I have actually experienced some problems in the past...this is probably why....mental note made!

Link to comment
Share on other sites

This is the output. I miss what you're trying to do here.

Array

(

    [server_id] => 1

    [server_hostname] => dadaadam

    [server_ip] => 121.21.233.11

    [server_status] => Active

    [server_os] => Windows Server 2003

    [server_hw] => Dual-Core AMD Opteron Processor 8218

    [server_sw] => S20

    [server_user] => Junior

    [server_date] => 2007-06-01 13:38:29

    [server_desc] =>  Test

)

1

 

 

Array

(

    [server_id] => 2

    [server_hostname] => gdf3321

    [server_ip] => 23.23.42.32

    [server_status] => Active

    [server_os] => Windows Server 2003

    [server_hw] => Dual-Core AMD Opteron Processor 8218

    [server_sw] => BS1

    [server_user] => Junior

    [server_date] => 2007-06-01 14:11:05

    [server_desc] => 

)

1

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.