PDO::fetchAll not working with MySQL stored procedure - php

I'm trying to return a row from a database by calling a stored procedure throught PHP. However, when I do this how I normally would I get a "General Error".
Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error in C:\xampp\htdocs\Skilaverkefni 4\Courses\read.php:11
Stack trace:
#0 C:\xampp\htdocs\Skilaverkefni 4\Courses\read.php(11): PDOStatement->fetchAll(2)
#1 C:\xampp\htdocs\Skilaverkefni 4\index.php(13): ReadCourse('FOR3L3U')
#2 {main}
thrown in C:\xampp\htdocs\Skilaverkefni 4\Courses\read.php on line 11
Here is the code:
<?php
function ReadCourse($courseID)
{
require "dbCon.php";
$SQL = "SET #p0='" . $courseID . "'; CALL ReadCourse(#p0);";
echo "$SQL";
$logon = $pdo->prepare($SQL);
$logon->execute();
$records = $logon->fetchAll(PDO::FETCH_ASSOC);
print_r($records);
}
?>
After a long Google-session I found out that the issue is most caused by the way I'm handling the reading of the data returned from the stored procedure, how do I do this correctly?

The whole point of PDO is to make it impossible to run any old database query you choose to pass as a string.
From the documentation:
<?php
/* Call a stored procedure with an INOUT parameter */
$colour = 'red';
$sth = $dbh->prepare('CALL puree_fruit(?)');
$sth->bindParam(1, $colour, PDO::PARAM_STR|PDO::PARAM_INPUT_OUTPUT, 12);
$sth->execute();
print("After pureeing fruit, the colour is: $colour");
?>

Related

SQL query to update item quantity using PHP PDO

Good morning everyone
I am trying to update the table with the new quantity selected, when I run the following function, however, I get this error:
Fatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in C:\xampp\htdocs\php_Assessments\shoppingList\model\functions_products.php:11 Stack trace: #0 C:\xampp\htdocs\php_Assessments\shoppingList\model\functions_products.php(11): PDOStatement->execute() #1 C:\xampp\htdocs\php_Assessments\shoppingList\controller\product_update_process.php(21): update_item('57', '3', '1') #2 {main} thrown in C:\xampp\htdocs\php_Assessments\shoppingList\model\functions_products.ph
Function to update the quantity, function_products.php:
<?php
function update_item($soldID, $orderedQuantity, $itemQuantity)
{
global $conn;
$sql = "UPDATE shopping_items.sold SET orderedQuantity = :itemQuantity WHERE soldID = :soldID";
$statement = $conn->prepare($sql);
$statement->bindValue(':soldID', $soldID);
$statement->bindValue(':orderedQuantity', $orderedQuantity);
$statement->bindValue(':itemQuantity', $itemQuantity);
$result = $statement->execute();
$statement->closeCursor();
return $result;
}
?>
product_update_process.php
<?php
// Require database connection
require('connection.php');
// Require function
require_once("../model/functions_products.php");
// Fetch the data required
$soldID = $_GET['soldID'];
$itemQuantity = $_POST['itemQuantity'];
$orderedQuantity = $_POST['orderedQuantity'];
if(empty($itemQuantity)) {
echo '<script type="text/javascript">alert("The quantity is required.")</script>' ;
// Redirect the browser window back to the add customer page
echo "<script>setTimeout(\"location.href = '../index.php';\",2000);</script>";
} else {
//call the update_item() function
$result = update_item($soldID, $itemQuantity, $orderedQuantity);
// Redirect the browser window back to the admin page
header("location: ../index.php");
}
?>
What could be the issue here?
Thanks for your assistance.
To add to #TangentiallyPerpendicular's comment, why are you binding to :orderedQuantity? This variable is not being used in your SQL statement, even though you have told the SQL engine to expect the variable. The column doesn't need to be a variable in order pass a variable to it.

PHP Inserting array values into MySQL prepared statement

I have an array of values that I am trying to insert into a database. Currently the below only inserts the first key value pair in the array and then returns an error:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error' in /_/components/php/functions.php:33
Stack trace:
0 /_/components/php/functions.php(33): PDOStatement->fetchAll()
1 /testdb.php(44): Photo\DB\query('INSERT into cat...', Array, Object(PDO))
2 {main} thrown in /_/components/php/functions.php on line 33
line 33 in functions.php is in this function
function query($query, $bindings, $conn){
$stmt = $conn->prepare($query);
$stmt->execute($bindings);
$results = $stmt->fetchAll(); <---line 33
return $results ? $results : false;
}
The main code is
foreach ($sitecategories as $key => $value) {
// print "this is key $key and this is value $value";
if ( $conn ) {
$catquery=query("INSERT into categories (cat_id, label) VALUES (:catid, :label)",
array('catid'=>$key, 'label'=>$value),
$conn);
} else {
print "could not connect to the database";
}
}
So I am connecting OK to the DB (elsewhere in the code) and the first key value pair is inserted successfully but not the rest of the array. I'm guessing my syntax is incorrect somewhere in
array('catid'=>$key, 'label'=>$value),
but I can't figure it out. Any help much appreciated!
You shouldn't fetchAll() after you INSERT. INSERT has no result set.
I just ran a test of inserting and then calling fetchAll() on the PDOStatement object, and I confirm that this causes the "General error" exception you showed.
$stmt = $dbh->prepare("insert into foo () values ()");
$stmt->execute();
$result = $stmt->fetchAll();
Throws:
PHP Fatal error: Uncaught exception 'PDOException' with message
'SQLSTATE[HY000]: General error' in /Users/billkarwin/workspace/PHP/21194489.php:14
Stack trace:
#0 /Users/billkarwin/workspace/PHP/21194489.php(14): PDOStatement->fetchAll()
#1 {main}
thrown in /Users/billkarwin/workspace/PHP/21194489.php on line 14
Your array of values is incorrect. The keys need to have the :, too.
array(':catid'=>$key, ':label'=>$value)

Binding a date string parameter in an MS Access PDO query

I've made a PDO database class which I use to run queries on an MS Access database.
When querying using a date condition, as is common in SQL, dates are passed as a string. Access usually expects the date to be surrounded in hashes however. E.g.
SELECT transactions.amount FROM transactions WHERE transactions.date = #2013-05-25#;
If I where to run this query using PDO I might do the following.
//instatiate pdo connection etc... resulting in a $db object
$stmt = $db->prepare('SELECT transactions.amount FROM transactions WHERE transactions.date = #:mydate#;'); //prepare the query
$stmt->bindValue('mydate', '2013-05-25', PDO::PARAM_STR); //bind the date as a string
$stmt->execute(); //run it
$result = $stmt->fetch(); //get the results
As far as my understanding goes the statement that results from the above would look like this as binding a string results in it being surrounded by quotes:
SELECT transactions.amount FROM transactions WHERE transactions.date = #'2013-05-25'#;
This causes an error and prevents the statement from running.
What's the best way to bind a date string in PDO without causing this error? I'm currently resorting to sprintf-ing the string which I'm sure is bad practise.
Edit: if I pass the hash-surrounded date then I still get the error as below:
Fatal error: Uncaught exception 'PDOException' with message
'SQLSTATE[22018]: Invalid character value for cast specification:
-3030 [Microsoft][ODBC Microsoft Access Driver] Data type mismatch in criteria expression. (SQLExecute[-3030] at
ext\pdo_odbc\odbc_stmt.c:254)' in
C:\xampp\htdocs\ips\php\classes.php:49 Stack trace: #0
C:\xampp\htdocs\ips\php\classes.php(49): PDOStatement->execute() #1
C:\xampp\htdocs\ips\php\classes.php(52): database->execute() #2
C:\xampp\htdocs\ips\try2.php(12): database->resultset() #3 {main}
thrown in C:\xampp\htdocs\ips\php\classes.php on line 49
Normally when using a prepared statement or a parameterized query you don't need to worry about delimiting string and date values; all of that is handled for you "behind the scenes".
I just tried the following and it worked for me:
<?php
$connStr =
'odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};' .
'Dbq=C:\\Users\\Gord\\Desktop\\Database1.accdb;' .
'Uid=Admin;Pwd=;';
$dbh = new PDO($connStr);
$sql =
"INSERT INTO tblDateTest (dateCol) VALUES (?)";
$newDateTimeValue = "2013-06-30 17:18:19";
$sth = $dbh->prepare($sql);
if ($sth->execute(array($newDateTimeValue))) {
echo "Done\r\n";
}
else {
$arr = $sth->errorInfo();
print_r($arr);
}
You should put your date (entire value) in the bindValue method. Example:
$stmt = $db->prepare('SELECT transactions.amount FROM transactions WHERE transactions.date = :mydate'); //prepare the query
$stmt->bindValue('mydate', '#2013-05-25#', PDO::PARAM_STR); //bind the date as a string

Inserting data into database with pdo prepared statment

Inserting data into database with pdo prepared statment, doesnt work for me:
I use this function:
public function get_number_of_matches(){
$stmt = $this->pdo->prepare("INSERT INTO `words`( `word_name`, `word_count`, `search_id`) VALUES (:word, :count,:searchID)");
$stmt->bindParam(':word', $word);
$stmt->bindParam(':count', $count);
$stmt->bindParam(':searchID', $search_id);
for($i=0;$i<count($this->words);$i++){
if(preg_match_all('/'.$this->words[$i].'/i', $this->text,$matches)){
$count=count($matches[0]);
$word=$this->words[$i];
$search_id=1;
$stmt->execute();
break;
}
}
return 0;
}
Basically, I try to loop over the values and put them into the database.. no error is given.. nothing goes into the database ..why?
This is how I connect to the database:
class DBConnection {
public static $connect;
public static function connect(){
if(!isset(self::$connect)){
try{
self::$connect=new PDO('mysql:host=localhost;dbname=tweeter', 'root', '');
}catch(Exception $ex){
echo $ex->getMessage();
}
}
return self::$connect;
}
}
UPDATE
Also..see here:
I do the same thing with a different query..but when I try to put object properties inside a variable I get an error:
$tweet= $tweet->tweet ;
$user=$tweet->tweeter_name;
$link= $tweet->link;
Those variables go into a query:
$pdo= DBConnection::connect();
$stmt = $pdo->prepare("INSERT INTO `tweets`( `tweet`, `tweeter_name`, `link`, `date`, `search_id`) VALUES (:tweet, :tweeter_name, :link, :date, :search_id)");
$stmt->bindParam(':tweet', $tweet);
$stmt->bindParam(':tweeter_name', $user);
$stmt->bindParam(':link', $link);
$stmt->bindParam(':date', $date);
$stmt->bindParam(':search_id', $search_id);
I get errors like this:
Notice: Trying to get property of non-object in C:\xampp\htdocs\Twitter\demo.php on line 36
Notice: Trying to get property of non-object in C:\xampp\htdocs\Twitter\demo.php on line 37
Notice: Trying to get property of non-object in C:\xampp\htdocs\Twitter\demo.php on line 38
I can print the properties..but when allocating them to those binded variables..the above errors crop up
I get also this:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'tweeter_name' cannot be null' in C:\xampp\htdocs\Twitter\demo.php:40 Stack trace: #0 C:\xampp\htdocs\Twitter\demo.php(40): PDOStatement->execute() #1 {main} thrown in C:\xampp\htdocs\Twitter\demo.php on line 40
I checked instead like this:
$tweet= "111111"; // $tweet->tweet ;
$user= "22222222"; // $tweet->tweeter_name;
$link= "3333333"; // $tweet->link;
$date= "444444";
and it worked..for some reason it hates those object properties ?!?
This should go as input:
RT #OrganicLiveFood: Scientists Warn #EPA Over #Monsanto's #GMO Crop Failures & Dangers #prop37 #labelGMO #yeson37 http://t.co/2XhuVxO8
Doumastic
TweetCaster for iOS
Mon, 19 Nov 2012 20:40:55 +0000
RT #OrganicLiveFood: Scientists Warn #EPA Over #Monsanto's #GMO Crop Failures & Dangers #prop37 #labelGMO #yeson37 http://t.co/2XhuVxO8
But it doesnt...?!?
Add self::$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); right after connecting.
It would make sure PDO will throw PDOExceptions on every error, making them very easy to see. The error would then outline exactly what's wrong.
Check the return value from $stmt->execute(), if there was a problem it will return false and you should check $stmt->errorInfo() for details.
Or else use the ERRMODE_EXCEPTION that #Madara Uchiha suggests, but if you're not already handling exceptions in your application, this can be hard to adapt to.
Re: your update.
You should check error status from both PDO::prepare() and PDOStatement::execute() every time you call them. The error about "Trying to get property of non-object" likely means that $stmt is actually the boolean value false instead of a valid PDOStatement object. Your call to $stmt->bindParam() fails because false is not an object, so it cannot have a bindParam() method.
In my opinion it's much easier to pass parameters by value instead of binding variables by reference. Here's an example of both error-checking and parameters by value:
$pdo = DBConnection::connect();
$sql = "INSERT INTO `tweets`( `tweet`, `tweeter_name`, `link`, `date`, `search_id`)
VALUES (:tweet, :tweeter_name, :link, :date, :search_id)";
if (($stmt = $pdo->prepare($sql)) === false) {
die(print_r($pdo->errorInfo(), true));
}
$params = array(
':tweet' => $tweet,
':tweeter_name' => $user,
':link' => $link,
':date' => $date,
':search_id' => $search_id
);
if (($status = $stmt->execute($params) === false) {
die(print_r($stmt->errorInfo(), true));
}
The error "Column 'tweeter_name' cannot be null'" that you saw in the exception means that your tweeter_name column is declared NOT NULL, but your $user variable had no value when you bound it to the :tweeter_name parameter.

Calling a function/procedure present inside an Oracle package from PHP

I am working with PHP-PDO and Oracle 11g. I am working with Oracle packages which have many functions and stored procedures. Now when I make a call to one of the functions from sql*plus or sql developer IDE, I run this command to get the result set.
select package_name.function_name(param1,param2) from dual
It works fine and returns my result set. Now when I do the same, I am getting errors from the PDO Exception handling. The code with on PHP end looks like this,
$stmt = "select package_name.function_name (?,?) from dual";
$res = $this->ConnOBJ->prepare($stmt);
$param1 = '1';
$param2 = null;
$result->bindParam(1,$param1);
$result->bindParam(2,$param2);
$result->execute();
And I get back an exception which is being logged into my log file.
Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 904 OCIStmtExecute: ORA-00904: "PACKAGE_NAME"."FUNCTION_NAME"": invalid identifier (/var/www/php-5.3.3/ext/pdo_oci/oci_statement.c:146)' in /opt/web/dir/ora_class.php:209 Stack trace: #0 /opt/web/dir/ora_class.php(209): PDOStatement->execute() #1 /opt/web/dir/ora_class.php(298): dbPDO->execPackage() #2 {main} thrown in /opt/web/dir/ora_class.php on line 209
Am I passing the query in a wrong way? Or am I binding the parameters in a wrong way?
Update
I have now got the data going through to Oracle, and have found how to pass null values. My code now is
$stmt = "select package_name.function_name(?,?) from dual";
$res = $this->ConnOBJ->prepare($stmt);
$param1 = 1;
$param2 = null;
$res->bindParam(1,$param1,PDO::PARAM_INT);
$res->bindParam(2,$param2,PDO::PARAM_NULL);
$res->execute();
var_dump($res->fetchAll());
And now when I pass data, I get back the error
PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 932 OCIStmtFetch: ORA-00932: inconsistent datatypes: expected CHAR got DTYCWD (/var/www/php-5.3.3/ext/pdo_oci/oci_statement.c:467)' in /opt/web/dir/ora_class.php:216 Stack trace: #0 /opt/web/dir/ora_class.php(216): PDOStatement->fetchAll() #1 /opt/web/dir/ora_class.php(305): dbPDO->execPackage() #2 {main} thrown in /opt/web/dir/ora_class.php on line 216
I am making sure all the types are right, but I still am getting back the same error. I even removed the null value and passed in a string, and changed the pdo type to PDO::PARAM_STR, but it still gives me the error.
Does the function take one parameter or two? In SQL*Plus, you're passing two parameters. In PHP, you're passing only one. If the function requires two parameters and there is no overloaded method that takes only one parameter, you'd get this error.
I am not using PDO anymore, I would be using OCI drivers. Thank you for all the help.
Here is a link to an answer for a similar question [LINK] : https://stackoverflow.com/a/57558306/7897970
or best
//Your connection details
$conn = oci_connect($username, $password, '(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521))(CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = XE)))' );
/* Your query string; you can use oci_bind_by_name to bind parameters or just pass the variable in it*/
$query = "begin :cur := functionName('".$param1."','".$param2."','".$param3."'); end;";
$stid = oci_parse($conn, $query);
$OUTPUT_CUR = oci_new_cursor($conn);
oci_bind_by_name($stid, ':cur', $OUTPUT_CUR, -1, OCI_B_CURSOR);
oci_execute($stid);
oci_execute($OUTPUT_CUR);
oci_fetch_all($OUTPUT_CUR, $res);
// To get your result
var_dump($res);

Categories