简体   繁体   English

PHP Cant从数据库行获取单个值

[英]PHP Cant get single value from database row

im trying to get a single value from a database row but i get nothing no matter what i use. 我试图从数据库行中获取单个值,但是无论我使用什么,我什么都没有。 I tried 我试过了

$con=mysqli_connect("localhost","user","pass","db");
$product_cat = mysql_result(mysqli_query($con,"SELECT product_category_id FROM am_product_product_category WHERE product_id=1  limit 1"),0);

and

$product_cat = mysql_fetch_assoc(mysqli_query($con,"SELECT product_category_id FROM am_product_product_category WHERE product_id=1  limit 1"));

and

$product_cat = mysqli_fetch_field(mysqli_query($con,"SELECT product_category_id FROM am_product_product_category WHERE product_id=1  limit 1"));

and

$product_cat = mysql_fetch_object(mysqli_query($con,"SELECT product_category_id FROM am_product_product_category WHERE product_id=1  limit 1"));

and

$product_cat = mysql_fetch_row(mysqli_query($con,"SELECT product_category_id FROM am_product_product_category WHERE product_id=1"));

But when i echo $product_cat i get nothing (it should echo 1). 但是,当我回显$ product_cat时,我什么也没得到(它应该回显1)。 Can you please tell me what im doing wrong? 你能告诉我我做错了什么吗?

Thank you. 谢谢。

becouse you are mixing mysqli_* and mysql_* function, use mysqli_* function only 因为您混用了mysqli_ *和mysql_ *函数,所以只能使用mysqli_ *函数

try this: 尝试这个:

$product_cat = mysqli_fetch_row(mysqli_query($con,"SELECT product_category_id FROM am_product_product_category WHERE product_id=1"));
echo $product_cat['product_category_id'];
print_r($product_cat);

Are your sql query correct ? 您的sql查询正确吗?

First check it and try again with mysqli_fetch_row . 首先检查它,然后使用mysqli_fetch_rowmysqli_fetch_row

If also you have problem you can try the mysqli_fetch_array and then use the $result[0] which is the field. 如果您还有问题,也可以尝试mysqli_fetch_array ,然后使用$result[0]作为该字段。

Otherwise, you can get the result an print the result with print_r($result) to check the problem. 否则,您可以使用print_r($result)将结果打印出来以检查问题。

try this 尝试这个

   $product_cat = mysqli_query($con,"SELECT product_category_id FROM am_product_product_category WHERE product_id=1  limit 1");
   $row=mysqli_fetch_assoc($product_cat);
   echo $row["product_category_id"] ;

If you have many values to fetch then use this: 如果您要提取许多值,请使用以下命令:

  $product_cat = mysqli_query($con,"SELECT product_category_id FROM am_product_product_category WHERE product_id=1  limit 1");
   while ($row=mysqli_fetch_assoc($product_cat)){
       echo $row["product_category_id"] ."<br />";
       }

Follow this example.I think this example is helpful to you. 请遵循此示例。我认为此示例对您有所帮助。

$id = $_GET['id'];
$result = mysqli_query($con,"select *from employee_details where emp_id= '$id'  ");
echo "Emp-Id";
$row = mysqli_fetch_array($result)
  echo ". $row['emp_id'] . ";
mysqli_close($con);

                  (or)

try this one also 也尝试这个

$product_cat = $mysqli->query("SELECT product_category_id FROM am_product_product_category WHERE product_id=1")->fetch_object()->product_category_id;

use this code. 使用此代码。

这是Programmer999提供的正确答案:

$product_cat = $con->query("SELECT product_category_id FROM am_product_product_category WHERE product_id=1")->fetch_object()->product_category_id;

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

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