简体   繁体   English

如何从数据库表中获取MySQL和MySQL中的MAX product_id?

[英]How can I get the MAX product_id in MySQL & PHP from database table?

I want to have a function on my website where on the "New" page, it will show my single newest product, so it will need to take the last one added to the products table in SQL but I cannot figure out how to get the max and retrieve it onto the website with the price and image. 我想在我的网站上的“新建”页面上有一个功能,它将显示我的最新产品,因此它将需要在SQL中将最后一个产品添加到产品表中,但是我无法弄清楚如何获得该产品。 max,并使用价格和图片将其检索到网站上。

I am getting the 2 errors below, line 58 is the $pro_price line and 59 is the $pro_image: 我收到下面的2个错误,第58行是$ pro_price行,而59是$ pro_image:

Notice: Undefined index: product_price in /opt/lampp/htdocs/ecommerce/new.php on line 58 注意:未定义的索引:第58行的/opt/lampp/htdocs/ecommerce/new.php中的product_price

Notice: Undefined index: product_image in /opt/lampp/htdocs/ecommerce/new.php on line 59 注意:未定义的索引:第59行/opt/lampp/htdocs/ecommerce/new.php中的product_image

还请查看带有屏幕截图的链接。

<?php

$get_pro = "select MAX(product_id) from products";

$run_pro = mysqli_query($con, $get_pro);

while($row_pro=mysqli_fetch_array($run_pro)){

    $pro_price = $row_pro['product_price'];
    $pro_image = $row_pro['product_image'];

To retrieve the last record, you can order it by the product_id (descending) and just use limit to fetch 1 record... 要检索最后一条记录,您可以按product_id(降序)对其进行排序,并仅使用limit来获取1条记录...

$get_pro = "select product_id, product_price, product_image 
               from products
               order by product_id desc
               limit 1";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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