Tony pressed the little lettered thingies in this order...

> I am trying to figure out how to read data base(Mysql) with rows of item
> number (item) and quantity, so that when item number was given, it searched
> for the quantity of the item number. If the quantity number is 0 or less,
> it will show "Out of Stock", else will show "In Stock" on my web page. Here
> is part of language that I wrote and didn't work. Please help me what to
> do,
> 

There are two common methods.  One uses PHP and the other uses 
MySQL.  The one that is most efficient depends largely on the design 
and size of the table and the desired output.  See below...

> $result = mysql_query("SELECT * FROM inventory",$link);
> 

Here are the two queries that will work.  The first with PHP:
$result = mysql_query("SELECT * FROM inventory WHERE item = 
'$itemx'",$link);
$num = mysql_num_rows($result);
        if ($num < 1) {
        echo "Out of stock";
        } else {
        echo "In stock";
        }

This would be very inneficient because it requires MySQL to do a full 
retrieval from the table. The only reason you would want to do it this way 
is if you wanted to relace the "In stock" above with actual results from 
the query.

If you just wanted to say "In stock" it would be more efficient to use 
MySQL's built-in COUNT(*) function:
$result = mysql_query("SELECT COUNT(*) FROM inventory WHERE 
item = '$itemx'",$link);
        while ($row = mysql_fetch_row($result)) {
        $num = $row[0];
        }
        if ($num < 1) {
        echo "Out of stock";
        } else {
        echo "In stock";
        }

Note that if you use mysql_num_rows() on the second query, you will 
always get a result of one (1) because the number of rows that MySQL 
returns with this query will always be one (it returns how many records 
matched your query as its one row return), assuming that there are no 
errors. In either case, you can echo $num to display the number of 
records returned.

Good luck...

Christopher Ostmo
a.k.a. [EMAIL PROTECTED]
AppIdeas.com
Innovative Application Ideas
Meeting cutting edge dynamic
web site needs since the 
dawn of Internet time (1995)

Business Applications:
http://www.AppIdeas.com/

Open Source Applications:
http://open.AppIdeas.com/

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to