2016-07-06 44 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)); 
相關問題