如何在这里使用mysqli_num_rows? [英] How to use mysqli_num_rows here?

查看:66
本文介绍了如何在这里使用mysqli_num_rows?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下查询,只有在我不使用mysqli_num_rows($ stmt)时才能将其插入数据库;出于某种原因,当我使用它时,它会说布尔错误,但我的查询看起来是正确的:



I have the following query and I can get it to insert into the database only if I don't use mysqli_num_rows($stmt); For some reason, when I am using it, it says something about boolean error but my query looks correct:

<pre><?php

include_once 'includes/dbh.php';
include_once 'header.php';

  $sql = "SELECT * FROM users WHERE user_uid = ?;";

  $stmt = mysqli_stmt_init($conn);
   if (!mysqli_stmt_prepare($stmt, $sql)) {
        echo 'SQL statement failed';
    } else {
         //Bind parameters to the placeholder
         mysqli_stmt_bind_param($stmt, "s", $_SESSION['u_uid']);
         //Run parameters inside database
         mysqli_stmt_execute($stmt);
         $result = mysqli_stmt_get_result($stmt);
         $row = mysqli_fetch_assoc($result);


         if ($row['admin'] == 0) {
             header("Location: header.php?add=notadmin");
             exit;
       } else {

   if (isset($_POST['submit'])) {

   
   
   $question_number = $_POST['question_number'];
   $question_text = $_POST['question_text'];
   $correct_choice = $_POST['correct_choice'];
   //Choices array
   $choices = array();
   $choices[1] = $_POST['choice1'];
   $choices[2] = $_POST['choice2'];
   $choices[3] = $_POST['choice3'];
   $choices[4] = $_POST['choice4'];



  $sql2 = "INSERT INTO questions (question_number, `text`) VALUES (?,?);";

  $stmt = mysqli_stmt_init($conn);
    //Prepare the prepared statement
    if (!mysqli_stmt_prepare($stmt, $sql2)) {
        echo 'SQL statement failed';
    } else {
         mysqli_stmt_bind_param($stmt, "is", $question_number, $question_text);
         $result = mysqli_stmt_execute($stmt);
    //     $result2 = mysqli_stmt_get_result($stmt);
         
    //   $resultCheck2 = mysqli_num_rows($result2);

        if ($result) {

 foreach($choices as $choice => $value) {

    if ($value != '') {
       if ($correct_choice == $choice) {
           $is_correct = 1;
     } else {
          $is_correct = 0;
     }

    $sql3 = "INSERT INTO choices (question_number, is_correct, `text`) VALUES (?,?,?);";

    $stmt = mysqli_stmt_init($conn);
    //Prepare the prepared statement
    if (!mysqli_stmt_prepare($stmt, $sql3)) {
        echo 'SQL statement failed';
    } else {
         mysqli_stmt_bind_param($stmt, "iis", $question_number, $is_correct, $value);
         mysqli_stmt_execute($stmt);
  }


}

  } 
 } else {
     echo "There are no results";
}
 } 
}
}
}













我尝试过:



我曾尝试使用mysqli_num_rows查看它是否确实返回数据库中的任何行但由于某种原因,它只是无法将其插入数据库....我只是想知道..如果数据库中没有自动增量,你可以使用num_rows吗?







What I have tried:

I have tried to use mysqli_num_rows to see if it does return any row in the database but for some reason, it just can't insert it into the database.... I am just wondering... can you use num_rows if you don't have auto increment in the database?

推荐答案

stmt);出于某种原因,当我使用它时,它会说布尔错误,但我的查询看起来是正确的:



stmt); For some reason, when I am using it, it says something about boolean error but my query looks correct:

<pre><?php

include_once 'includes/dbh.php';
include_once 'header.php';

  


sql = SELECT * FROM users WHERE user_uid =?;;

sql = "SELECT * FROM users WHERE user_uid = ?;";


stmt = mysqli_stmt_init(
stmt = mysqli_stmt_init(


这篇关于如何在这里使用mysqli_num_rows?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆