Page 1 of 1
simple search for sql data base
Posted: Tue Apr 20, 2004 4:48 pm
by hward
i have a data base with a table named "items" with a field "stock_num" that I want to search but just the stock_num field and return and show just a fiew fields like
"stock_num"
"qty"
"price"
"cost"
"discription"
don't care how they are displayed I have been trying alot of different scripts i have found on the net and have given up on them they all just seem to be just displaying all items in a list from the table so I have given up on them
Posted: Tue Apr 20, 2004 5:12 pm
by tim
use the % symbol.
Its a wildcard - like * is
Posted: Tue Apr 20, 2004 5:43 pm
by litebearer
Tossed this together kind of fast, but I think its correct
Code: Select all
<?PHP
// connect to database
$user="*****";
$password="*****";
$database="*****";
$conn = mysql_connect(localhost,$user,$password);
mysql_select_db($database) or die( "Unable to select database");
// ----------------------------
// create the sql statement
$sql = "SELECT stocknum, qty, price, cost, description FROM items WHERE stock_num = $what_stock";
// execute the sql statement
$result = mysql_query($sql, $conn) or die("problem here");
// -----------------------------------
// loop thru and display the rows
?>
<table>
<tr>
<td>Stock Number</td>
<td>Qty</td>
<td>Price</td>
<td>Cost</td>
<td>Description</td>
</tr>
<?PHP
while ($row = mysql_fetch_array($result)) {
// get data from the current row
$qty = $rowї"qty"];
$price = $rowї"price"];
$cost = $rowї"cost"];
$description = $rowї"description"];
?>
<tr>
<td><?PHP echo $what_stock; ?></td>
<td><?PHP echo $qty; ?></td>
<td><?PHP echo $price; ?></td>
<td><?PHP echo $cost; ?></td>
<td><?PHP echo $description; ?></td>
</tr>
<?PHP
}
?>
Lite...
Posted: Tue Apr 20, 2004 6:15 pm
by hward
i am getting the problem here error
am i missing where its saying what table to look in "items"
Posted: Tue Apr 20, 2004 6:36 pm
by hward
Think i got it
Code: Select all
<?PHP
// connect to database
$database = "******";
$table_name = "******";
$connection = @mysql_connect("localhost", "******", "*******")
or die("Couldn't connect.");
$db = @mysql_select_db($database, $connection)
or die("Couldn't select database.");
// ----------------------------
// create the sql statement
$sql = "SELECT stock_num, price, cost, item_type, item_disc
FROM $table_name WHERE stock_num = $what_stock";
// execute the sql statement
$result = mysql_query($sql, $connection) or die("problem here");
// -----------------------------------
// loop thru and display the rows
?>
<table>
<tr>
<td>Stock Number</td>
<td>Qty</td>
<td>Price</td>
<td>Cost</td>
<td>Description</td>
</tr>
<?PHP
while ($row = mysql_fetch_array($result)) {
// get data from the current row
$qty = $rowї"qty"];
$price = $rowї"price"];
$cost = $rowї"cost"];
$item_disc = $rowї"item_disc"];
?>
<tr>
<td><?PHP echo $what_stock; ?></td>
<td><?PHP echo $qty; ?></td>
<td><?PHP echo $price; ?></td>
<td><?PHP echo $cost; ?></td>
<td><?PHP echo $item_disc; ?></td>
</tr>
<?PHP
}
?>
Posted: Tue Apr 20, 2004 6:37 pm
by hward
Thank you very much that was a big help