如何修复警告:mysqli_query()期望参数2为字符串

时间:2016-07-06 04:48:27

标签: php mysql arrays html5 while-loop

我需要回应job_id 1的所有经历, 当我执行我的代码时,它会出现以下错误

  

警告: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>

如何修复错误?

5 个答案:

答案 0 :(得分:2)

您必须删除第二个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在上述情况下{{1}}工作,但无论如何您的查询失败,您将获得大量{{1}}错误。感谢

答案 1 :(得分:1)

您可以删除

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

将您的$sql重命名为$result

原因: 您正试图通过第一次mysqli_query向第二次mysqli_query电话分配所产生的资源。在mysqli_query的第二次调用中,第二个参数不是字符串,而是从第一次调用返回的资源。

答案 2 :(得分: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>

答案 3 :(得分:0)

使查询整洁。 试试:

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

$conn = connection in your DB file.

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

尝试从$ result获取数组。

答案 4 :(得分:0)

使代码优化,更好地使用

mysqli_query($query) or die('Error in Query'.mysqli_error($con));