我一直在尝试将我的脚本从mysql转换为mysqli,在此过程中我收到了以下错误。
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);
}
?>
答案 0 :(得分:0)
应为$mysqli->query("SELECT * FROM tags");
$ mysqli是对象,查询是该对象的方法。它只需要您传入查询。显然,所有对mysqi_query的调用都需要更改为匹配。
答案 1 :(得分:0)
我建议你向后退一步并使用mysqli的包装器吗?最近几个月我一直在使用Rob Poyntz codesense_mysqli封装器,我发现它非常适合减少mysqli以其他方式转储到代码中的绝对数量。
当然,知道幕后发生的事情是很好的,但有时人们只需要最干净的方法让汽车上班。