2016-07-06 46 views
0

我需要呼应job_id 1, 所有的经验,当我执行我的代码,它提供了以下错误如何解决警告:mysqli_query()预计参数2为字符串

警告:mysqli_query()预计参数2是字符串,

这里是我的代码,

<?php include_once 'db.php'; ?> 

<form action='update.php' method='post'> 
    <table border='1'> 
     <?php 
      $sql = mysqli_query($con,"SELECT *FROM `experience` WHERE job_id=1"); 

      $result= mysqli_query($con,$sql); 
      if ($result) { 
       // The query was successful! 
      } 
      else { 
       // The query failed. Show an error or something. 
      } 
      while($row = mysqli_fetch_array($result)){ 
       echo "<tr>"; 
       echo "<td><input type='hidden' name='experi_id[]' value='".$row['exp_id']."' /></td>"; 
       echo "<td>Experince :<input type='text' name='experi[]' value='".$row['experience']."' /></td>"; 
       echo "<td>year :<input type='text' name='year[]' value='".$row['year']."' /></td>"; 
       echo "<td>job id :<input type='text' name='job_id[]' value='".$row['job_id']."' /></td>"; 
       echo "</tr>"; 
      } 
      echo "<input type='submit' name='update' value='UPDATE' />"; 
      mysqli_close($con); 
     ?> 
    <table> 
</form> 

如何解决这个错误吗?

+1

我无法看到你的代码 –

回答

0

您可以删除

$result=mysqli_query($con,$sql);

重命名$sql$result

原因: 您正在尝试将第一个mysqli_query创建的资源分配给您的第二个mysqli_query调用。在第二次调用mysqli_query时,第二个参数不是一个字符串,而是您第一次调用返回的资源。

+0

是的,它是工作非常感谢你:) – Deepashika

+0

都好.... :) – Deepak

+1

@ Deepashika如果您的查询在任何时候都失败会发生什么。你会遇到很大的麻烦,因为'if'else'外面有'while'。因此,如果''里面有'while',我建议 –

0
<?php include_once 'db.php'; ?> 
<form action='update.php' method='post'> 
    <table border='1'> 
    <?php $sql=mysqli_query($con, "SELECT *FROM `experience` WHERE job_id=1"); if ($sql) { // The query was successful! } else { // The query failed. Show an error or something. } while($row=mysqli_fetch_array($result)){ 
    echo "<tr>"; echo "<td><input type='hidden' name='experi_id[]' value='".$row[ 'exp_id']. "' /></td>"; echo "<td>Experince :<input type='text' name='experi[]' value='".$row[ 'experience']. "' /></td>"; echo 
    "<td>year :<input type='text' name='year[]' value='".$row[ 'year']. "' /></td>"; echo "<td>job id :<input type='text' name='job_id[]' value='".$row[ 'job_id']. "' /></td>"; echo "</tr>"; } echo "<input type='submit' name='update' value='UPDATE' />"; mysqli_close($con); ?> 
    <table> 
</form> 
0

使查询整洁。 尝试:

$sql = "SELECT *FROM `experience` WHERE job_id=1"; 

$conn = connection in your DB file. 

$result = $conn->query($createquery); 

并尝试获取从$结果阵列。

2

您必须删除第二mysqli_query(),把你的while代码中if象下面这样: -

<form action='update.php' method='post'> 
    <table border='1'> 
     <?php 
      $result = mysqli_query($con,"SELECT *FROM `experience` WHERE job_id=1"); 

       //$result= mysqli_query($con,$sql); // since query is done already in previous statement so not needed second time 
      if ($result) { 

       while($row = mysqli_fetch_array($result)){ 

        echo "<tr>"; 
         echo "<td><input type='hidden' name='experi_id[]' value='".$row['exp_id']."' /></td>"; 
         echo "<td>Experince :<input type='text' name='experi[]' value='".$row['experience']."' /></td>"; 
         echo "<td>year :<input type='text' name='year[]' value='".$row['year']."' /></td>"; 
         echo "<td>job id :<input type='text' name='job_id[]' value='".$row['job_id']."' /></td>"; 
        echo "</tr>"; 
       } 
       echo "<input type='submit' name='update' value='UPDATE' />"; 
      }else { 
       echo "following error occurred:-".mysqli_error($con); // check the exact error happen while query execution so that fix can be possible easily 
      }  
      mysqli_close($con); 
     ?> 
    <table> 
</form> 

注: - 虽然else后您的while在上述情况下工作,但如果在任何情况下,你查询失败,您将收到很多undefined indexes错误。由于

0

使代码优化,更好地使用

mysqli_query($query) or die('Error in Query'.mysqli_error($con)); 
相关问题