simple search for sql data base

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hward
Forum Contributor
Posts: 107
Joined: Mon Apr 19, 2004 6:19 pm

simple search for sql data base

Post 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
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

use the % symbol.

Its a wildcard - like * is
litebearer
Forum Contributor
Posts: 194
Joined: Sat Mar 27, 2004 5:54 am

Post 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)) &#123;

// get data from the current row
$qty = $row&#1111;"qty"]; 
$price = $row&#1111;"price"]; 
$cost = $row&#1111;"cost"];
$description = $row&#1111;"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
&#125;
?>
Lite...
hward
Forum Contributor
Posts: 107
Joined: Mon Apr 19, 2004 6:19 pm

Post by hward »

i am getting the problem here error

am i missing where its saying what table to look in "items"
hward
Forum Contributor
Posts: 107
Joined: Mon Apr 19, 2004 6:19 pm

Post 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)) &#123; 

// get data from the current row 
$qty = $row&#1111;"qty"]; 
$price = $row&#1111;"price"]; 
$cost = $row&#1111;"cost"]; 
$item_disc = $row&#1111;"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 
&#125; 
?>
hward
Forum Contributor
Posts: 107
Joined: Mon Apr 19, 2004 6:19 pm

Post by hward »

Thank you very much that was a big help
Post Reply