【问题标题】:how to print the fetched value in mysqli prepared statement?如何在 mysqli 准备好的语句中打印获取的值?
【发布时间】:2016-03-14 11:40:42
【问题描述】:

我的 html 表单:

<form action="" method="post" > 
<tr> 
<td><select name="bs_edu">
<option value="" selected="selected">Select Basic Education</option>   
<option value="BA">B.A</option>
<option value="BARCH">B.ARCH</option>
<option value="BCA">BCA</option>
<option value="BBA">BBA</option> 
</select>
<td><input type="text" name="desg"/> </td>
<td><input type="email" name="email"/></td>
</tr>
</form>

我的查询:

<?php
    $stmt = $con->prepare("SELECT * FROM user_tbl WHERE bs_edu = ? AND desg = ? AND exp = ? AND pr_sal = ? AND plc = ? AND email = ?  ");
    $stmt->bind_param('ssssss', $_POST['bs_edu'], $_POST['desg'], $_POST['exp'], $_POST['prsal'], $_POST['place'], $_POST['email']);
    $stmt->execute();
    $result = $stmt->get_result();
    if ($result->num_rows > "0")
    {
        while($row = $result->fetch_assoc());
        {
            echo 'first name: ' .$row['fname'];  
        }
    }
    $stmt->close(); 
?>

我知道这是显示数据的错误方式。阅读本手册http://php.net/manual/en/mysqli-stmt.fetch.php n 真的运气不好,请展示我如何显示获取的数据..这里有任何帮助..谢谢

【问题讨论】:

  • 你为什么觉得不对?
  • bcz 我不认为这是正确的方法,n m 没有从这个得到任何结果
  • 问题是 ;就在while循环之后;
  • 表单中也缺少两个表单域($_POST['exp'], $_POST['prsal']);
  • @itzmukeshy7 您的待编辑 stackoverflow.com/review/suggested-edits/11623946 您正在将 OP 的“代码”从 if ($result-&gt;num_rows &gt; "0") 修改为 if ($result-&gt;num_rows){为什么??请不要这样做!你不应该修改“代码”。我拒绝了编辑。如果它确实通过了,我将对其进行回滚。

标签: php mysqli prepared-statement


【解决方案1】:

下面的分号使你的循环停止并且是语句结束字符。

while($row = $result->fetch_assoc());
                                    ^ end of statement

删除它。

while($row = $result->fetch_assoc())

你不会得到一个错误,因为它被认为是有效的语法。

引用的零也被解释为字符串,因此您需要将其删除。

if ($result->num_rows > 0)

关于get_result()的旁注。

手册http://php.net/manual/en/mysqli-stmt.get-result.php 指出:

仅限 MySQL 原生驱动程序

仅适用于mysqlnd

从这个答案中提取的示例https://stackoverflow.com/a/24985955/

if ($stmt = $mysqli->prepare("SELECT id, community, map, image FROM `googleMaps`")) {
    $stmt->execute();
    $stmt->bind_result($id, $community, $map, $image);
    while ($stmt->fetch()) {
            printf("%s %s\n", $title);
    }
    $stmt->close();
}

补充说明。

&lt;form&gt; 不能是&lt;table&gt; 的子级,因为我怀疑您可能在未显示的代码中隐藏了&lt;table&gt;&lt;/table&gt; 标签。

您也没有与您的 POST 数组匹配的表单元素。

在打开 PHP 标记后立即将错误报告添加到文件顶部 例如
&lt;?php error_reporting(E_ALL); ini_set('display_errors', 1); 然后你的代码的其余部分,看看它是否产生任何东西, 以及or die(mysqli_error($con))mysqli_query()

或者另一种方法是改变:

$stmt->execute();

到:

if(!$stmt->execute()){
   trigger_error("There was an error.... ".$con->error, E_USER_WARNING);
}

编辑:

借用这个答案https://stackoverflow.com/a/22899155/

$stmt = $con->prepare("SELECT * FROM user_tbl ... ");

$result = $stmt->execute(); 
$stmt->store_result();

if ($stmt->num_rows > 1) {

  while($row = $result->fetch_assoc()){ 

  echo 'First name: ' .$row['fname'];

  }

}else{

   echo "0 records found";
}

另外,您似乎还在同一个文件中使用 HTML 表单和 PHP/MySQL。

您需要对 POST 数组使用 isset()!empty()

您也没有包含提交按钮,并且不知道您是否正在使用。

对于我已经说过的关于缺少具有匹配名称属性的表单元素的内容相同:

  • $_POST['exp']
  • $_POST['prsal']
  • $_POST['place']

【讨论】:

  • 但我没有得到任何输出..我删除了分号
  • @StillLearning get_result() 正如我在回答中所说,它只是一个 MySQL 本机驱动程序,您可能没有在您的服务器上安装它。这个答案是另一种stackoverflow.com/a/24985955(见第二种方法)。
  • @StillLearning 你确实也看到了删除的引号if ($result-&gt;num_rows &gt; 0) 对吧?
  • @StillLearning 好的...好吧,您的 HTML 表单没有 POST 数组的名称属性,所以我不知道您打算在哪里使用这些属性。使用错误报告 php.net/manual/en/function.error-reporting.php 并检查查询中的错误 php.net/manual/en/mysqli.error.php 添加到我的答案中
  • @StillLearning 重新加载我的答案,因为我做了一些编辑。另请参阅 Edit: 下的另一种略有不同的方法。我还添加了检查错误的方法,因此您需要检查可能缺少的表单元素/名称属性以及&lt;form&gt; 不能是&lt;table&gt; 的“子”,并且在您的问题中未显示。这是我能做的最好的了。
猜你喜欢
  • 2012-12-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-11
  • 1970-01-01
  • 1970-01-01
  • 2012-01-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多