PHP -> PDO -> Prepare -> Call Procedure -> Insert Into -> Bind Parameters - php

using this procedure
CREATE PROCEDURE `Insert_New_Return_Id`(IN Insert_Stmnt varchar(1000), OUT IDNum int)
BEGIN
SET #buffer = Insert_Stmnt;
PREPARE stmt FROM #buffer;
EXECUTE stmt;
SELECT LAST_INSERT_ID() INTO IDNum;
DEALLOCATE PREPARE stmt;
END
the following code works fine :
$statement=$con->prepare("CALL Insert_New_Return_Id (\"INSERT INTO users (first_name,last_name)VALUES('test','test')\",#ID)");
$statement->execute();
$statement=$con->query("SELECT #ID");
while ($row = $statement->fetch()){echo "Last ID Insert : " . $row['#ID'];}
but when i'm trying to bind parameters the values are ?
$first_name = "test";
$last_name = "test";
$statement=$con->prepare("CALL Insert_New_Return_Id (\"INSERT INTO users (first_name,last_name)VALUES('?','?')\",#ID)");
$statement->bindParam(1, $first_name, PDO::PARAM_STR);
$statement->bindParam(2, $last_name, PDO::PARAM_STR);
$statement->execute();
$statement=$con->query("SELECT #ID");
while ($row = $statement->fetch()){echo "Last ID Insert : " . $row['#ID'];}
If i try VALUES(?,?) returns an error.
How can i make this work? Call a procedure with prepare statement and binding parameters?
Thank you

$statement->bindParam(1, 'test', PDO::PARAM_STR);
$statement->bindParam(2, 'test', PDO::PARAM_STR);
You must use a variable instead of the string 'test'. PDOStatement::bindParam binds variables by reference. By definition, you cannot do this with a string.
Use a variable instead.
$statement->bindParam(1, $str1, PDO::PARAM_STR);
$statement->bindParam(2, $str2, PDO::PARAM_STR);
Also, when you want to use CALL to call a stored procedure, just call the stored procedure by name. Do not repeat the query. Of course, this assumes you've done the work of adding the stored procedure to MySQL.
$statement=$con->prepare('CALL Insert_New_Return_Id(?,?)');
If you need a third parameter, add it to the stored procedure in MySQL and call it like this.
$statement=$con->prepare('CALL Insert_New_Return_Id(?,?,?)');

Related

PHP PDO Statement: Incorrect number of arguments for PROCEDURE

What is wrong in this code?
$statement = $dbConn->prepare("CALL SearchUser(?)");
$statement->bindParam(1, $username, PDO::PARAM_STR|PDO::PARAM_INPUT_OUTPUT, 4000);
This is the procedure:
CREATE PROCEDURE SearchUser(IN Username VARCHAR(10), OUT numRows INT)
BEGIN SELECT COUNT(*) INTO numRows
FROM USER
WHERE Username='IN';
END//
The error is: Incorrect number of arguments for PROCEDURE, expected 2 got 1.
Why? Thank you.
You need to pass two parameter
$statement = $dbConn->prepare("CALL SearchUser(?,?)");
$statement->bindParam(1, $username, PDO::PARAM_STR|PDO::PARAM_INPUT_OUTPUT, 4000);
$statement->bindParam(2, $rowcount, PDO::PARAM_INT|PDO::PARAM_INPUT_OUTPUT);
You could refer this article

What are the differences array usage and bindParam usage during mysql data inserting?

I am preparing mysql configuration settings using class. I am confused about it. I always use bindParam. It also possible to insert using array. I mean, what are the differences between array and bindparam.
eg array
$query = $db->prepare("INSERT INTO users SET
username = :uname,
password = :upass,
email = :umail");
$insert = $query->execute(array(
"upass" => "123456",
"umail" => "user#user.com",
"uname" => "username",
));
if ( $insert ){
$last_id = $db->lastInsertId();
}
eg
$stmt = $this -> db_conn -> prepare("INSERT into users(username, password) VALUES(:uname, :upass)");
$stmt -> bindParam(':uname', $username);
$stmt -> bindParam(':upass', $password);
$stmt -> execute();
Desconsidering the fact that with execute you can't choose the data type (it's always PDO::PARAM_STR) There's only a main difference between both (which is from core). PDOStatement::bindParam is by reference while PDOStatement::execute isn't. PDOStatement::execute do internnaly the same thing as PDOStatement::bindValue, but twice.
Internally (in C), bindValue calls the same method which execute calls. The method name which is called is really_register_bound_param.
On the other hand, bindParam calls other method called register_bound_param.
It means that bindValue calls the same method called by execute more than one time while bindParam calls a method to bind as a reference and only "really register" the param when execute is called.
Thinking about bind by reference, it's only possible using bindParam:
//fictional code
$stmt= $pdo->prepare("INSERT INTO table (column) VALUES (:value)");
$stmt->bindParam(":value", $randomValue);
for($i = 0 ; $i < 1000; $i))
{ $randomValue = rand(1000,1000000);
$stmt->execute();
}
Is it worthy? Perhaps while a while with a complex insert with multiples parameters could reduce the overhead with rebinding a new parameter or a big amount of them.

How to fetch data using procedure in PHP?

I am using procedure first time.How can I fetch data using procedure.I created this procedure in sql file.
CREATE PROCEDURE `GetUserProfile`(IN p_user_id INT, IN p_enabled BIT)
READS SQL DATA
BEGIN
# prepare init params
if p_user_id = 0 then
set p_user_id = null;
end if;
select parent_user_id
,user_id
,Last_name
,First_name
,email as Email_address
,mobile as Phone
from app_users
where app_user_id = IFNULL(p_user_id, user_id)
and enabled = IFNULL(p_enabled, enabled);
then How can I fetch the user detail through PHP like First_name,Last_name etc.
thanks
Do you can use sqlsrv_connect function to connect to SQL Server db, use sqlsrv_prepare function to call tsql function and $params args and, at last, use one of the sqlsrv_fetch_* function to retrieve data.
see instruction on this page:
http://technet.microsoft.com/en-us/library/cc793139(v=sql.90).aspx
You can use PDO to execute a mysql stored procedure and access the results.
See the examples on this page from the PHP Documentation. In particular example #4
<?php
$stmt = $dbh->prepare("CALL sp_returns_string(?)");
$stmt->bindParam(1, $return_value, PDO::PARAM_STR, 4000);
// call the stored procedure
$stmt->execute();
print "procedure returned $return_value\n";
?>
and example #5
<?php
$stmt = $dbh->prepare("CALL sp_takes_string_returns_string(?)");
$value = 'hello';
$stmt->bindParam(1, $value, PDO::PARAM_STR|PDO::PARAM_INPUT_OUTPUT, 4000);
// call the stored procedure
$stmt->execute();
print "procedure returned $value\n";
?>
If you haven't used PDO before I'd suggest browsing through the documentation, creating a new PDO object to manipulate a dabase is as simple as:
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);

PDO PHP Stored Procedure (bindParam)

Everything worked for me with bindParam and PDO.
But the moment I add 3 bindParam, it doesn't work.
Example that work:
$stmt=$dbh->prepare("select * from SP_IMPORT_CRM_SELECTIE(?,?,'test','org naam','French','1','2','adres','adres1','city','city','state','state','postal','postal','country','country','po','po','phone','other','email','otheremail','fax','web','VAT')");
$stmt->bindParam(1, $firma, PDO::PARAM_INT);
$stmt->bindParam(2, $ACC, PDO::PARAM_STR,20);
The moment I add a third param, my browser gives me the message = Can't receive data:
$stmt=$dbh->prepare("select * from SP_IMPORT_CRM_SELECTIE(?,?,?,'org naam','French','1','2','adres','adres1','city','city','state','state','postal','postal','country','country','po','po','phone','other','email','otheremail','fax','web','VAT')");
$stmt->bindParam(1, $firma, PDO::PARAM_INT);
$stmt->bindParam(2, $ACC, PDO::PARAM_STR,20);
$stmt->bindParam(3, $org, PDO::PARAM_STR,50);
Is there a limit on bindParam?
I have 26 input parameters. When I return 26 values in my stored procedure, IT WORKS..
It that normal?
So you always have to have the same number of input as output parameter?

how to get insert_id using mysqli prepare statements

How can I get the insert_id of the last record using mysqli prepare statement, for example following?
$stmt = $this->mysqli->prepare("INSERT INTO test (name) values (?)")
$stmt->bind_param('s', $name);
$stmt->execute();
$new_id = $this->mysqli->insert_id; (after $stmt->execute())
mysqli->insert_id

Categories