bind_result failed - php

I actually tried mysqli->fetch_array(); for fetching array but that couldn't work so I used bind_result(); and it throws the below error
Warning: mysqli_stmt::bind_result(): Number of bind variables doesn't match number of fields in prepared statement
So I did a error check on bind_result and what I got was bind_result() failed: so what I am really doing wrong?
$wtf = "";
$hello = "";
$check_empty_field = $mysqli - > prepare("select `username`, `firstname`, `lastname` from `vpb_uploads` where `username` = ? and `firstname` = ? and `lastname` = ?");
$check_empty_field - > bind_param('sss', $username, $wtf, $hello);
$check_empty_field - > execute();
$check_empty_field - > store_result();
if($check_empty_field - > num_rows < 1) {
$date = date("d-m-Y");
$i2 = '';
$i3 = '';
$i4 = '';
$i5 = '';
$insert = $mysqli - > prepare("insert into `vpb_uploads` (`username`, `firstname`, `lastname`, image_one, image_two, image_three, image_four, image_five, date)values(?,?,?,?,?,?,?,?,?)");
$insert - > bind_param('sssssssss', $username, $wtf, $hello, $random_name_generated, $i2, $i3, $i4, $i5, $date);
$insert - > execute();
$identity = "image_one";
} else {
$get_empty_field = $check_empty_field - > bind_result($username, $wtf, $hello);
if(false === $get_empty_field) {
die('bind_result() failed: '.htmlspecialchars($mysqli - > error));
}
$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;
}

This error is thrown because you have not set each of the column names which are returned from the SQL query to a variable.
Example:
If you have 6 columns in your table and you have selected all (*) then you will have to:
$stmt->bind_result($Col_1,$Col_2,$Col_3,$Col_4,$Col_5,$Col_6);
If you have:
$stmt->bind_result($Col_1,$Col_2,$Col_3,$Col_4,$Col_5);
an error will be thrown.
$check_empty_field->data_seek(0);
$get_empty_field = $check_empty_field - > bind_result($username, $wtf, $hello);

Related

php script return true suddenly stopped returning value

I have a function in my PHP script which restores data from backup. Everything was fine and working well, until suddenly it stopped working after months of working well. I am using OC 2.2.0 and this function is supposed to restore products and their data from oc_product_backup table. I print_r every step so that I would see where the problem is, and realized that when it gets to:
return true;
it never happens. What could be wrong all of the sudden, and how do I make this work? I never had this kind of problem. My function looks like this:
function restoreBackup()
{
global $mysqli;
$i = 0;
$getpic = "SELECT * FROM oc_product_backup LIMIT 0, 100000";
$backup = $mysqli->query($getpic);
$mysqli->autocommit(FALSE);
$updateproduct_sql = "UPDATE oc_product SET image = ?, modified_by = ?, date_modified = ? WHERE product_id= ?";
$updatedescription_sql = "UPDATE oc_product_description SET meta_description = ?, meta_keyword = ?, tag = ?, modified_by = ? WHERE product_id = ? AND language_id = ?";
$stmt = $mysqli->prepare($updateproduct_sql);
$stmt->bind_param('siss', $image, $modified_by, $date_modified, $product_id);
//print_r ($updateproduct_sql);
$stmt2 = $mysqli->prepare($updatedescription_sql);
$stmt2->bind_param('sssisi', $meta_description, $meta_keyword, $tag, $modified_by, $product_id, $language_id);
//print_r($updatedescription_sql);
while($row = $backup->fetch_array(MYSQLI_ASSOC))
{
//$name = removeslashes($row['name']);
//$name = $row['name'];
//$description = removeslashes($row['description']);
//$description = $row['description'];
$meta_description = $row['meta_description'];
$meta_keyword = $row['meta_keyword'];
$tag = $row['tag'];
$product_id = $row['product_id'];
$modified_by = $row['modified_by'];
$language_id = $row['language_id'];
//if($row['language_id'] == 1)
//{
$image = $row['image'];
//$ean = $row['ean'];
//$name = $row['name'];
//$model = $row['model'];
//$status = $row['status'];
$price_sync = $row['price_sync'];
$date_modified = $row['date_modified'];
if(!$stmt->execute())
return false;
//}
if(!$stmt2->execute())
return false;
$i++;
if(($i % 500) === 0) $mysqli->commit();
}
$mysqli->commit();
$backup->close(); //the last line that gets executed
return true; //this never happens
writeToLog('- Backup restored');
}
After looking at the code a bit, it seems like your prepared statements binding the data was outside of the loop, so technically it would never have written any data.
function restoreBackup() {
global $mysqli;
$i = 0;
$getpic = "SELECT * FROM oc_product_backup LIMIT 0, 100000";
$backup = $mysqli - > query($getpic);
$mysqli - > autocommit(FALSE);
$updateproduct_sql = "UPDATE oc_product SET image = ?, modified_by = ?, date_modified = ? WHERE product_id= ?";
$updatedescription_sql = "UPDATE oc_product_description SET meta_description = ?, meta_keyword = ?, tag = ?, modified_by = ? WHERE product_id = ? AND language_id = ?";
while ($row = $backup - > fetch_array(MYSQLI_ASSOC)) {
$meta_description = $row['meta_description'];
$meta_keyword = $row['meta_keyword'];
$tag = $row['tag'];
$product_id = $row['product_id'];
$modified_by = $row['modified_by'];
$language_id = $row['language_id'];
$image = $row['image'];
$price_sync = $row['price_sync'];
$date_modified = $row['date_modified'];
$stmt = $mysqli - > prepare($updateproduct_sql);
$stmt - > bind_param('siss', $image, $modified_by, $date_modified, $product_id);
if (!$stmt - > execute())
return false;
$stmt2 = $mysqli - > prepare($updatedescription_sql);
$stmt2 - > bind_param('sssisi', $meta_description, $meta_keyword, $tag, $modified_by, $product_id, $language_id);
if (!$stmt2 - > execute())
return false;
$i++;
if (($i % 500) === 0) $mysqli - > commit();
}
$mysqli - > commit();
$backup - > close(); //the last line that gets executed
return true; //this never happens
writeToLog('- Backup restored');
}

Request Http Url

I just want to call script.php to control the database like a polling.
Here is my code,
but it gives error : can not resolve HttpClient.
HttpClient httpclient = new DefaultHttpClient();
httpclient.execute(new HttpGet("http://www.holobech.com/script.php"));
also here my script.php which is check the time and create a table every day :
<?php
require "includes/database.php";
require "classes/C_UserList.php";
require "classes/C_GlobalClass.php";
$current_game = GlobalClass::fetchinfo("value", "info", "name", "current_game");
$today = date("Y-m-d H:i:s");
$objUserList = new UserList;
$game_started_at = $objUserList->getTotalTime($current_game);
$diff = abs(strtotime($game_started_at . "+1 days") - strtotime($today));
$one_day = 60*60*24;
if ($diff >= $one_day) {
$db_connect = Database::connect();
$old_game = GlobalClass::fetchinfo("value", "info", "name", "current_game");
$new_game = $old_game + 1;
$sql = "UPDATE info SET value = ? WHERE name = ?";
$query = $db_connect->prepare($sql);
$query->execute(array($new_game, "current_game"));
$sql = "CREATE TABLE game".$new_game."(
id INT AUTO_INCREMENT PRIMARY KEY,
uid INT NOT NULL,
highlighting INT NOT NULL,
sent_who INT NOT NULL,
sent_amount VARCHAR(255) NOT NULL,
sent_at DATETIME DEFAULT NULL,
joined_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)";
$query = $db_connect->prepare($sql);
$query->execute();
$sql = "SELECT * FROM game".$old_game;
$query = $db_connect->query($sql, PDO::FETCH_ASSOC);
if ($query->rowCount() > 0) {
$rows = $query->fetchAll();
if (count($rows) == 1) {
$sql = "INSERT INTO game".$new_game." SET uid = ?, highlighting = ?";
$query = $db_connect->prepare($sql);
$query->execute(array($rows[0]["uid"], $rows[0]["highlighting"]));
$game_card = 1;
$highlight_card = $rows[0]["highlighting"];
} else {
for ($i = 0; $i < count($rows); $i++) {
$sql = "SELECT played FROM user_game_info WHERE uid = ?";
$query = $db_connect->prepare($sql);
$query->execute(array($rows[$i]["uid"]));
$played = $query->fetch();
$sql = "UPDATE user_game_info SET at_game = ?, game_card = ?, played = ? WHERE uid = ?";
$query = $db_connect->prepare($sql);
$query->execute(array("0", "0", $played["played"], $rows[$i]["uid"]));
}
$game_card = 0;
$highlight_card = 0;
}
} else {
$game_card = 0;
$highlight_card = 0;
}
$sql = "INSERT INTO games SET game_id = ?, money_amount = ?, game_card = ?, highlight_card = ?, started_at = NOW()";
$query = $db_connect->prepare($sql);
$query->execute(array($new_game, "0", $game_card, $highlight_card));
}
?>
Apache Http is deprecated.
Add
useLibrary 'org.apache.http.legacy'
in app's build.gradle file in defaultConfig to use apache httpclient.

mysql php: How to bind result with the same recurring variable [duplicate]

I had a mysql query and I was converting it to mysqli(prepared statement) but I ran in to a problem which throws the below error,
Warning: mysqli_stmt::bind_result(): Number of bind variables doesn't match number of fields in prepared statement
Mysql code
$random_name_generated = vpb_generate_random_name().'.jpg'; //Generated name for uploaded files or images
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;
The below is what I tried even though it didn't work. I already knew it won't work but I wanted to try it myself before asking a question. And the error is coming from the $get_empty_field = $stmt->bind_result($stmt);
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();
$stmt->close();
$identity = "image_one";
} else {
$get_empty_field = $stmt->bind_result($stmt);
$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;
You need to change
$get_empty_field = $stmt->bind_result($stmt);
To
$get_empty_field = $stmt->bind_result($field1, $field2, $field3);
The number of $fieldx variables being equal to the number of fields that are selected. If you don't know how many there are, use this:
// Throw an exception if the result metadata cannot be retrieved
if (!$meta = $stmt->result_metadata())
{
throw new Exception($stmt->error);
}
// The data array
$data = array();
// The references array
$refs = array();
// Iterate over the fields and set a reference
while ($name = $meta->fetch_field())
{
$refs[] =& $data[$name->name];
}
// Free the metadata result
$meta->free_result();
// Throw an exception if the result cannot be bound
if (!call_user_func_array(array($stmt, 'bind_result'), $refs))
{
throw new Exception($stmt->error);
}
And then you access the result, after fetching, with $data['field'];

Mysqli update command not updating

include('config.php');
mysqli_select_db($mysqli, "real");
if ($transaction == "Success" && $currency == "USD") {
$user_ids = '".$user_id."'; $total_cred = `user_credits` +'".$package_credits."';
$add = $mysqli->prepare("UPDATE `users` SET `user_credits` = ? WHERE `user_id` = ?");
$add->bind_param('si', $total_cred,$user_ids); $add->execute();
}
The code doesn't throw out any error nor its updating the database .
Change the if block to
// $user_ids = '".$user_id."'; REMOVE THE statement
// $total_cred = `user_credits` + '".$package_credits."'; REMOVE THIS too
$add = $mysqli->prepare("UPDATE `users` SET `user_credits` = `user_credits` + ? WHERE `user_id` = ?");
$add->bind_param('ii', $package_credits, $user_id ); $add->execute();
Let MySQL do the hard part.
Try this one:
include('config.php');
mysqli_select_db($mysqli, "real");
if ($transaction == "Success" && $currency == "USD")
{
$user_ids = '".$user_id."';
$total_cred = user_credits +'".$package_credits."';
$add = $mysqli->prepare("UPDATE users SET user_credits = ? WHERE user_id = ?");
$add->bind_param('si', $total_cred,$user_ids);
$add->execute();
}

Full-text search using mysqli prepared statement errors with unmatched bind variables [duplicate]

I had a mysql query and I was converting it to mysqli(prepared statement) but I ran in to a problem which throws the below error,
Warning: mysqli_stmt::bind_result(): Number of bind variables doesn't match number of fields in prepared statement
Mysql code
$random_name_generated = vpb_generate_random_name().'.jpg'; //Generated name for uploaded files or images
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;
The below is what I tried even though it didn't work. I already knew it won't work but I wanted to try it myself before asking a question. And the error is coming from the $get_empty_field = $stmt->bind_result($stmt);
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();
$stmt->close();
$identity = "image_one";
} else {
$get_empty_field = $stmt->bind_result($stmt);
$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;
You need to change
$get_empty_field = $stmt->bind_result($stmt);
To
$get_empty_field = $stmt->bind_result($field1, $field2, $field3);
The number of $fieldx variables being equal to the number of fields that are selected. If you don't know how many there are, use this:
// Throw an exception if the result metadata cannot be retrieved
if (!$meta = $stmt->result_metadata())
{
throw new Exception($stmt->error);
}
// The data array
$data = array();
// The references array
$refs = array();
// Iterate over the fields and set a reference
while ($name = $meta->fetch_field())
{
$refs[] =& $data[$name->name];
}
// Free the metadata result
$meta->free_result();
// Throw an exception if the result cannot be bound
if (!call_user_func_array(array($stmt, 'bind_result'), $refs))
{
throw new Exception($stmt->error);
}
And then you access the result, after fetching, with $data['field'];

Categories