[英]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 我需要回应所有
job_id 1
的经验,当我执行我的代码时,它给出以下错误
Warning: mysqli_query() expects parameter 2 to be string,
警告:mysqli_query()期望参数2为字符串,
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:- 您必须删除第二个
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>
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. 注: -虽然经过
else
的while
在上述情况下工作,但如果在你的查询失败任何情况下,你会得到很多的undefined indexes
误差。 Thanks 谢谢
You can remove 您可以删除
$result=mysqli_query($con,$sql);
Rename your $sql
to $result
. 将
$sql
重命名为$result
。
Reason: You are trying to assing the resource genetated by first mysqli_query
to your second mysqli_query
call. 原因:您正在尝试将第一个
mysqli_query
生成的资源关联到第二个mysqli_query
调用。 In the second call of mysqli_query
the second parameter is not a string but resource returned from your first call. 在
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>
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. 并尝试从$ result获取数组。
使代码优化,更好地使用
mysqli_query($query) or die('Error in Query'.mysqli_error($con));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.