I have a problem with the PDO::FETCH_OBJECT argument. I want to fetch an object and not an array, but when I try this:
try {
$conn = new PDO('mysql:host=localhost;dbname=washngo', $config['DB_USERNAME'], $config['DB_PASSWORD']);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //Fetch errors by default ( display any errors during the development process )
$stmt = $conn->prepare('SELECT * FROM news');
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_OBJECT)) { //By default, it fetch an array. The "PDO::FETCH_OBJECT" argument allows us to fetch an object
print_r($row);
}
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
I get
Fatal error: Undefined class constant 'FETCH_OBJECT' in index.php on line 18.
When I try to let the fetch() by default (without PDO::FETCH_OBJECT()), it works fine.
Correct is not PDO::FETCH_OBJECT but PDO::FETCH_OBJ
Related
//this is my connection function. It is connecting databse successfully when I check.
$conn = connection($config['servername'],$config['username'],$config['password']);
after this I Used following code to fetch data from Database
$id = 2;
if($conn) {
try {
$stmt = $conn->prepare('SELECT * FROM customer_tbl WHERE cus_id = :id');
$stmt->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt->bindParam(':id', $id);
$results = $stmt->execute();
}catch (PDOException $e){
echo 'Error: ' . $e->getMessage();
}
}
this code showing following error message on the browser
Error: SQLSTATE[IM001]: Driver does not support this function: This driver doesn't support setting attributes
what's wrong with my code?. Why I could not fetch data's from database?
if I want to fetch this specified data from databese using prepare statement
how to code?
Add the following
$stmt->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
after the connection string with $conn Object
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
To fetch data use
$stmt->execute();
$rows= $stmt->fetch(PDO::FETCH_ASSOC);
print_r($rows); // to print an array
it will return data in associative array format.
PDO provides various fetch options look here
I am try to connect phpmyadmin database using my php script in openshift
but the result is a empty page.
then, I find the question is the query didn't work
but I don't know why
There is my original code
try{
$dsn = 'mysql:dbname=exampleDataBase;host=127.**.***.***;port=*****';
$dbh = new PDO($dsn, "account", "password");
$sth = $dbh->prepare('SELECT * FROM test1');
$fin = $sth->execute();
while($row = $sth->fetch(PDO::FETCH_ASSOC)){
print_r($row);
}
} catch (PDOException $e){
echo "Sytan error" . $e -> getMessage();
}
$dbh = null;
and the result is a empty page, so I modify my code
There is my modify code
try{
$dsn = 'mysql:dbname=exampleDataBase;host=127.**.***.***;port=*****';
$dbh = new PDO($dsn, "account", "password");
$sth = $dbh->prepare('jngfcjfgcnmgcm,,hmnxf');
$fin = $sth->execute();
while($row = $sth->fetch(PDO::FETCH_ASSOC)){
print_r($row);
}
} catch (PDOException $e){
echo "Sytan error" . $e -> getMessage();
}
$dbh = null;
I input the wrong query sytanx(jngfcjfgcnmgcm,,hmnxf), but it didn't return error.
Add this to your script see your errors
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors',1);
ini_set('html_errors', 1);
and change your query code to this, see notes
try{
//port=***** is only need where its different from the default
$dsn = 'mysql:host=localhost;dbname=exampleDataBase';
$options = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
$dbh = new PDO($dsn, "account", "password", $options);
$sth = $dbh->prepare('SELECT * FROM test1');
// execute $sth
$sth->execute();
//Change fetch to fetchAll
while($row = $sth->fetchAll(PDO::FETCH_ASSOC)){
print_r($row);
}
} catch (PDOException $e){
echo "Sytan error" . $e->getMessage();
}
You modified your code to a wrong statement to see the error message?
You have your PHP errors turned off, when doing a statement like:
$sth = $dbh->prepare('jngfcjfgcnmgcm,,hmnxf');
You would receive an error like:
Sytan errorSQLSTATE[42000]: Syntax error or access violation: 1064 You
have an error in your SQL syntax; check the manual that corresponds to
your MySQL server version for the right syntax to use near
'jngfcjfgcnmgcm,,hmnxf' at line 1
What do you exactly want? The exception is not showing?
I know this question has been answered before, but I actually have an error when I use an the following:
try {
$conn = new PDO('mysql:host=localhost;dbname=open', $user, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
$count=$conn->prepare(" SELECT * FROM `dictionary` WHERE word = ':input' ");
$count->bindParam(":input",$input);
$count->execute();
$no=$count->rowCount();
if( $no > 0 ){
echo "no";
}
Which gives me the error:
Fatal error: Call to a member function prepare() on a non-object in... on line 18
I don't see where the error is, particularly because I based this code off an accepted answer.
try replace this
word = ':input'
by
word = :input
I'm just beginning with PDO and have look up in several tutorials for an answer but I just can't make it work.
I got
Notice: Undefined property: PDOStatement::$fetch in E:\-------- on line 22
Result: 1
with
$dsn = "mysql:host=localhost;dbname=the_database;";
try {
$dbh = new PDO($dsn, "root", "");
$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} catch (PDOException $e){
die( "failed conexion: ".$e->getMessage() );
}
$query = "SELECT MAX(price) AS max, MIN(price) AS min FROM cellphones";
try {
$sth = $dbh->prepare($query);
$sth->execute();
$sth->setFetchMode(PDO::FETCH_ASSOC);
$result = $sth->fetchAll;
}
catch(PDOException $e){
echo $e->getMessage();
}
die( "<br />Result: ".print_r($result, true) );
I get the same result with
$sth = $dbh->query($query);
$result = $sth->fetchAll;
and
$sth = $dbh->prepare($query);
$sth->execute();
$result = $sth->fetch;
What I do get is that it might be returning the count of results
But why? And why it gives me a Notice about fetch / fetchAll not even declared.
I don't get any exception either.
You need to use the method call with paranthesis:
$sth->fetchAll();
Or
$sth->fetch();
not just
$sth->fetchAll;
PHP thinks your trying to hit a property called fetchAll!
It appears I can connect to my database using PDO, but can't execute any queries with it. Example:
private function connect() {
try {
$link = new PDO("mysql:host=$this->sHost;dbname=$this->sName", $this->sUser, $this->sPass);
}
catch (PDOException $e) {
die ($e);
}
print_r($link);
$result = $link->query("select * from mt3_users");
var_dump($result);
$row = $result->fetch($result);
die("Your id is: ".$row["id"]);
//$link = mysql_connect($this->sHost, $this->sUser, $this->sPass);
if (!$link) {
echo "Failed to connect to $this->sHost!";
return false;
}
return $link;
}
This returns the following:
PDO Object ( ) bool(false)
Fatal error: Call to a member function fetch() on a non-object in Database.php on line 32
So basically, $link is coming back as a PDO object (I changed my username and password to see if an exception was caught; it was) and PDOConnection::Query is returning null for some reason. This is my first time dealing with PDOs -- am I doing something funny?
Most likely the query fails, are you sure of the name of the table mt3_users and that you have selected the right database? That error message shows that $result is not an object and that's due to an error in the query.
Also:
$row = $result->fetch($result);
should be
$row = $result->fetch();
unless you want to specify options to fetch(), but you don't pass the object as argument.
Array ( [0] => 00000 [1] => 1046 [2] => No database selected ) Array ( )
Nevermind, I guess. It turns out that while migrating from using regular MySQL functions, I wasn't setting $this->sName ($this->sName was null. I'm half surprised it didn't throw an exception. Only half, though)
Fixed.
But thank you guys for the answers :)
can you give this a try
private function connect() {
try {
$link = new PDO("mysql:host=$this->sHost;dbname=$this->sName", $this->sUser, $this->sPass);
return $link ;
}
catch (PDOException $e) {
die ($e);
}
}
$pdolink = $this->connect();
$rows = $pdolink->query("select * from mt3_users");
foreach($rows as $row ){
echo("Your id is: ".$row["id"]);
}
and if you need to stick with fetchAll function
private function connect() {
try {
$link = new PDO("mysql:host=$this->sHost;dbname=$this->sName", $this->sUser, $this->sPass);
return $link ;
}
catch (PDOException $e) {
die ($e);
}
}
$pdolink = $this->connect();
$q = $pdolink->prepare("select * from mt3_users");
$q->exectue();
$rows = $q->fetchAll();
var_dump($rows);
foreach($rows as $row ){
echo("Your id is: ".$row["id"]);
}
In order to get the error in your query:
$result = $link->query(...);
if($result===FALSE){
print_r( $link->errorInfo);
exit();
}
// the correct way to fetch one row
$link->fetch(PDO::FETCH_ASSOC); // or whatever way you want to fetch data