繁体   English   中英

无法在简单的php-mysql查询中找到错误

[英]not able to find the error in a simple php-mysql query

在这个简单的代码中,我必须从具有fname列和其他一些列的表人员中检索,我无法通过匹配fname进行检索。 代码:->

    <!DOCTYPE html>
<html>
<title>fname searched!!!</title>
<body>

<div id="container" style="width:1500px"> <! div for main header ... orange portion>

<div id="header" style="background-color:#FFA500;">
<h1 style="margin-bottom:0;">LIST OF SEARCHED ACTIVE SERVICE PERSONNEL</h1>
<figure>
  <img src="personnel_head.jpg" alt="botg" width="1400" height="250">
  <figcaption>.....tracking records......</figcaption>
</figure>

</div>

<div id="menu" style="background-color:#FFD700;height:1050px;width:200px;float:left;"> 
                                            <! div for left side menus ... yellow portion>
candidate_reg_form<br>
view enrolled persons<br>
list of ammunitions<br>
character_certificate<br>
list_dependents<br>
regiments<br>
current serving general<br>
<b>SEARCH<b><br>

</div>

                                            <! div for main container ... grey portion>

<div id="content" style="background-color:#EEEEEE;height:1050px;width:1300px;float:left;">


<BODY>

    <fieldset>
<h1>active service personnel's details=>></h1>
<?php
$con=mysqli_connect("localhost","sumit","","ind_army");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
 $fname2=$_POST['fname2'];  
  echo"<br>";
  $result = mysqli_query($con,"SELECT * FROM 'personnel' WHERE fname=$fname2");

echo "<table border='1'>
<tr>
<th>FNAME</th>
<th>MNAME</th>
<th>LNAME</th>
<th>SERV_NO</th>
<th>SEX</th>
<th>RANK</th>
<th>BDATE&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp</th>
<th>R_NAME</th>
<th>SALARY</th>
<th>ADDRESS</th>
<th>SUPER_SERV_NO</th>
</tr>";

    while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['fname'] . "</td>";
  echo "<td>" . $row['mname'] . "</td>";
  echo "<td>" . $row['lname'] . "</td>";
  echo "<td>" . $row['SERV_NO'] . "</td>";
  echo "<td>" . $row['sex'] . "</td>"; 
  echo "<td>" . $row['RANK'] . "</td>";
 echo "<td>" . $row['bdate'] ."</td>";
 echo "<td>" . $row['R_NAME'] . "</td>";
 echo "<td>" . $row['salary'] . "</td>";
 echo "<td>" . $row['address'] . "</td>"; 
  echo "<td>" . $row['super_serv_no'] . "</td>";
 echo "</tr>";
  }
echo "</table>";
mysqli_close($con);

?>

<br><br>
<font color="orange">enter the service no for knowing his/her service    `    `details</font>
<!-- form to get key detail of personnel record in database -->
<form name="form" method="POST" action="http://localhost/search_per2.php">
  serv_no<input type="text" name="search"> <br><br>
  <input type="submit"  value="submit">
</form>


<font color="blue">enter the service no for knowing his/her depedents</font>
<!-- form to get key detail of dependent record in database -->
<form name="form" method="POST" action="http://localhost/dep_search.php">
  serv_no<input type="text" name="search2"> <br><br>
  <input type="submit"  value="submit">
</form>


<LABEL>
<form action="http://localhost/indarmy.php" method="post" >
<form>
        <input type="SUBMIT" name="HOME" VALUE="HOME"></label> 
</FORM>
</FIELDSET>



<div id="footer" style="background-color:#FFA500;clear:both;text-align:center;">
Copyright © indarmy.com</div>
</div>

</body>
</html>

它给出的错误是:->警告:mysqli_fetch_array()期望参数1为mysqli_result,在66行的C:\\ xampp \\ htdocs \\ search_fname.php中给出布尔值

更改

while($row = mysqli_fetch_array($result))

至:

while($row = mysqli_fetch_assoc($result))

如果循环获取结果作为数组你通过数组有循环,而得到行作为关联数组,你将能够引用列,你在你的循环,现在做的事情。

要查看发生了什么错误:

if(!$result)
{
 echo "error: ".mysqli_error($con);
}

在查询执行之后。

这可能是SQL中的东西。

还考虑mysqli_error_list

您认为可以肯定尝试的一种方法是,通过在搜索字符串$ fname周围添加引号来清理查询。 我很确定不能使用单引号,所以我用反引号代替了它们。

 $result = mysqli_query($con,"SELECT * FROM `personnel` WHERE fname = '$fname2' ");

尝试如下查询:

$result = mysqli_query($con,"SELECT * FROM `personnel` WHERE fname='$fname2'");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM