This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 4 years ago.
$stmt variable is null means it doesn't contain my query even though connection($con) to the database is created.
Fatal error: Uncaught Error: Call to a member function bind_param() on boolean in C:\xampp\htdocs\WebServersAndroid\includes\DbOperations.php:24 Stack trace: #0 C:\xampp\htdocs\WebServersAndroid\v1\registerUser.php(12): DbOperation->createUser('syed', '1997') #1 {main} thrown in C:\xampp\htdocs\WebServersAndroid\includes\DbOperations.php on line 24
error occurs in bind_param maybe because there is no query to store data to. Please tell me what is the problem and why $stmt is null. Thank you
class DbOperation{
private $con;
function __construct(){
require_once dirname(__FILE__).'/DbConnect.php';
$db = new DbConnect();
$this->con = $db->connect();
}
function createUser($name,$usn){
$query = "INSERT INTO 'test_col'('name','usn') VALUES (?,?); ";
$stmt = $this->con->prepare($query);
echo $stmt;
if($stmt != null)
{
$stmt->bind_param("ss",$name,$usn);
if($stmt -> execute())
{
return true;
}else{
return false;
}
}
else{
echo "in else";
}
}
}
I think this code generates a lot of errors.
$query = "INSERT INTO 'test_col'('name','usn') VALUES (?,?); ";
values (?, ?) expects 2 values to be bind later.
While this code
`$stmt->bind_param("ss",$name,$usn);`
binds 3 parameters which trigger an error in binding.
Next, the use of single/double quotation mark is interpreted by MySQL as String literal as stated in documantation
As a result, 'test_col'('name','usn') causes error, you should use ` or just leave the name of table and columns instead of using single quotation mark (') which translate your table name, database name, and/or column name to string instead of reference.
Related
This question already has an answer here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 5 years ago.
I am working on the below code. Why am I not able to run the query properly? I already check the database connection and it is fine
<?php
$sql = "SELECT dt, events, eventtype FROM events";
$stmt = $mysqli->prepare($sql);
$stmt->execute();
$stmt->bind_result($dt,$events,$eventtype);
$stmt->store_result();
if($stmt->num_rows >0) {
$stmt->fetch();
}
else {
echo "Cant Find The data!";
}
$stmt->close();
$mysqli->close();
echo $dt;
echo $events;
echo $eventtype;
?>
getting this error
Fatal error : Call to a member function execute() on boolean in
/srv/disk1/2555378/www/domain.net/index.php on line 113
This means that the variable $mysqli contains a boolean value, probably false.
According to the php docs, http://php.net/manual/en/mysqli.prepare.php, the function mysqli::prepare will return false in case of an error.
You should use the error variable to get more information, like here: http://php.net/manual/en/mysqli.error.php
This question already has an answer here:
Can I use a PDO prepared statement to bind an identifier (a table or field name) or a syntax keyword?
(1 answer)
Closed 5 years ago.
I'm failing to see the problem why I'm getting a pdo error, I'm not missing a simple : or a parameter (since there are only 2)
public function does_stringid_excist($strTable, $strColumn, $strValue)
{
$sql = "SELECT count(1) AS count FROM tblemployer WHERE :strColumn = :strValue";
$this->objDatabase->query($sql); //Makes a prepare with the given sql
// $this->objDatabase->bind_column(':strTable', $strTable);
$this->objDatabase->bind_column(':strColumn', $strColumn); // Uses the `bindColumn()` from PDO
$this->objDatabase->bind_value(':strValue', $strValue); // Uses the `bindValue()` from PDO
$result = $this->objDatabase->single();
return $result['count'];
}
SELECT count(1) AS count FROM `tblemployer` WHERE `employerID` = :strValue" works just fine so the error isn't with the value.
A column is not the same as a table. Youre using bindColumn to bind a table, which does not work.
See: http://php.net/manual/en/pdostatement.bindcolumn.php
This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 5 years ago.
So I have the following code which is used to add a row to the respondent table, all working except when trying to add the value in of Brand:
$brand = 'Central';
function new_respondent() {
global $link;
$proc = mysqli_prepare($link, "INSERT INTO trespondent (brand, code) VALUES (?, uuid());");
mysqli_stmt_bind_param($proc, "s", $brand);
mysqli_stmt_execute($proc);
$respondent_id = mysqli_insert_id($link);
mysqli_stmt_fetch($proc);
mysqli_stmt_close($proc);
mysqli_clean_connection($link);
}
This code works (to a point) adds a row in the table and adds in the UUID no problems but brand is going in as NULL - I'm trying to work out if I am missing something very obvious here!
Any and all suggestion welcome.
You need to add $brand to your global, since it's outside of the function:
global $link, $brand;
Alternatively, you can modify your function to accept $brand as parameter:
function new_respondent($brand) {
...
}
This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 5 years ago.
when i try to add a new studio in my php application in this example i use newstudio as a name i get this error i think that it read the name i give as a row in the database table
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42S22]: Column not found: 1054 Champ 'newstudio' inconnu dans field list'
private $conn;
public function __construct()
{
$database = new Database();
$db = $database->dbConnection();
$this->conn = $db;
}
public function addStudio($sname,$sdes,$sidusr)
{
$stmt = $this->conn->prepare("INSERT INTO studio (name,des,idusr) VALUES ($sname,$sdes, $sidusr)");
$stmt->execute();
}
if you have string value you should use single quote around these vars
public function addStudio($sname,$sdes,$sidusr)
{
$stmt = $this->conn->prepare("INSERT INTO studio (name,des,idusr)
VALUES ('$sname','$sdes', '$sidusr')");
$stmt->execute();
}
or you could use a parametrized query
public function addStudio($sname,$sdes,$sidusr)
{
$stmt = $this->conn->prepare("INSERT INTO studio (name,des,idusr)
VALUES (:sname,:sdes, ':sidusr)");
$stmt->bindParam(':sname', $sname);
$stmt->bindParam(':sdes', $sdes);
$stmt->bindParam(':sidusr', $sidusr);
$stmt->execute();
}
Values require a ' wrapped around it, but don't add it yourself.
You are already using prepare(), make use of it.
public function addStudio($sname,$sdes,$sidusr){
$stmt = $this->conn->prepare("INSERT INTO studio (name,des,idusr) VALUES (?,?,?)");
$stmt->execute([$sname, $sdes, $sidusr]);
}
Also, you should actually check the value of $stmt and verify if execute() did its job correctly.
This question already has answers here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(2 answers)
Closed 2 years ago.
I have the following code:
$statement = $mysqli->prepare("INSERT INTO `paypal_transactions` (`txn_id`, `payer_email`, `mc_gross`, `mc_currency`, `expires`, `userid`) VALUES (?, ?, ?, ?, " . (time() + 2678400) . ", ?)");
file_put_contents('error.txt', $mysqli->error . mysqli_error($mysqli));
$statement->bind_param('ssdsi', $txn_id, $payer_email, $payment_amount, $payment_currency, $userid);
$statement->execute();
error.txt is blank every single time, and this is what I see in the error_log file:
[02-Jul-2013 09:08:15 America/Denver] PHP Fatal error:
Call to a member function bind_param() on a non-object in /home4/site/public_html/paypal.php on line 96
which is referring to the block of code above.
I am at my wits end with this, I have been trying to fix it for hours and it just won't work. I cannot find any problems with my sql query and I am losing my mind trying to figure out what's wrong.
It seems $statement = $mysqli->prepare(..) give result FALSE so $statement is not object and you can't use $statement->bind_param(..)
$statement = $mysqli->prepare("...");
if( $statement !== FALSE ) {
$statement->bind_param(...);
$statement->execute();
}
PHP - MySQLi - prepare
BTW: Have you test your SQL query directly in database by copy/paste ?
Don't use MYSQL keywords in $mysqli->prepare,for example:from,select etc.
So,your datatables fields name are important!Please checking