我正在尝试将下面的 mysql 更改为 mysqli(准备好的语句),但我陷入了困境。
问题: Warning: mysqli_stmt
::bind_result(): Number of bind variables doesn't match number of fields in prepared statement
这个错误发生在 $get_empty_field = $ok->bind_result($username, $firstname, $lastname); 线。我还没有完成转换,我只是做了我能做的,其余的保持原样。(我没有混合 mysql 和 mysqli)
mysql查询
$check_added_files = mysql_query("select * from `vpb_uploads` where `username` = '".mysql_real_escape_string($username)."' and `firstname` = '' and `image_one` != '' and `image_two` != '' and `image_three` != '' and `image_four` != '' and `image_five` != ''");
if(mysql_num_rows($check_added_files) == 1)
{
echo 'up_to_five_already';
}
else
{
if (move_uploaded_file($_FILES['file_to_upload']['tmp_name'], $final_uploads_location))
{
$check_empty_field = mysql_query("select * from `vpb_uploads` where `username` = '".mysql_real_escape_string(strip_tags($username))."' and `firstname` = '".mysql_real_escape_string("")."' and `lastname` = '".mysql_real_escape_string("")."'");
if(mysql_num_rows($check_empty_field) < 1)
{
mysql_query("insert into `vpb_uploads` values('', '".mysql_real_escape_string($username)."', '', '', '".mysql_real_escape_string($random_name_generated)."', '', '', '', '', '".mysql_real_escape_string(date("d-m-Y"))."')");
$identity = "image_one";
}
else
{
$get_empty_field = mysql_fetch_array($check_empty_field);
$image_one = strip_tags($get_empty_field["image_one"]);
$image_two = strip_tags($get_empty_field["image_two"]);
$image_three = strip_tags($get_empty_field["image_three"]);
$image_four = strip_tags($get_empty_field["image_four"]);
$image_five = strip_tags($get_empty_field["image_five"]);
global $identity;
if(empty($image_one))
{
mysql_query("update `vpb_uploads` set `image_one` = '".mysql_real_escape_string($random_name_generated)."' where `username` = '".mysql_real_escape_string(strip_tags($username))."' and `firstname` = '".mysql_real_escape_string("")."' and `lastname` = '".mysql_real_escape_string("")."'");
$identity = "image_one";
}
elseif(empty($image_two))
{
mysql_query("update `vpb_uploads` set `image_two` = '".mysql_real_escape_string($random_name_generated)."' where `username` = '".mysql_real_escape_string(strip_tags($username))."' and `firstname` = '".mysql_real_escape_string("")."' and `lastname` = '".mysql_real_escape_string("")."'");
$identity = "image_two";
}
elseif(empty($image_three))
{
mysql_query("update `vpb_uploads` set `image_three` = '".mysql_real_escape_string($random_name_generated)."' where `username` = '".mysql_real_escape_string(strip_tags($username))."' and `firstname` = '".mysql_real_escape_string("")."' and `lastname` = '".mysql_real_escape_string("")."'");
$identity = "image_three";
}
elseif(empty($image_four))
{
mysql_query("update `vpb_uploads` set `image_four` = '".mysql_real_escape_string($random_name_generated)."' where `username` = '".mysql_real_escape_string(strip_tags($username))."' and `firstname` = '".mysql_real_escape_string("")."' and `lastname` = '".mysql_real_escape_string("")."'");
$identity = "image_four";
}
elseif(empty($image_five))
{
mysql_query("update `vpb_uploads` set `image_five` = '".mysql_real_escape_string($random_name_generated)."' where `username` = '".mysql_real_escape_string(strip_tags($username))."' and `firstname` = '".mysql_real_escape_string("")."' and `lastname` = '".mysql_real_escape_string("")."'");
$identity = "image_five";
}
到目前为止我有什么:
$mysqli = new mysqli("localhost", "root", "", "newlogin");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
$stmt = $mysqli->prepare("select * from `vpb_uploads` where `username` = ? and `firstname` = '' and `image_one` != '' and `image_two` != '' and `image_three` != '' and `image_four` != '' and `image_five` != ''");
$stmt->bind_param('s', $username);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows == 1) {
echo 'up_to_five_already';
}
else
{
if (move_uploaded_file($_FILES['file_to_upload']['tmp_name'], $final_uploads_location))
{
$firstname = "''";
$lastname = "''";
$stmt = $mysqli->prepare("select * from `vpb_uploads` where `username` = ? and `firstname` = ? and `lastname` = ?");
$stmt->bind_param('sss', $username, $firstname, $lastname);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows < 1)
{
$date = 'date("d-m-Y")';
$image_2 = "''";
$image_3 = "''";
$image_4 = "''";
$image_5 = "''";
$stmt = $mysqli->prepare("insert into `vpb_uploads` (`username`, `firstname`, `lastname`, `image_one`, `image_two`, `image_three`, `image_four`, `image_five`, `date`) values(?,?,?,?,?,?,?,?,?)");
$stmt->bind_param('sssssssss', $username, $firstname, $lastname, $random_name_generated, $image_2, $image_3, $image_4, $image_5, $date);
$stmt->execute();
$identity = "image_one";
}
else
{
$get_empty_field = $stmt->bind_result($username, $firstname, $lastname);
$image_one = strip_tags($get_empty_field["image_one"]);
$image_two = strip_tags($get_empty_field["image_two"]);
$image_three = strip_tags($get_empty_field["image_three"]);
$image_four = strip_tags($get_empty_field["image_four"]);
$image_five = strip_tags($get_empty_field["image_five"]);
global $identity;
if(empty($image_one))
{
$stmt = $mysqli->prepare("update `vpb_uploads` set `image_one` = ? where `username` = ? and `firstname` = ? and `lastname` = ? ");
$stmt->bind_param('ssss', $random_name_generated, $username, $firstname, $lastname);
$stmt->execute();
$stmt->close();
$identity = "image_one";
}
elseif(empty($image_two))
{
mysql_query("update `vpb_uploads` set `image_two` = '".mysql_real_escape_string($random_name_generated)."' where `username` = '".mysql_real_escape_string(strip_tags($username))."' and `firstname` = '".mysql_real_escape_string("")."' and `lastname` = '".mysql_real_escape_string("")."'");
$identity = "image_two";
}
elseif(empty($image_three))
{
mysql_query("update `vpb_uploads` set `image_three` = '".mysql_real_escape_string($random_name_generated)."' where `username` = '".mysql_real_escape_string(strip_tags($username))."' and `firstname` = '".mysql_real_escape_string("")."' and `lastname` = '".mysql_real_escape_string("")."'");
$identity = "image_three";
}
elseif(empty($image_four))
{
mysql_query("update `vpb_uploads` set `image_four` = '".mysql_real_escape_string($random_name_generated)."' where `username` = '".mysql_real_escape_string(strip_tags($username))."' and `firstname` = '".mysql_real_escape_string("")."' and `lastname` = '".mysql_real_escape_string("")."'");
$identity = "image_four";
}
elseif(empty($image_five))
{
mysql_query("update `vpb_uploads` set `image_five` = '".mysql_real_escape_string($random_name_generated)."' where `username` = '".mysql_real_escape_string(strip_tags($username))."' and `firstname` = '".mysql_real_escape_string("")."' and `lastname` = '".mysql_real_escape_string("")."'");
$identity = "image_five";
}