简体   繁体   中英

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

in this siomple code where i have to retrieve from table personnel which is having a column fname and some other columns as well , im not able to retrieve by matching fname. code:->

    <!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>

and the error it is giving is:--> Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\\xampp\\htdocs\\search_fname.php on line 66

Change

while($row = mysqli_fetch_array($result))

to:

while($row = mysqli_fetch_assoc($result))

If you loop fetch the result as an array you have loop through the array, whereas getting the row as associated array you will be able to reference the columns as you are doing now in your loop.

To see what's the error put:

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

right after the query execution.

It's probably something in the SQL.

Consider also mysqli_error_list

One you think you can definitely try is cleaning up your query just a tad bit by adding quotes around your search string $fname. Im pretty sure cant use single quotes, so i replaced those with backticks.

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

尝试如下查询:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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