我得到一个像这样的错误列计数与行的值计数不匹配。 1请解决此错误

时间:2016-02-09 04:46:36

标签: php mysql

//这是我的查询

if(isset($_POST["break-first"])) {
    $bflag='true';
    $bfid=$_POST["break-first"];

    $query = "SELECT * FROM food_details WHERE food_id = '$bfid'";
    $result = mysqli_query($_SESSION["connect"],$query);
        if(!$result) {
            $error = mysqli_error($_SESSION["connect"]);
            print $error;
            exit;
        }

    while ($row = mysqli_fetch_array($result)) {
        $bfid =$row["food_id"];
        $bf=$row["f_name"];
        $bprice= $row["price"];
    }

我收到此错误“列数与第1行的值计数不匹配”

enter image description here

3 个答案:

答案 0 :(得分:0)

改变
 $query = "SELECT * FROM food_details WHERE food_id = '$bfid'";

$query = "SELECT food_id,f_name,price FROM food_details WHERE food_id = '$bfid'";

答案 1 :(得分:0)

使用以下代码:

$mysqli = new mysqli("127.0.0.1", "database_id", "database_password", "database_name");

/* check connection */
if ($mysqli->connect_errno) 
{
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}

$query = $mysqli->prepare("SELECT food_id,f_name,price FROM food_details WHERE food_id = ?");

$query->bind_param('s',$bfid);

if (!$query->execute()){
    echo $mysqli->error;
}

$query->bind_result($food_id,$f_name,$price);

$query->store_result();
$query->fetch();

注1:准备好的陈述更安全。

注2:如果bfid为整数,则可以将'​​s'替换为'i'。

答案 2 :(得分:0)

  

mysqli_fetch_array()函数将结果行作为关联数组

    $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
    $bfid =$row["food_id"];
    $bf=$row["f_name"];
    $bprice= $row["price"];