So i have this query and i want it to echo it or print it somehow.
$my_query = $db->prepare("DECLARE #item varbinary(1728); SET #item = (SELECT Inventory FROM Character WHERE Name='CharName'); print #item");
$my_query->execute();
$my_query = $my_query->fetch();
echo $my_query[0] // give me error
But is not working is giving me error.
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[24000]: Invalid cursor state: 0
Problem:
You cannot call multiples queries in prepare() method, only one.
Solution:
Move the SQL into a stored procedure, then call the stored procedure to return the value
Related
I have this problem, when i am printing the second parameter from mysqli query it prints out the query to be performed but when i print the first parameter it prints nothing and when I perform the query it produces an error and it cannot insert the data into database. Here's my code:
$Insert_Patient_Data = "INSERT INTO patient_account(P_Password,P_Fname,P_Lname,P_Mname,P_Age,P_Gender,P_Email)
VALUES('$Encrypted_Password', '$Inputted_First_Name', '$Inputted_Last_Name', '$Inputted_Middle_Name', '$Inputted_Age', '$Inputted_Gender', '$Inputted_Email')";
$Patient_Query = mysqli_query($Connection, $Insert_Patient_Data);
if(!$Patient_Query)
echo "<script type = 'text/javascript'> alert('Error: Database Connection error. Please try again Later.') </script>";
else
echo "<script type = 'text/javascript'> alert('Succesfully Registered.') </script>";
I tried to print the second parameter which is $Insert_Patient_Data , it prints the query. there is nothing wrong in the output.
I tried to print the first paramter which is $Connection and it gives an error: Recoverable fatal error: Object of class MySQLi could not be converted to string
I tried to print the $Patient_Query but it prints nothing, even the second parameter (which is $Insert_Patient_Data) became null at this line.
At this time i tried to perform the $Patient_Query (Not printing it) and it goes to the if(!$Patient_Query) decision.
Question: Why it becomes a null? and what happens? and what is the solution on it? There are some queries on this page accessing the same table but they had no problem.
P.S. i checked the table name and column names from the database and they are all okay. I also included the System_Connector.php (Where the connection to database and the variable $connection is declared).
var_dump($Connection) will show you have a mysqli object, you cant echo objects!
Anyway, call mysqli_error($Connection) if it fails http://php.net/manual/en/mysqli.error.php
$Patient_Query = mysqli_query($Connection, $Insert_Patient_Data) or die(mysqli_error($Connection));
Or even:
if(!$Patient_Query = mysqli_query($Connection, $Insert_Patient_Data);)
//fail
else
// pass
My controller.php is including the connection to the database, and I'm accessing information from my database with the following code:
function getWidgets(){
$widgetQ = "SELECT * FROM tblfinal WHERE Type = 'Widget'";
global $myPdo;
$myCommand = $myPdo->prepare($widgetQ);
$myCommand->execute();
return $myCommand;
}
However, it doesn't seem to like my SQL statement, and gives me the following error:
Fatal error: Call to a member function prepare() on null on line 11
I think this means that my SQL statement isn't actually pulling anything from my database.
Any tips?
is there something wrong with my code, it look exactly like the example on the php page but it give me this error Fatal error: Call to a member function bindParam() on a non-object in /Applications/XAMPP/xamppfiles/htdocs/videosharing/index.php on line 68
$hi = 'hi';
$limit = 4;
$isi = 1;
$query = "SELECT `videoname`,`username`,`videourl`,`uploaddate`,`duration`,`views`,`tags` FROM `videolist` WHERE `tags` = :atagz ";
$stmt = $connection->prepare($query);
$stmt->bindParam(':atagz',$hi);
Your connection is likely fine (otherwise, you'd have a different error, sooner).
If the error is "Fatal error: Call to a member function bindParam() on a non-object", then $stmt isn't an object. In other words, your prepare() call is failing. Per the documentation for prepare(), that occurs when the database can't prepare the statement.
Reporting these errors is one of the areas where I think PDO falls short. You can get more information on the error with the following:
var_dump($connection->errorInfo());
The most likely cause is a misspelling in an attribute or table name.
I'm using this page to convert my queries from mysql to PDO, but it's breaking whenever the database query returns an empty result and gives this error message:
Fatal error: Call to a member function query() on null in ...
This is the line that is producing the error:
$Result =$db_val->query($Query)
Were at that state the $Query is equal to "Select * from cms_roleassign WHERE rid='5' " and that returns nothing.
EDIT:
adding in some bit of the code where $db_val is initialized:
$db_val = new PDO("sqlsrv:server=".$config['DBHostName'].";database=".$config['DBName']
, $config['DBUserName']
, $config['DBPassword']);
I have index.php page and in the title tag I have something like this:
<title><?php echo getBasic('title'); ?></title>
And it's returning the following error:
Fatal error: Call to a member function bind_param() on a non-object in C:\Program Files\WAMP\www\Filmovi\modules\database\dbcon.php on line 12
And in dbcon.php included on the top of the index with require_once('modules/database/dbcon.php') I have this:
function getBasic($type){
global $db;
$sql='SELECT content FROM a853_filmovi WHERE type = ?';
$stmt = $db->prepare($sql);
$stmt->bind_param('s',$type); <-- Line 12
$stmt->execute();
$stmt->bind_result($content);
return $content;
}
On the line number 3 I have this:
$public = getBasic('public');
and it's working perfectly.
By the way, this worked and showed the title properly and then stopped working because of an uknown reason. I don't get it how is it working with getBasic('public') but not with the title. I have a record with the type 'title' in the database so that's not a problem.
Thanks in advance.
Errors like this happen because you are not checking return values before using them.
In this case the error happens because $db->prepare($sql) fails, returns false, and then you use it as if it is a statement (stmt) object.
Check your return values before using them:
$stmt = $db->prepare($sql);
if ($stmt === false) {
die('Preparing SQL string failed');
}
One reason for the error is, prepare() is getting failed -
if the sql statement sent to it is not valid in the current DB.
prepare() will then return false.
Eg - if the table name is not correct or one or more field in the query does not exist.