I'm retrieving tweets from the twitter api, which i'm trying to save in my database however i keep getting an error, which i cant seem to fix. i've checked the number of parameters is correct and everything should be okay, so i dont see why i get following error:
Fatal error: Call to a member function bind_param() on a non-object
tweets database:
function retrievePlayerTweets(){
global $con;
$query = $con->prepare("Select players.fullname, players.twitter_user, team.id as teamId FROM players, team WHERE players.teamId = team.id");
$query->execute();
$query->bind_result($fullname, $twitter_user, $teamId);
while ($query->fetch()) {
foreach(retrieveUserTweets($twitter_user) as $twitterData) {
$id = $twitterData['id_str'];
$text = $twitterData['text'];
$name = $twitterData['user']['name'];
$dateString = $twitterData['created_at'];
$favoriteCount = $twitterData['favorite_count'];
$date = date('Y-m-d H:i:s', strtotime($dateString));
$insert_tweet = $con->prepare("INSERT IGNORE INTO tweets (`fullname`, `username`, `text`, `created`, `teamId`, `twitterId`, `favoriteCount`) VALUES (?, ?, ?, ?, ?, ?, ?)");
$insert_tweet->bind_param("ssssisi", $name, $twitter_user, $text, $date, $teamId, $id, $favoriteCount);
$insert_tweet->execute() or die(mysqli_error($con));
}
}
}
The problem is with your $con variable which is not connecting to the database, due to which the methods inside cannot be called.
also it is good pratice to add die() function to print error message after call to the execute function in PDO or mysqli, to see error in query use:
if using PDO:
$insert_tweet->execute() or die(print_r($con->errorInfo()));
if using mysqli:
$insert_tweet->execute() or die(mysqli_error($con));
Related
I am attempting to update a row in my database where the users IP address and email address from an email are the used in the where clause of the query.
public function insert_user_captured_data($userip, $didIclickemaillink, $emailclickedfrom, $fname, $lname, $emailentered, $submitclicked){
$sql = "UPDATE testdata
SET useripclicked = ?,
emailclickedfromhere = ?,
userlinkclicked = ?,
userfnameentered = ?,
userlnameentered = ?,
useremailentered = ?,
usersubmittedform = ?,
timestamp = CURRENT_TIMESTAMP
WHERE useripclicked = ? AND emailclickedfromwhere = ?";
$stmnt = $this->dbConnection->prepare($sql);
$stmnt->bind_param("ssisssiss",$userip,$emailclickedfrom,$didIclickemaillink,$fname,$lname,$emailentered,$submitclicked,$userip,$emailclickedfrom);
$insRes = $stmnt->execute();
if (!$insRes) {
throw new Exception("Error Processing Request: Row Not inserted $stmnt->error", 1);
}
}
I am successful in the initial insert but want to update the second pass to fill in the rows after the user has filled out a contact form. I get the following error (in code tags to stand out)
Fatal error: Uncaught Error: Call to a member function bind_param() on boolean in C:\wamp64\www\example.com\assets\php\database.php:73 Stack trace: #0 C:\wamp64\www\example.com\assets\php\thankyou.php(37): Database->insert_user_captured_data('10.0.2.15', 'jfender#example.com', 'Jesse', 'Fender', 'jfender#example.com', 1) #1 {main} thrown in C:\wamp64\www\example.com\assets\php\database.php on line 73
Additionally removed some of the repeated values that really didn't need to be updated with the following as a result:
public function insert_user_captured_data($userip, $emailclickedfrom, $fname, $lname, $emailentered, $submitclicked){
$sql = "UPDATE testdata
SET userfnameentered = ?,
userlnameentered = ?,
useremailentered = ?,
usersubmittedform = ?,
timestamp = CURRENT_TIMESTAMP
WHERE useripclicked = ? AND emailclickedfromwhere = ?";
$stmnt = $this->dbConnection->prepare($sql);
$stmnt->bind_param("sssiss",$fname,$lname,$emailentered,$submitclicked,$userip,$emailclickedfrom);
$insRes = $stmnt->execute();
if (!$insRes) {
throw new Exception("Error Processing Request: Row Not inserted $stmnt->error", 1);
}
}
But am getting the same error, and printing out the error and stack trace have been no help at all...
What am I doing wrong? I'm currently using PHP 7.1.9, MySQL 5.7.19, and Apache 2.4.17 On a Windows 10 system I am using OOP mysqli, and have been inserting just fine in my table.
Im so Sorry guys... I found the error... it happened in the prepare() and it was due to my entering an erroneous letter in one of the column names. here is the corrected code::
public function insert_user_captured_data($userip, $emailclickedfrom, $fname, $lname, $emailentered, $submitclicked){
$sql = "UPDATE testdata
SET userfnameentered = ?,
userlnameentered = ?,
useremailentered = ?,
usersubmittedform = ?,
timestamp = CURRENT_TIMESTAMP
WHERE useripclicked = ? AND emailclickedfromhere = ?";
$stmnt = $this->dbConnection->prepare($sql);
$stmnt->bind_param("sssiss",$fname,$lname,$emailentered,$submitclicked,$userip,$emailclickedfrom);
$insRes = $stmnt->execute();
if (!$insRes) {
throw new Exception("Error Processing Request: Row Not inserted $stmnt->error", 1);
}
}
Jesse Fender
Nice, prepare returns false when there is an SQL error. To track them easier you can make a quick check. Hope it helps:
if ($stmt = $this->dbConnection->prepare("QUERY")) {
$stmt->bind_param(...);
} else {
echo "Prepare error: {$this->dbConnection->errno} - {$this->dbConnection->error}";
}
I'm trying to do a simple insert on my database after retrieving a value from it, I'm following the same procedure to retrieve a value from my database as to insert values in it, but I get the following error:
Fatal error: Uncaught Error: Call to a member function bind_param() on boolean
Here's my code:
$getuid = $mysqli->prepare("SELECT id FROM members WHERE email = ?");
$getuid->bind_param("s", $email);
$getuid->execute();
$getuid->bind_result($uid);
$nombre = $_POST['nombre'];
$direccion = $_POST['direccion'];
$codpost = $_POST['codpost'];
$municipio = $_POST['municipio'];
$estado = $_POST['estado'];
while($getuid->fetch()){
echo("INSERT INTO infoclientes VALUES ($uid, $nombre, $direccion, $codpost, $municipio, $estado)");
$infocte = $mysqli->prepare("INSERT INTO infoclientes VALUES(?, ?, ?, ?, ?, ?)");
$infocte->bind_param("ssssss", $uid, $nombre, $direccion, $codpost, $municipio, $estado);
$infocte->execute();
$infocte->close();
}
$getuid->close();
Apparently, the error comes out from
$infocte->bind_param("ssssss", $uid, $nombre, $direccion, $codpost, $municipio, $estado);
This is the output from the echo before the second bind_param:
INSERT INTO infoclientes VALUES (1, Fernando Cervantes, Av. Pie de la cuesta 2, 76158, Querétaro, Querétaro)
I got it to work! I just moved $getuid->close() to the line before the echo(). I guess it was as #LouisLoudogTrottier mentioned, I was trying to prepare both queries while the same connection was opened.
This question already has answers here:
Call to a member function bind_param() on a non-object [duplicate]
(6 answers)
Closed 6 years ago.
So I'm having the same problem for 3 hours straight and I've been searching Stack Overflow, Google and even Bing (yes even Bing..) for the right answer but I can't seem to find a solution...
The problem:
I try to insert data into my database using a prepared statement, but I keep getting the same error over and over again. I'm using everything in the right order and stuff so that's not the mistake...
My code:
//INCLUDES FILES THAT HOLD ALL DATA (servername, username, ...)
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// prepare and bind
$stmt = $conn->prepare("INSERT INTO REGISTRY (user_ref, email_user, date_created, title, tweet, description, category, filename, invoice) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param("sssssssss", $gebruikerID, $email, $date_created, $title, $tweet, $description, $category, $nameImage, $customUniqueId);
// set parameters and execute
$gebruikerID = $gebruikerID;
$email = $email;
$date_created = date("d/m/Y H:i:s");
$title = date("d/m/Y H:i:s");
$tweet = $_POST["tweet"];
$description = $_POST["project_description"];
$category = $_POST["category"];
$nameImage = $nameImage;
$customUniqueId = $_GET["redirect"];
$stmt->execute();
echo "New records created successfully";
$stmt->close();
$conn->close();
The error I'm getting for 3 hours straight:
Fatal error: Call to a member function bind_param() on a non-object on line 148
For those of you that have the same problem like me, here's what was wrong:
I had forgotten to put every row of my database in the code... So what I ended up was go to phpmyadmin and go to the table i wanted to insert some data in. Then I clicked SQL and I chose insert.
After that, I got a code and I replaced all [val-x] with ? And ended up with this code:
// prepare and bind
$stmt = $conn->prepare("INSERT INTO `projecten`(`user_ref`, `email_user`, `date_created`, `title`, `tweet`, `description`, `category`, `filename`, `invoice`, `admin_checkup`, `visible`, `project_finished`, `deleted`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)");
$stmt->bind_param("sssssssssssss", $gebruikerID, $email, $date_created, $title, $tweet, $description, $category, $nameImage, $customUniqueId, $adminCheck, $visible, $project_finished, $deleted);
// set parameters and execute
$gebruikerID = $gebruikerID;
$email = $email;
$date_created = date("d/m/Y H:i:s");
$title = date("d/m/Y H:i:s");
$tweet = $_POST["tweet"];
$description = $_POST["project_description"];
$category = $_POST["category"];
$nameImage = $nameImage;
$customUniqueId = $_GET["redirect"];
$adminCheck = "0";
$visible = "0";
$project_finished = "0";
$deleted = "0";
$stmt->execute();
I hope this helps you guys out!
I've created below function which include several mysql queries, which seem to create an issue. Cause when I run this function it returns following error:
Errormessage: Commands out of sync; you can't run this command now
I've tried to include next_result(), but does not do any difference?
function retrievePlayerTweets(){
global $con;
$query = $con->prepare("Select players.fullname, players.twitter_user, team.id as teamId FROM players, team WHERE players.teamId = team.id");
$query->execute();
$query->bind_result($fullname, $twitter_user, $teamId);
while ($query->fetch()) {
foreach(retrieveUserTweets($twitter_user) as $twitterData) {
$id = $twitterData['id_str'];
$text = $twitterData['text'];
$name = $twitterData['user']['name'];
$dateString = $twitterData['created_at'];
$favoriteCount = $twitterData['favorite_count'];
$date = date('Y-m-d H:i:s', strtotime($dateString));
if ($insert_tweet = $con->prepare("INSERT IGNORE INTO tweets (`fullname`, `username`, `text`, `created`, `teamId`, `twitterId`, `favoriteCount`) VALUES (?, ?, ?, ?, ?, ?, ?)")) {
$insert_tweet->bind_param("ssssisi", $name, $twitter_user, $text, $date, $teamId, $id, $favoriteCount);
$insert_tweet->execute();
$con->next_result();
} else {
die("Errormessage: ". $con->error);
}
}
}
}
If you get Commands out of sync; you can't run this command now in your client code, you are calling client functions in the wrong order.
This can happen, for example, if you are using mysql_use_result() and try to execute a new query before you have called mysql_free_result(). It can also happen if you try to execute two queries that return data without calling mysql_use_result() or mysql_store_result() in between.
https://dev.mysql.com/doc/refman/5.0/en/commands-out-of-sync.html
I've gone over this script like 30 times, and I can't for the life of me find my problem. Here is the code:
function redeem() {
$case = $_POST["case"];
$name = $_POST["name"];
$profession = $_POST["profession"];
$city = $_POST["city"];
$country = $_POST["country"];
$totalpercent = $_POST["totalpercent"];
$pretest = $_POST["pretest"];
$posttest = $_POST["posttest"];
$investigationspercent = $_POST["investigationspercent"];
$timesreset = $_POST["timesreset"];
$creditsspent = $_POST["creditsspent"];
$timescompleted = $_POST["timescompleted"];
//Add the information to the learnent_cases_leaderboard table
$stmt = $this->db->prepare("INSERT INTO learnent_cases_leaderboard (case, name, profession, city, country, totalpercent, pretest, posttest, investigationspercent, creditsspent, timescompleted, timesreset, timestamp) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP)");
$stmt->bind_param("sssssiiiiiii", $case, $name, $profession, $city, $country, $totalpercent, $pretest, $posttest, $investigationspercent, $creditsspent, $timescompleted, $timesreset); //the quotations specify the type of variable;
//See http://php.net/manual/en/mysqli-stmt.bind-param.php for more information on bind_param
$stmt->execute();
$stmt->close();
When I look at the error log, it gives me this error message:
Line 105 is this line:
PHP Fatal error: Call to a member function bind_param() on a non-object on line 105
Code:
$stmt->bind_param("sssssiiiiiii", $case, $name, $profession, $city, $country, $totalpercent, $pretest, $posttest, $investigationspercent, $creditsspent, $timescompleted, $timesreset);
You never checked that $stmt is an object. In this case, it's more likely to be FALSE, which is what PDO::prepare returns when your query has an error in it.
And your query has an error in it, because you did not delimit your field names in backticks and timestamp is a keyword.
Check for errors after invoking functions from 3rd party APIs, and fix your query.
First of; always run your queries in the localhost to see if your query executes without error. Next always make sure your the names of the fields and data types corresponds with what you have in your code