php函数在“mysqli select”prepare语句中出错

时间:2014-12-30 15:20:14

标签: php mysql mysqli

我创建了一个函数,用left-join和prepare语句从数据库中调用一些数据。

这是php函数的草图:

function getStock()
{
    global $mysqli;
        $stmt = $mysqli->prepare 
            ("SELECT     products.`product_name`, product_category.`price`
              FROM products    
                LEFT JOIN  product_category
                ON         products.product_category_id = product_category.id

                WHERE      products.id = ?");

    $id=3; 
    $stmt->bind_param("s", $id);
    $stmt->execute();
    $stmt->fetch();
    return $stmt;

在视图页面中,我这样做:

    $resultsTicket = getStock();
    $results = $resultsTicket->num_rows;
    var_dump($results);
    if ($resultsTicket->num_rows > 0) {
        while($resultsTicket->fetch()){

然而在var_dump中我只得到int(0)

我不知道如何在var_dump旁边找到错误。请帮我完成上面的代码。谢谢!

[UPDATE]

$query = "SELECT     products.`product_name`, product_category.`price`
          FROM       products    
          LEFT JOIN  product_category
          ON         products.product_category_id = product_category.id
          WHERE      products.id = 3;

if ($stmt = $mysqli->prepare($query)) {     
    $stmt->execute();
    $stmt->bind_result($product_name, $price);
    //$stmt->fetch();
    while ($stmt->fetch()) {
    printf ("%s (%s)\n", $product_name, $price);
    }   
        $stmt->close();}
    return $stmt;

}

在上面的代码中,我从数据库中获取一个数据/行。

2 个答案:

答案 0 :(得分:2)

问题是您正在函数中执行和获取但返回语句。您可以进行两种可能的更改。

  1. 在函数内执行和获取,然后遍历结果并返回表示结果的数组。

  2. 从函数返回语句,并执行执行和获取函数的结果。

答案 1 :(得分:1)

我认为你们得到了足够的帮助,如果你还不确定如何编码,请点击此处:

单一记录

$query = "SELECT     products.`product_name`, product_category.`price`
          FROM       products    
          LEFT JOIN  product_category
          ON         products.product_category_id = product_category.id
          WHERE      products.id = 3";

if ($stmt = $mysqli->prepare($query)) {     
    $stmt->execute();
    $stmt->bind_result($product_name, $price);
    $row = array();
    while ($stmt->fetch()) {
        $row = array('product_name'=>$product_name, 'price'=>$price);
    }   
    $stmt->close();
}
return $row;

多条记录:

$query = "SELECT     products.`product_name`, product_category.`price`
          FROM       products    
          LEFT JOIN  product_category
          ON         products.product_category_id = product_category.id
          WHERE      products.id = 3";

if ($stmt = $mysqli->prepare($query)) {     
    $stmt->execute();
    $stmt->bind_result($product_name, $price);
    $rows = array();
    while ($stmt->fetch()) {
        $rows[] = array('product_name'=>$product_name, 'price'=>$price);
    }   
    $stmt->close();
}
return $rows;

PDO中的示例:

$query = "SELECT     products.`product_name`, product_category.`price`
          FROM       products    
          LEFT JOIN  product_category
          ON         products.product_category_id = product_category.id
          WHERE      products.id = 3";

if ($stmt = $pdo->prepare($query)) {     
    $stmt->execute();
    $rows = $stmt->fetchAll();
}
return $rows;