如何在php中返回值

时间:2015-10-08 10:11:59

标签: php mysql

我试图返回值,即查询结果。对于下面的函数,我传递product_id来获取product_name。基本上我需要做的是当我调用这个函数时,我应该能够将product_name作为返回类型,可以在其他地方使用。

function getProductName($product_id){
    $conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 
    $sql = "SELECT product_name FROM product_list where product_id ='$product_id'";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
            echo '<span>'. $row["product_name"].'</span>';
        }
    } else {
        echo "No results found";
    }
    $conn->close(); 
    return $productName;    
}

例如:

$product_name = return getProductName(45);

我会得到numark mixtrack pro 2,应该能够分配到$product_name;

我该怎么做?

5 个答案:

答案 0 :(得分:1)

您需要创建一个空数组并将产品名称分配给

$productName=array();//creare an empty array
if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
            $productName[]=$row["product_name"];// assing your product name to array
        }
    } else {
        echo "No results found";
    }
    $conn->close(); 
    return $productName; // return your data

您可以使用

获取产品名称
 $product_name =  getProductName(45);
 print_r($product_name);

答案 1 :(得分:1)

function getProductName($product_id)
{
    $conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 
    $sql = "SELECT product_name FROM product_list where product_id ='$product_id'";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) 
    {
        $row = $result->fetch_assoc() 
        $productName =  $row["product_name"];
    }
    else 
        echo "No results found";
    $conn->close(); 
    return $productName;    
}

答案 2 :(得分:1)

在您退回之前,您永远不会设置$productName。您需要将其设置为$row['product_name']

function getProductName($product_id){
    $conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 
    $sql = "SELECT product_name FROM product_list where product_id ='$product_id'";
    $result = $conn->query($sql);
    $row = $result->fetch_assoc();
    if ($row) {
        $productName = $row["product_name"];
        echo '<span>'. $productName.'</span>';
    } else {
        echo "No results found";
        $productName = "";
    }
    $conn->close(); 
    return $productName;    
}

不需要while循环,因为只能有一个带ID的产品。

答案 3 :(得分:0)

也许你想要这样的东西

function getProductName($product_id){
    $conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 
    $sql = "SELECT product_name FROM product_list where product_id ='$product_id'";
    $htmlResult= '';
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
            $htmlResult.= '<span>'. $row["product_name"].'</span>';
        }
    } else {
        $htmlResult = "No results found";
    }
    $conn->close(); 
    return  $htmlResult;    
}

答案 4 :(得分:0)

更新:

function getProductName($product_id){
    $conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 
    $sql = "SELECT product_name FROM product_list where product_id ='$product_id'";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
       $row = $result->fetch_assoc());
       $result= $row["product_name"];
    } 
    else {  $result = false;  }
    $conn->close(); 

   return $result;

}

使用:

 $productName = getProductName(45);
 if ( $ProductName != false ) { echo '<span>'.$productName.'</span>'; } 
 else { echo 'No product found!!"; }