php mysql_query() 期望参数 2 是资源,字符串中给出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11646087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
mysql_query() expects parameter 2 to be resource, string given in
提问by user1550195
Possible Duplicate:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in select
what's wrong with this line?
这条线有什么问题?
23. $result = mysql_query("INSERT INTO $tbl_name('city_id', 'city', 'state_id') VALUES('NULL, '.$city.","', '4421'') or die(mysql_error())");
I get this error message each time :
我每次都会收到此错误消息:
Warning: mysql_query() expects parameter 2 to be resource, string given in line 23
警告:mysql_query() 期望参数 2 为资源,第 23 行给出的字符串
This my full code
这是我的完整代码
<?php
// Get values from form
$city = explode(',', $_POST['city']);
//create a loop
if(isset($_POST['city'])) {
$city = explode(',', $_POST['city']);
$n = count($city);
for($i=0; $i<$n; $i++) {
$result = mysql_query("INSERT INTO $tbl_name(city_id, city, state_id) VALUES(NULL, '.$city.","', '4421')") or die(mysql_error());
}
} if($result) {
header('Location: index.html');
}
?>
回答by 000
try mysql_query("INSERT INTO ".$tbl_name."(city_id, city, state_id) VALUES(NULL,'".$city."','4421')") or die(mysql_error());
尝试 mysql_query("INSERT INTO ".$tbl_name."(city_id, city, state_id) VALUES(NULL,'".$city."','4421')") or die(mysql_error());
i guess it should work this way..
我想它应该这样工作..
回答by Muhammad Raheel
Your query shoul be like this
你的查询应该是这样的
mysql_query("INSERT INTO $tbl_name(city_id, city, state_id) VALUES('NULL, '.$city.","', '4421'") or die(mysql_error()));
Columns without commas
没有逗号的列
回答by Praveen Kumar Purushothaman
You forgot to give closing "after after '4421'')and the syntax is incorrect!
你忘记在"之后关闭after '4421'')并且语法不正确!
Replace:
代替:
'NULL, '.$city.","', '4421'') or die(mysql_error())");
With
和
'NULL, '.$city.","', '4421'')") or die(mysql_error());
回答by Joe Green
mysql_query("INSERT INTO $tbl_name(city_id, city, state_id) VALUES(NULL, ".$city.",'4421')") or die(mysql_error());
Try this.
尝试这个。
or die()is a php construct - you misplaced a bracket which meant it went inside the sql query.
or die()是一个 php 构造——你放错了一个括号,这意味着它进入了 sql 查询。
This is why your error thinks that mysql_query()has a second parameter.
这就是为什么你的错误认为它mysql_query()有第二个参数。
回答by Omesh
This is because of incorrect syntax and you column names in INSERT query should not be enclosed in quotes: try
这是因为语法不正确,并且 INSERT 查询中的列名不应包含在引号中:尝试
$result = mysql_query("INSERT INTO ".$tbl_name."(city_id, city, state_id) VALUES(NULL, '".$city."','4421') or die(mysql_error()";

