Connecting Tech Pros Worldwide Help | Site Map

I have a weird database problem: it is returning spurious rows withall the fields concatenated into one

 
LinkBack Thread Tools Search this Thread
  #1  
Old March 4th, 2006, 09:45 AM
Jeff Silverman
Guest
 
Posts: n/a
Default I have a weird database problem: it is returning spurious rows withall the fields concatenated into one

I have a PHP program that almost works. I'm running it from the command
line and simulating a form using a GET method. That part is working,
but I get spurious records with all of the fields concatenated into a
single field.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=windows-1252"
http-equiv="content-type">
<title>eqdb_read.php - Read the Equipment Database</title>
</head>
<body>
<small>This document last modified
<script>document.writeln(document.lastModified)</script>.</small>

<?php
$_GET['hostname'] = $argv[1]; // grab [1] because [0] holds the script
name FOR DEVELOPMENT ON THE COMMAND LINE ONLY

$hostname = $_GET['hostname'];
if ( is_null($hostname) ) {
?>

<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="GET">
Host Name:
<input type="text" name="hostname" /> <br />
<input type="submit" name="Lookup by hostname" />
</form>

<?php
} else {
require_once('DB.php');
$dsn = "mysql://read@black/equipment_test";
print("Connecting to database $dsn <br>\n");
$db=DB::connect($dsn, array('debug'=>1 ));
if (DB::iserror($db)) {
die($db->getMessage());
};
print("Connected. Looking up $hostname <br/>\n");
$sql = "SELECT name, location, kernel, distribution, sysadmin, ip,
mac, vendor_sn, real_num
FROM computers WHERE name LIKE '".$hostname."%' ORDER BY name ASC";

$q = $db->query($sql);
if ( DB::iserror($q)) {
die( $q->getMessage());
}
print("Completed the query<br><br>");


// generate the table
?>
<table>
<tr>
<th>name</th>
<th>location</th>
<th>kernel</th>
<th>distribution</th>
<th>sysadmin</th>
<th>ip</th>
<th>mac</th>
<th>vendor_sn</th>
<th>real_num</th>
</tr>
<?php
while ( $row = $q->fetchRow() ) {
if ( DB::isError($row) ) {
die ( $row->getMessage() );
}
print(" <tr>\n");
for ($field=0; $field<4; $field++ ) {
print(" <td>$row[$field] </td>\n");
};
print(" </tr>\n");
}
$db->disconnect();
}

?>
</table>
</body>
</html>

When I run the program, I get no error messages, but I get spurious records:

[jeffs@black public_html]$ php eqdb_read.php 238lvs | more
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=windows-1252"
http-equiv="content-type">
<title>eqdb_read.php - Read the Equipment Database</title>
</head>
<body>
<small>This document last modified
<script>document.writeln(document.lastModified)</script>.</small>

Connecting to database mysql://read@black/equipment_test <br>
Connected. Looking up 238lvs <br/>
Completed the query<br><br><table>
<tr>
<th>name</th>
<th>location</th>
<th>kernel</th>
<th>distribution</th>
<th>sysadmin</th>
<th>ip</th>
<th>mac</th>
<th>vendor_sn</th>
<th>real_num</th>
</tr>
<tr>
<td>238lvs01.prognet.com N debian Linux 2.4.20 prod
Se </td> <---- this is incorrect!!!!
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<------ This is okay!!!!!
<td>238lvs01.prognet.com </td>
<td>Seattle,MSR,C05,43 </td>
<td>Linux 2.4.20 </td>
<td>debian </td>
<td>ISO </td>
<td>192.168.238.25 </td>
<td>00:03:47:84:D6:63 </td>
<td> </td>
</tr>
<tr>
<td>238lvs02.prognet.com N debian Linux 2.4.20 prod
Se </td> <------- this is INCORRECT!!!!!!!!
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>238lvs02.prognet.com </td>
<-------- This is okay!!!!
<td>Seattle,MSR,C05,28 </td>
<td>Linux 2.4.20 </td>
<td>debian </td>
<td>ISO </td>
<td>192.168.238.112 </td>
<td>00:03:47:7C:25:CE </td>
<td> </td>
</tr>
</table>
</body>
</html>
[jeffs@black public_html]$

I don't get it. Does anybody have an insight?


Many thanks,


Jeff Silverman

jeff aat commercialventvac d0t com


  #2  
Old March 4th, 2006, 09:55 AM
Janwillem Borleffs
Guest
 
Posts: n/a
Default Re: I have a weird database problem: it is returning spurious rows with all the fields concatenated into one

Jeff Silverman" <""\"Remove the letters in all caps\" <jeff\ wrote:[color=blue]
> I have a PHP program that almost works. I'm running it from the
> command line and simulating a form using a GET method. That part is
> working, but I get spurious records with all of the fields concatenated
> into a
> single field.
>[/color]
[...][color=blue]
> <small>This document last modified
> <script>document.writeln(document.lastModified)</script>.</small>
>[/color]

You are using PHP, no use for JS here:

<small> This document last modified
<?php print date("F d Y H:i:s.", getlastmod()) ?></small>
[color=blue]
> $sql = "SELECT name, location, kernel, distribution, sysadmin, ip,
> mac, vendor_sn, real_num
> FROM computers WHERE name LIKE '".$hostname."%' ORDER BY name
> ASC";[/color]

You are selecting 9 fields from each row...
[color=blue]
> print(" <tr>\n");
> for ($field=0; $field<4; $field++ ) {
> print(" <td>$row[$field] </td>\n");
> };
> print(" </tr>\n");
>[/color]

....but are only using the 4 first fields from each row?

Why not doing something like:

print "<tr>\n\t<td>";
print implode("</td>\n\t<td>", $row);
print "</td>\n</tr>";


JW


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.