PHP和MySQL警告问题帮助?
我一直在尝试将我的脚本从mysql转换为mysqli,并在此过程中出现了下面列出的以下错误。PHP和MySQL警告问题帮助?
Warning: mysqli_query() expects parameter 1 to be mysqli, string given in on line 41
Warning: mysqli_error() expects exactly 1 parameter, 0 given in on line 43
以下是下面的完整代码。
<?php
require_once ('./mysqli_connect.php'); // Connect to the db.
if (isset($_POST['submitted'])) {
// Query member data from the database and ready it for display
$mysqli = new mysqli("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"SELECT * FROM tags");
if (!$dbc) {
// There was an error...do something about it here...
print mysqli_error($mysqli);
}
// grab the tag
$tag = mysqli_real_escape_string($mysqli, $_POST['tag']);
// see if the tag already exists and potentially what the current count is
$mysqli = new mysqli("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"SELECT id, count, page FROM tags WHERE tag='$tag'");
//--if there is a row, that means the tag exists
if(mysqli_num_rows($dbc))
{
//--pull out the tag ID and the current count and increment by one.
$tag_info = mysqli_fetch_array($result);
$tag_info_id = $tag_info["id"];
$tag_info_count = $tag_info["count"] + 1;
//--update the table with the new count
$sql_update_cnt = "UPDATE tags SET count='$tag_info_count'
WHERE id='$tag_info_id'";
mysqli_query($sql_update_cnt);
echo "$tag now with $tag_info_count instances";
}
else
{
// tag is not there, so insert a new instance
$query = "INSERT INTO tags (tag, count) VALUES ('$tag', 1)";
if (!mysqli_query($query, $dbc))
{
die('Error: ' . mysqli_error());
}
echo "1 record added";
}
mysqli_close($dbc);
}
?>
你对它的调用不正确。
应该$mysqli->query("SELECT * FROM tags");
$ mysqli的是对象和查询的是,对象上的方法。它只需要你传入查询。显然,所有对mysqi_query的调用都需要进行更改以匹配。
他使用的是程序API,而不是OO。 – 2009-12-02 19:10:54
因此,他应该使用mysqli_connect – 2009-12-02 19:16:00
我可以建议你向后退一步,并使用mysqli的包装?我最近几个月一直在使用Rob Poyntz codesense_mysqli封装,我发现它非常适合减少mysqli中大量的垃圾,否则它会转储到您的代码中。
当然很好地知道引擎盖下发生了什么,但有时只需要最简洁的方法让汽车工作。
请不要在第一个问题仍然打开时打开第二个问题:http://*.com/questions/1834834/need-help-in-converting-mysql-to-mysqli-in-my-php-脚本 – 2009-12-02 19:12:13
正是我刚才说的。 – 2009-12-02 19:15:28
他们是两个不同的问题,最后我检查了另一个被检查。 – bot 2009-12-02 19:18:21