繁体   English   中英

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

[英]How to fix Warning: mysqli_query() expects parameter 2 to be string

我需要回应所有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>

如何解决错误?

您必须删除第二个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>

注: -虽然经过elsewhile在上述情况下工作,但如果在你的查询失败任何情况下,你会得到很多的undefined indexes误差。 谢谢

您可以删除

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

$sql重命名为$result

原因:您正在尝试将第一个mysqli_query生成的资源关联到第二个mysqli_query调用。 mysqli_query的第二次调用中,第二个参数不是字符串,而是从第一次调用返回的资源。

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

使查询整洁。 尝试:

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

$conn = connection in your DB file.

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

并尝试从$ result获取数组。

使代码优化,更好地使用

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

暂无
暂无

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

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