homepage Welcome to WebmasterWorld Guest from 54.242.126.9
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Databases
Forum Library, Charter, Moderator: open

Databases Forum

    
Calling rows based on keyword
Jim123



 
Msg#: 4186871 posted 1:24 am on Aug 14, 2010 (gmt 0)

I would like to print part of a row. The row is selected by a search with a keyword.

table name: items

item_no name totA totB totC totD totE
01001 house 10 20 30 40 50
01002 food 15 25 35 45 55
01003 work 115 125 135 145 155


I want to print only the columns 'name', 'totA', 'totB', 'totC' and 'totE'

So far, I use this as the search form
<form action="/vitamine/wp-content/themes/twentyten/insert.php" method="post">
Value1: <input type="text" name="value1"><br>
<input type="Submit">
</form>


And this to call the data
<?php
$username="root";
$password="";
$database="test";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM items WHERE name LIKE '%".mysql_real_escape_string($_POST['value1'])."%'";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();
?>
<table border="1" align="center" cellpadding="2" cellspacing="2">
<tr>
<th><font face="Arial, Helvetica, sans-serif">Value1</font></th>
</tr>

<?php
$i=0;
while ($i < $num) {

$f1=mysql_result($result,$i,"Shrt_Desc");

?>

<tr>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font></td>

</tr>

<?php
$i++;
}

?>
</table>


But this gives me a list of all search results items and no rows. How can I print the whole row minus the columns I don't want?

Thanks again!

 

rocknbil

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4186871 posted 3:23 am on Aug 14, 2010 (gmt 0)

By selecting the columns you want, instead of * which selects all columns. also the "counter method" (I call it) is not needed to step through recordsets unless you specifically need a count of rows. Like so . . .

$results = null; // squelches concatenation errors and makes for a nice if at the end
$query="SELECT name, totA, totB, totC, totD, totE FROM items WHERE name LIKE '%".mysql_real_escape_string($_POST['value1'])."%'";

$result=mysql_query($query) or die("could not execute query: " . mysql_error());
while ($row=mysql_fetch_array($result)) {
$results .= '<tr>
<td>' . $row['name'] . '</td>
<td>' . $row['totA'] . '</td>
<td>' . $row['totB'] . '</td>
<td>' . $row['totC'] . '</td>
<td>' . $row['totE'] . '</td>
</tr>';
}
mysql_close();
//
if ($results) {
echo "<table border=\"1\" align=\"center\" cellpadding=\"2\" cellspacing=\"2\">
<tr><td>NAME</td><td>A</td><td>B</td><td>C</td><td>D</td><td>E</td></tr>
$results
</table>
";
}
else { echo "<p>No results were found with that search.</p>"; }
?>


May contain typos, but that should give you an idea.

Jim123



 
Msg#: 4186871 posted 5:42 am on Aug 14, 2010 (gmt 0)

Looks great (and simple)

I got the idea and I want to try it out now, but my wife won't let me :)

Thanks!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Databases
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved