简体   繁体   中英

Count Rows MySQL

[I know mysqli is the new standard and I am in the process of updating, but for now I have questions about the old standard.]

I am trying to count the number of rows that match a query, then add a number to it.

$findTypes = "SELECT * _products WHERE product_type = '$productType'";
$queryTypes = mysql_query($findTypes, $db_products);
$numTypes = mysql_num_rows($queryTypes);
$productID = $numTypes + 100;

If there is already an existing product-type in the database, then there should return a result of 101, if there are two then 102, etc.

As it stands now this doesn't work... not sure why.

Thanks,

Your query is wrong, you have no FROM statement...

Try:

SELECT * FROM _products WHERE product_type = '$productType'

You could get the value from a single faster query, leaving MySQL to do all the work:

$findTypes = "SELECT COUNT(*) + 100 FROM _products "
           . "WHERE product_type = '$productType'";

Use mysql_error() function, something like

mysql_query($query) or die(mysql_error());

It will return error message, if your syntax is wrong.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM