简体   繁体   中英

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

I need to echo all the experiences of job_id 1 , when I execute my code it gives following error

Warning: mysqli_query() expects parameter 2 to be string,

here is my code,

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

How do I fix the error?

You have to remove second mysqli_query() and put your while code inside if like below:-

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

Note:- although after else your while is worked in above case, but if in any case your query failed, you will get lot of undefined indexes error. Thanks

You can remove

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

Rename your $sql to $result .

Reason: You are trying to assing the resource genetated by first mysqli_query to your second mysqli_query call. In the second call of mysqli_query the second parameter is not a string but resource returned from your first call.

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

Make the query neat. Try:

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

$conn = connection in your DB file.

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

And try to fetch the array from $result.

使代码优化,更好地使用

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

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