简体   繁体   中英

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). Can you please tell me what im doing wrong?

Thank you.

becouse you are mixing mysqli_* and mysql_* function, use mysqli_* function only

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 ?

First check it and try again with mysqli_fetch_row .

If also you have problem you can try the mysqli_fetch_array and then use the $result[0] which is the field.

Otherwise, you can get the result an print the result with print_r($result) to check the problem.

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;

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