PHP - mysqli_query()期望至少有2个参数,给出1

时间:2017-01-12 04:55:25

标签: php

这是错误:

  

警告:mysqli_query()需要至少2个参数,在第25行的> C:\ xampp \ htdocs \ ajax_cart \ dbcontroller.php中给出1

     

警告:mysqli_fetch_assoc()要求参数1为mysqli_result,null>在第26行的C:\ xampp \ htdocs \ ajax_cart \ dbcontroller.php中给出

这是代码:     

function construct() {
    $conn = $this->connectDB();
    if(!empty($conn)) {
        $this->selectDB($conn);
    }
}

function connectDB() {
    $conn = mysqli_connect($this->host,$this->user,$this->password);
    return $conn;
}

function selectDB($conn) {
    mysqli_select_db($this->database,$conn);
}

function runQuery($query) {
    $result = mysqli_query($query);
    while($row=mysqli_fetch_assoc($result)) {
        $resultset[] = $row;
    }       
    if(!empty($resultset))
        return $resultset;
}

function numRows($query) {
    $result  = mysqli_query($query);
    $rowcount = mysqli_num_rows($result);
    return $rowcount;   
}
}
?>

这是索引文件:

<?php
require_once("dbcontroller.php");
$db_handle = new DBController();
?>
<HTML>
<HEAD>
<TITLE>PHP Shopping Cart with jQuery AJAX</TITLE>
<link href="style.css" type="text/css" rel="stylesheet" />
<script src="https://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript"></script>
<script>
function showEditBox(editobj,id) {
    $('#frmAdd').hide();
    $(editobj).prop('disabled','true');
    var currentMessage = $("#message_" + id + " .message-content").html();
    var editMarkUp = '<textarea rows="5" cols="80" id="txtmessage_'+id+'">'+currentMessage+'</textarea><button name="ok" onClick="callCrudAction(\'edit\','+id+')">Save</button><button name="cancel" onClick="cancelEdit(\''+currentMessage+'\','+id+')">Cancel</button>';
    $("#message_" + id + " .message-content").html(editMarkUp);
}
function cancelEdit(message,id) {
    $("#message_" + id + " .message-content").html(message);
    $('#frmAdd').show();
}
function cartAction(action,product_code) {
    var queryString = "";
    if(action != "") {
        switch(action) {
            case "add":
                queryString = 'action='+action+'&code='+ product_code+'&quantity='+$("#qty_"+product_code).val();
            break;
            case "remove":
                queryString = 'action='+action+'&code='+ product_code;
            break;
            case "empty":
                queryString = 'action='+action;
            break;
        }    
    }
    jQuery.ajax({
    url: "ajax_action.php",
    data:queryString,
    type: "POST",
    success:function(data){
        $("#cart-item").html(data);
        if(action != "") {
            switch(action) {
                case "add":
                    $("#add_"+product_code).hide();
                    $("#added_"+product_code).show();
                break;
                case "remove":
                    $("#add_"+product_code).show();
                    $("#added_"+product_code).hide();
                break;
                case "empty":
                    $(".btnAddAction").show();
                    $(".btnAdded").hide();
                break;
            }    
        }
    },
    error:function (){}
    });
}
</script>
</HEAD>
<BODY>
<div id="product-grid">
    <div class="txt-heading">Products</div>
    <?php
    $product_array = $db_handle->runQuery("SELECT * FROM tblproduct ORDER BY id ASC");
    if (!empty($product_array)) { 
        foreach($product_array as $key=>$value){
    ?>
        <div class="product-item">
            <form id="frmCart">
            <div class="product-image"><img src="<?php echo $product_array[$key]["image"]; ?>"></div>
            <div><strong><?php echo $product_array[$key]["name"]; ?></strong></div>
            <div class="product-price"><?php echo "$".$product_array[$key]["price"]; ?></div>
            <div><input type="text" id="qty_<?php echo $product_array[$key]["code"]; ?>" name="quantity" value="1" size="2" />
            <?php
                $in_session = "0";
                if(!empty($_SESSION["cart_item"])) {
                    $session_code_array = array_keys($_SESSION["cart_item"]);
                    if(in_array($product_array[$key]["code"],$session_code_array)) {
                        $in_session = "1";
                    }
                }
            ?>
            <input type="button" id="add_<?php echo $product_array[$key]["code"]; ?>" value="Add to cart" class="btnAddAction cart-action" onClick = "cartAction('add','<?php echo $product_array[$key]["code"]; ?>')" <?php if($in_session != "0") { ?>style="display:none" <?php } ?> />
            <input type="button" id="added_<?php echo $product_array[$key]["code"]; ?>" value="Added" class="btnAdded" <?php if($in_session != "1") { ?>style="display:none" <?php } ?> />
            </div>
            </form>
        </div>
    <?php
            }
    }
    ?>
</div>
<div class="clear-float"></div>
<div id="shopping-cart">
<div class="txt-heading">Shopping Cart <a id="btnEmpty" class="cart-action" onClick="cartAction('empty','');">Empty Cart</a></div>
<div id="cart-item"></div>
</div>
<script>
$(document).ready(function () {
    cartAction('','');
})
</script>


</BODY>
</HTML>

我不知道另一个参数是什么......我可能会错过什么?

2 个答案:

答案 0 :(得分:1)

您需要将$conn变量传递给mysqli_query($query); 以下是您需要遵循的示例。

mysqli_query($conn,"SELECT * FROM table");

遵循该方法您的新代码应该是(仅限两个函数,其余代码将保持相同)

function runQuery($query) {
    $conn = connectDB(); // if you are using within class then $this->connectDB();
    $result = mysqli_query($conn,$query);
    if($result)
    {
    if(mysqli_num_rows($result) > 0)
    {
    while($row=mysqli_fetch_assoc($result)) {
        $resultset[] = $row;
    }       
    if(!empty($resultset))
       {
        return $resultset;
       }
   }
   }
}

function numRows($query) {
    $conn = connectDB(); // if you are using within class then $this->connectDB();
    $result  = mysqli_query($query);
    $rowcount = mysqli_num_rows($result);
    return $rowcount;   
}

答案 1 :(得分:0)

执行过程样式时,需要提供db和查询。如果使用面向对象的方法,则只需要查询。

所以要么:

$conn->query;

mysqli_query($conn, $query);

Link to documentation