mysql_error() 期望参数 1 为资源,给定的字符串
mysql_error() expects parameter 1 to be resource, string given
我正在编写用于民意调查的代码。管理员可以创建问题并删除它们。用户可以从下拉菜单中选择答案来回答问题。此下拉菜单由查询请求动态填充。
现在我希望用户在完成投票后将其姓名插入数据库,这样他就不能进行两次投票。这就是这里的代码的用途。
这是我正在使用的查询:
$idfind = "SELECT `id` FROM `Umfragenteilnahme` WHERE `Username` ='$user'";
$id = mysql_query($idfind) or die (mysql_error());
if($id) {
while($rpw = mysql_fetch_assoc($id)) {
$id = $rpw['id']; // Using $rpw->id doesnt work
echo $id; // it gives the right answer and everything works fine
//id= 4 for example
}
}
else {
echo "Fehler"; // it does not report a fail
}
我知道我应该使用新的 mysqli_...
标签。该代码工作得非常好,查询给出了正确的答案,并且在 MySql 数据库中也能工作。
但是每次都报这个错误
mysql_fetch_assoc() expects parameter 1 to be resource, string given in
或 (mysql_error())
这个:
mysql_error() expects parameter 1 to be resource, string given in.
一切正常,但显示这些错误。为什么会发生这些错误?我怎样才能避免它们?
编辑:
现在我把它放在第一个 <?php
:
之后的代码开头
set_error_handler ( function($errno, $errstr, $errfile, $errline) {
if (false!==stripos($errstr, 'mysql')) {
$file = file($errfile);
$code = '';
for($i=max(0, $errline-8); $i<min($errline+8, count($file)); $i++) {
$code .= sprintf('%4d | %s', $i+1, $file[$i]);
}
printf('<fieldset><legend>%s (%d), %s@%d</legend>
<pre>%s</pre></fieldset>',
htmlspecialchars($errstr), $errno,
htmlspecialchars($errfile), $errline,
htmlspecialchars( $code )
);
}
else {
return false;
}});
输出是这样的:
Benutzer id: 4
mysql_fetch_assoc() expects parameter 1 to be resource, string given (2), /censored/censored/www/Website2.0/btn_lehrer.php@36
29 | /* The $user is defined at start of code */
30 | /* With the next query i get the user-id from this specific user */
31 |
32 | $idfind = "SELECT `id` FROM `Umfragenteilnahme` WHERE `Username` = '$user'";
33 | $id = mysql_query($idfind)or die (mysql_error());
34 | if($id){
35 |
36 | while($rpw = mysql_fetch_assoc($id)){
37 | $id = $rpw['id'];
38 | echo "Benutzer id: $id "; //fehler (Warning: mysql_fetch_assoc() expects parameter 1 to be resource, string given line 15)
39 | }
40 |
41 | }
42 | else{
43 | echo "Fehler bei der Bestimmung der Benutzer id!";
44 | }
我希望你不介意花了这么长时间,但正如我所说,我完全重组了我的代码。
感谢到目前为止的所有帮助,也许现在有人可以解决我的问题。 :)
您的回答实际上是您的错误信息。你给函数 mysq_error
你的 SQL 查询是一个字符串。如 here 所述,该函数将资源作为参数。资源应该是函数 mysql_connect
returns.
此外,如果您不向 mysql_error
提供任何参数,它将使用上次打开的 link,如 here 所述。删除参数 $idfind
很可能会解决问题。
还没有答案:
你能把
set_error_handler ( function($errno, $errstr, $errfile, $errline) {
if (false!==stripos($errstr, 'mysql')) {
$file = file($errfile);
$code = '';
for($i=max(0, $errline-8); $i<min($errline+8, count($file)); $i++) {
$code .= sprintf('%4d | %s', $i+1, $file[$i]);
}
printf('<fieldset><legend>%s (%d), %s@%d</legend>
<pre>%s</pre></fieldset>',
htmlspecialchars($errstr), $errno,
htmlspecialchars($errfile), $errline,
htmlspecialchars( $code )
);
}
else {
return false;
}
});
它在任何 mysql_* 函数之前执行的某个地方 - 在第一个 <?php
之后的主脚本顶部就可以了 - 然后是 add the output to your question?
错误处理程序应该为所有 mysql 相关的 warnings/errors 打印 "surrounding code" 并且(原始)输出应该看起来像
<fieldset><legend>mysql_fetch_assoc() expects parameter 1 to be resource, string given (2), ...test.php@21</legend>
<pre> 14 | );
15 | }
16 | else {
17 | return false;
18 | }
19 | });
20 |
21 | mysql_fetch_assoc('foo');
22 |
</pre></fieldset>
您正在循环中为 $id 分配一个字符串,$id = $rpw['id'];
。下次执行 mysql_fetch_assoc($id) 时,$id 是一个字符串,因此会出现警告消息。
while($rpw = mysql_fetch_assoc($id)) {
echo "Benutzer id: ", $rpw['id'], "\r\n";
}
我正在编写用于民意调查的代码。管理员可以创建问题并删除它们。用户可以从下拉菜单中选择答案来回答问题。此下拉菜单由查询请求动态填充。
现在我希望用户在完成投票后将其姓名插入数据库,这样他就不能进行两次投票。这就是这里的代码的用途。 这是我正在使用的查询:
$idfind = "SELECT `id` FROM `Umfragenteilnahme` WHERE `Username` ='$user'";
$id = mysql_query($idfind) or die (mysql_error());
if($id) {
while($rpw = mysql_fetch_assoc($id)) {
$id = $rpw['id']; // Using $rpw->id doesnt work
echo $id; // it gives the right answer and everything works fine
//id= 4 for example
}
}
else {
echo "Fehler"; // it does not report a fail
}
我知道我应该使用新的 mysqli_...
标签。该代码工作得非常好,查询给出了正确的答案,并且在 MySql 数据库中也能工作。
但是每次都报这个错误
mysql_fetch_assoc() expects parameter 1 to be resource, string given in
或 (mysql_error())
这个:
mysql_error() expects parameter 1 to be resource, string given in.
一切正常,但显示这些错误。为什么会发生这些错误?我怎样才能避免它们?
编辑:
现在我把它放在第一个 <?php
:
set_error_handler ( function($errno, $errstr, $errfile, $errline) {
if (false!==stripos($errstr, 'mysql')) {
$file = file($errfile);
$code = '';
for($i=max(0, $errline-8); $i<min($errline+8, count($file)); $i++) {
$code .= sprintf('%4d | %s', $i+1, $file[$i]);
}
printf('<fieldset><legend>%s (%d), %s@%d</legend>
<pre>%s</pre></fieldset>',
htmlspecialchars($errstr), $errno,
htmlspecialchars($errfile), $errline,
htmlspecialchars( $code )
);
}
else {
return false;
}});
输出是这样的:
Benutzer id: 4
mysql_fetch_assoc() expects parameter 1 to be resource, string given (2), /censored/censored/www/Website2.0/btn_lehrer.php@36
29 | /* The $user is defined at start of code */
30 | /* With the next query i get the user-id from this specific user */
31 |
32 | $idfind = "SELECT `id` FROM `Umfragenteilnahme` WHERE `Username` = '$user'";
33 | $id = mysql_query($idfind)or die (mysql_error());
34 | if($id){
35 |
36 | while($rpw = mysql_fetch_assoc($id)){
37 | $id = $rpw['id'];
38 | echo "Benutzer id: $id "; //fehler (Warning: mysql_fetch_assoc() expects parameter 1 to be resource, string given line 15)
39 | }
40 |
41 | }
42 | else{
43 | echo "Fehler bei der Bestimmung der Benutzer id!";
44 | }
我希望你不介意花了这么长时间,但正如我所说,我完全重组了我的代码。 感谢到目前为止的所有帮助,也许现在有人可以解决我的问题。 :)
您的回答实际上是您的错误信息。你给函数 mysq_error
你的 SQL 查询是一个字符串。如 here 所述,该函数将资源作为参数。资源应该是函数 mysql_connect
returns.
此外,如果您不向 mysql_error
提供任何参数,它将使用上次打开的 link,如 here 所述。删除参数 $idfind
很可能会解决问题。
还没有答案:
你能把
set_error_handler ( function($errno, $errstr, $errfile, $errline) {
if (false!==stripos($errstr, 'mysql')) {
$file = file($errfile);
$code = '';
for($i=max(0, $errline-8); $i<min($errline+8, count($file)); $i++) {
$code .= sprintf('%4d | %s', $i+1, $file[$i]);
}
printf('<fieldset><legend>%s (%d), %s@%d</legend>
<pre>%s</pre></fieldset>',
htmlspecialchars($errstr), $errno,
htmlspecialchars($errfile), $errline,
htmlspecialchars( $code )
);
}
else {
return false;
}
});
它在任何 mysql_* 函数之前执行的某个地方 - 在第一个 <?php
之后的主脚本顶部就可以了 - 然后是 add the output to your question?
错误处理程序应该为所有 mysql 相关的 warnings/errors 打印 "surrounding code" 并且(原始)输出应该看起来像
<fieldset><legend>mysql_fetch_assoc() expects parameter 1 to be resource, string given (2), ...test.php@21</legend>
<pre> 14 | );
15 | }
16 | else {
17 | return false;
18 | }
19 | });
20 |
21 | mysql_fetch_assoc('foo');
22 |
</pre></fieldset>
您正在循环中为 $id 分配一个字符串,$id = $rpw['id'];
。下次执行 mysql_fetch_assoc($id) 时,$id 是一个字符串,因此会出现警告消息。
while($rpw = mysql_fetch_assoc($id)) {
echo "Benutzer id: ", $rpw['id'], "\r\n";
}