I have a query result. Here is my code.
$P = new Product();
echo "<pre>";
print_r($P->get_product_image_path(1));
echo "</pre>";
In Product Class i have this function.
public function get_product_image_path($productid){
$sql = "SELECT picture FROM ".PREFIX."product_picture WHERE product_id = :id";
$this->query($sql);
$this->bind(':id', $productid);
if ($this->rowCount() > 0)
return $this->queryResults();
else
return NULL;
}
Here is my database class functions
public function query($sql)
{
return $this->_sql = $this->getPdo()->prepare($sql);
}
public function bind($param, $value, $type = null){
if (is_null($type)) {
switch (true) {
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
$this->_sql->bindValue($param, $value, $type);
}
public function execute(){
return $this->_sql->execute();
}
public function queryResults(){
$this->execute();
return $this->_sql->fetchAll(PDO::FETCH_ASSOC);
}
The result is returning
Array
(
[0] => Array
(
[picture] => 1328630682_1_xl.jpg
)
[1] => Array
(
[picture] => 1328630696_1_xl.jpg
)
[2] => Array
(
[picture] => 1328630689_1_xl.jpg
)
)
But i want to merge results like that
Array
(
[0] => 1328630682_1_xl.jpg
[1] => 1328630696_1_xl.jpg
[2] => 1328630689_1_xl.jpg
)
How can i do that. I'm new on PDO that's why i couldn't do it.
get rid of your database class.
do not extend your product class from database class.
use PDO to get an array in the desired form
here it goes
public function get_product_image_path($productid)
{
$sql = "SELECT picture FROM ".PREFIX."product_picture WHERE product_id = :id";
$stm = $this->pdo->prepare($sql);
$stm->execute(array($productid));
return $stm->fetchAll(PDO::FETCH_COLUMN, 0);
}
$pdo = new PDO(...);
$P = new Product($pdo);
echo "<pre>";
print_r($P->get_product_image_path(1));
echo "</pre>";
I wrote a new database class function
public function queryColumn($index = NULL){
$index = (isset($index)) ? intval($index) : 0;
$this->execute();
return $this->_sql->fetchAll(PDO::FETCH_COLUMN,$index);
}
And i edited my function
public function get_product_image_path($productid){
$sql = "SELECT picture FROM ".PREFIX."product_picture WHERE product_id = :id";
$this->query($sql);
$this->bind(':id', $productid);
if ($this->rowCount() > 0)
return $this->queryColumn();
else
return NULL;
}
Hence, i manage to merging arrays.
Related
I have a sql UPDATE query in PDO that should update a name and a permissions string.
But instead it just places the id of that row inside all columns.
Here is my code:
public function saveRole($roleID, $name, $permissions)
{
$sql = "UPDATE roles SET name = :name, permissions = :permissions WHERE id = :id";
//this one sets a string variable in the PDO wrapper class
PDO::setSQL($sql);
//this one sets an array inside the PDO wrapper class
PDO::setData([
'name' => $name,
'permissions' => $permissions,
'id' => $roleID,
]);
PDO::execute();
return PDO::getResponse(true);
}
As you see, I've written a wrapper for PDO, which looks like this:
static function execute($sql = null, $data = null, $fetchmode = \PDO::FETCH_OBJ)
{
//check if data and SQL are set in function call, if so, use function call params, if not use class params ($this->SQL & $this->data)
self::connect();
try
{
$stmnt = self::$con->prepare(self::$sql);
$stmnt->setFetchMode($fetchmode);
if (sizeof(self::$data) > 0)
{
foreach (self::$data as $key => $value)
{
$stmnt->bindParam(':' . $key, $value);
}
}
$stmnt->execute();
self::$stmnt = $stmnt;
self::$data = [];
self::$sql = '';
self::$lastResponse = new pdoReturn(true, $stmnt);
return;
} catch (\PDOException $exception)
{
self::$data = [];
self::$sql = '';
self::$lastResponse = new pdoReturn(false, $exception);
return;
}
}
function setSQL($sql) {
if (!is_string($sql))
return false;
if (strlen($sql) == 0)
return false;
$this->sql = $sql;
return true;
}
function setData($data) {
if (!is_array($data))
return false;
$this->data = $data;
return true;
}
As you see, I've written a wrapper for PDO
For the immediate fix, change
$stmnt = self::$con->prepare(self::$sql);
$stmnt->setFetchMode($fetchmode);
if (sizeof(self::$data) > 0)
{
foreach (self::$data as $key => $value)
{
$stmnt->bindParam(':' . $key, $value);
}
}
$stmnt->execute();
to
$stmnt = self::$con->prepare(self::$sql);
$stmnt->setFetchMode($fetchmode);
$stmnt->execute(self::$data);
Then read about your first database wrapper's childhood diseases and fix other issues such as statefulness and error reporting.
I have an issue with my INSERT query, $pdo->execute return false, with error code 00000
Query
string 'INSERT INTO module_test (img_name, description, priority) VALUES(:img_name, :description, :priority)' (length=100)
errorInfo() return:
array (size=3)
0 => string '00000' (length=5)
1 => null
2 => null
Code:
private function Init($query, $parameters = "")
{
# Connect to database
if (!$this->bConnected) {
$this->Connect();
}
try {
# Prepare query
$this->sQuery = $this->pdo->prepare($query);
# Add parameters to the parameter array
$this->bindMore($parameters);
# Bind parameters
if (!empty($this->parameters)) {
foreach ($this->parameters as $param => $value) {
$type = PDO::PARAM_STR;
switch ($value[1]) {
case is_int($value[1]):
$type = PDO::PARAM_INT;
break;
case is_bool($value[1]):
$type = PDO::PARAM_BOOL;
break;
case is_null($value[1]):
$type = PDO::PARAM_NULL;
break;
}
// Add type when binding the values to the column
$this->sQuery->bindValue($value[0], $value[1], $type);
}
}
# Execute SQL
var_dump($query);
var_dump($this->sQuery->execute());
var_dump($this->sQuery->errorInfo());
}
catch (PDOException $e) {
# Write into log and display Exception
echo $this->ExceptionLog($e->getMessage(), $query);
die();
}
# Reset the parameters
$this->parameters = array();
}
public function query($query, $params = null, $fetchmode = PDO::FETCH_ASSOC)
{
$query = trim(str_replace("\r", " ", $query));
$this->Init($query, $params);
$rawStatement = explode(" ", preg_replace("/\s+|\t+|\n+/", " ", $query));
# Which SQL statement is used
$statement = strtolower($rawStatement[0]);
if ($statement === 'select' || $statement === 'show') {
return $this->sQuery->fetchAll($fetchmode);
} elseif ($statement === 'insert' || $statement === 'update' || $statement === 'delete') {
return $this->sQuery->rowCount();
} else {
return NULL;
}
}
public function insert($table, $keyValue)
{
$fieldString = '';
$valueString = '';
$i = 1;
foreach ($keyValue as $key => $currKeyValue)
{
$fieldString .= $key;
$valueString .= ':'.$key;
if($i != count($keyValue))
{
$fieldString .= ', ';
$valueString .= ', ';
}
$i++;
}
$query = 'INSERT INTO '.$table.' ('.$fieldString.') VALUES('.$valueString.')';
$this->query($query, $keyValue);
}
Parameters array
F:\Dev\wamp\wamp64\www\include\class\Database.class.php:216:
array (size=3)
'img_name' => string 'ttt1' (length=4)
'description' => string 'ttt1' (length=4)
'priority' => int 0
I already try this query in phpmyadmin and everything worked well.
If someone know how to solve this?
thanks
PS: sorry for my bad english
PDO is reported not to fill the errorInfo property in certain circumstances.
Instead, you have to make it throw an exception, which is the most reliable way to get the error message. To do so, in your constructor, add this line
$this->pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
Also note that your class is a genuine example of all the mistakes one could make writing a PDO wrapper. I compiled the most popular mistakes in an article, Your first database wrapper's childhood diseases and your class contains every single one of them.
Can someone please help me, I get this fatal error message when I try to run the code in a browser. I have searched through various topics and couldn't find the answer. I have two very similar functions and only one of them is reporting an error. Function getById is working just fine, but function get reports an error. Here's my db.php file:
class DB {
private $db;
public $limit = 5;
public function __construct($config){
$this->connect($config);
}
private function connect($config){
try{
if ( !class_exists('Mongo')){
echo ("The MongoDB PECL extension has not been installed or enabled");
return false;
}
$connection= new MongoClient($config['connection_string'],array('username'=>$config['username'],'password'=>$config['password']));
return $this->db = $connection->selectDB($config['dbname']);
}catch(Exception $e) {
return false;
}
}
public function create($collection,$article){
$table = $this->db->selectCollection($collection);
return $result = $table->insert($article);
}
public function get($page,$collection){
$currentPage = $page;
$articlesPerPage = $this->limit;
//number of article to skip from beginning
$skip = ($currentPage - 1) * $articlesPerPage;
$table = $this->db->selectCollection($collection); **//here reports an error**
$cursor = $table->find();
//total number of articles in database
$totalArticles = $cursor->count();
//total number of pages to display
$totalPages = (int) ceil($totalArticles / $articlesPerPage);
$cursor->sort(array('saved_at' => -1))->skip($skip)->limit($articlesPerPage);
//$cursor = iterator_to_array($cursor);
$data=array($currentPage,$totalPages,$cursor);
return $data;
}
public function getById($id,$collection){
// Convert strings of right length to MongoID
if (strlen($id) == 24){
$id = new MongoId($id);
}
$table = $this->db->selectCollection($collection);
$cursor = $table->find(array('_id' => $id));
$article = $cursor->getNext();
if (!$article ){
return false ;
}
return $article;
}
public function delete($id,$collection){
// Convert strings of right length to MongoID
if (strlen($id) == 24){
$id = new \MongoId($id);
}
$table = $this->db->selectCollection($collection);
$result = $table->remove(array('_id'=>$id));
if (!$id){
return false;
}
return $result;
}
public function update($id,$collection,$article){
// Convert strings of right length to MongoID
if (strlen($id) == 24){
$id = new \MongoId($id);
}
$table = $this->db->selectCollection($collection);
$result = $table->update(
array('_id' => new \MongoId($id)),
array('$set' => $article)
);
if (!$id){
return false;
}
return $result;
}
public function commentId($id,$collection,$comment){
$postCollection = $this->db->selectCollection($collection);
$post = $postCollection->findOne(array('_id' => new \MongoId($id)));
if (isset($post['comments'])) {
$comments = $post['comments'];
}else{
$comments = array();
}
array_push($comments, $comment);
return $postCollection->update(
array('_id' => new \MongoId($id)),
array('$set' => array('comments' => $comments))
);
}
}
I'm using this class to connect to database. It works just fine, except I couldn't get the lastInsertId().
<?php
class connDB
{
public function connDB()
{
require_once( 'dbconfig/config.php' );
$this->confPDO = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_PERSISTENT => false,
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES UTF8"
);
try
{
$this->dbc = new PDO( "mysql:host=$this->dbHost;dbname=$this->dbName",
$this->dbUser, $this->dbPass, $this->confPDO );
}
catch( PDOException $errMsg )
{
return false;
}
}
public function exec( $sql, array $params = array() )
{
try
{
$this->stmt = $this->dbc->prepare( $sql );
if ( count( $params ) > 0 )
{
foreach ( $params as $k=>$v )
{
$this->bind($k, $v);
}
}
return $this->stmt->execute();
}
catch( PDOException $errMsg )
{
$this->dbc = null;
return false;
}
}
public function bind( $param, $value, $type = null )
{
if ( is_null( $type ) )
{
switch ( true )
{
// Boolen parameter
case is_bool( $value ):
$type = PDO::PARAM_BOOL;
break;
// Integer parameter
case is_int( $value ):
$type = PDO::PARAM_INT;
break;
// Null parameter
case is_null( $value ):
$type = PDO::PARAM_NULL;
break;
// String parameter
default:
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue( $param, $value, $type );
}
public function single()
{
return $this->stmt->fetch(PDO::FETCH_ASSOC);
}
public function resultset()
{
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function rowCount()
{
return $this->stmt->rowCount();
}
}
Usage: [SELECT]
$sql = "SELECT * FROM < table >";
$db->exec($sql, $params);
$rows = $db->resultset();
foreach ($rows as $row)
{
echo $row['< column >'] . "\n";
}
Usage: [INSERT]
$sql = "INSERT INTO < table > (< column_1 >, < column_2 >, ... ) VALUES
(:valuename_1,
:valuename_2, ...)";
$params = array(':valuename_1' => 'value', ':valuename_2' => 'value', ...);
$db->exec($sql, $params);
I tried to do it this way:
include_once'classe.php';
$db = new connDB();
$sql = "INSERT INTO < table > (< column_1 >, < column_2 >, ... ) VALUES
(:valuename_1,
:valuename_2, ...)";
$params = array(':valuename_1' => 'value', ':valuename_2' => 'value', ...);
$db->exec($sql, $params);
$id = $db->lastInsertId();
I am getting an error:
Fatal error: Call to undefined method connDB::lastInsertId() in
I've tried adding a method into the class:
public function lastinsert()
{
// Return result
return $this->stmt->lastInsertId();
}
Then I called it like this this:
$db = new connDB();
$id = $db->lastinsert();
The error this time was
Fatal error: Call to undefined method PDOStatement::lastInsertId() in
There is no lastInsertId() method in your class.
You need to add it to the connDB class.
you need to call $dbc, not $stmt to get lastInsertId();
$this->dbc->lastInsertId();
as this function belongs to PDO class, not PDO statement class
Also, this piece of code may cause the problem
catch( PDOException $errMsg )
{
$this->dbc = null;
return false;
}
}
Make your exec() function this way
public function exec( $sql, array $params = array() )
{
$this->stmt = $this->dbc->prepare( $sql );
foreach ( $params as $k=>$v )
{
$this->bind($k, $v);
}
return $this->stmt->execute();
}
Using the same database class and was able to use lastInsertId like this:
$db = new connDB();
...
$_SESSION['users_id'] = $db->dbc->lastInsertId('id');
If you are using the model's save function:
$row->save();
You can use:
return $row->save();
which returns the id
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
This is the Query that is being run:
SELECT `fleet_pilots`.`user_id` , `pilot_tracker`.`start_time` , `fleets`.`fleet_cta`
FROM `fleet_pilots`
LEFT JOIN `fleets` ON `fleets`.`fleet_id` = `fleet_pilots`.`fleet_id`
LEFT JOIN `pilot_tracker` ON `pilot_tracker`.`user_id` = `fleet_pilots`.`user_id`
AND `pilot_tracker`.`end_time` = '0000-00-00 00:00:00'
WHERE `fleet_pilots`.`fleet_id` = '26'
AND `fleet_pilots`.`user_approved` = '1'
When I run the query in mysql I have no issues and get the results as I expect.
The problem occurs when I use the results in php:
$sql = "SELECT `fleet_pilots`.`user_id`, `pilot_tracker`.`start_time`, `fleets`.`fleet_cta`
FROM `fleet_pilots`
LEFT JOIN `fleets` ON `fleets`.`fleet_id` = `fleet_pilots`.`fleet_id`
LEFT JOIN `pilot_tracker` ON `pilot_tracker`.`user_id` = `fleet_pilots`.`user_id` AND `pilot_tracker`.`end_time` = '0000-00-00 00:00:00'
WHERE `fleet_pilots`.`fleet_id` = :fleet_id AND `fleet_pilots`.`user_approved` = '1'";
$this->db->query($sql);
$args = array(
':fleet_id' => $this->input['fleet_id'],
);
$this->db->execute($args);
$fleet_users = array();
while ( $row = $this->db->fetch_row() )
{
$fleet_users[] = $row['user_id'];
if (isset($row['start_time']) && $row['fleet_cta'])
{
$sql = "UPDATE `pilots`
SET `user_total_points` = `user_total_points` + :user_total_points, `user_points` = `user_points` + :user_points
WHERE `user_id` = :user_id";
$this->db->query($sql);
$args = array(
':user_id' => $row['user_id'],
':user_points' => $this->conf->Config['point_per_min'] * ((strtotime('now') - strtotime($row['start_time']))/60),
':user_total_points' => $this->conf->Config['point_per_min'] * ((strtotime('now') - strtotime($row['start_time']))/60),
);
$this->db->execute($args);
}
}
I get this error message:
<br />
<b>Fatal error</b>: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error' in /usr/local/apache2/vhosts/trifectas.org/htdocs/fleetmaster/libs/database.class.php:109
Stack trace:
#0 /usr/local/apache2/vhosts/trifectas.org/htdocs/fleetmaster/libs/database.class.php(109): PDOStatement->fetch(2)
#1 /usr/local/apache2/vhosts/trifectas.org/htdocs/fleetmaster/libs/common.class.php(1170): Database->fetch_row()
#2 /usr/local/apache2/vhosts/trifectas.org/htdocs/fleetmaster/libs/common.class.php(1226): Common->submit_end_fleet()
#3 /usr/local/apache2/vhosts/trifectas.org/htdocs/fleetmaster/libs/common.class.php(225): Common->process_end_fleet()
#4 /usr/local/apache2/vhosts/trifectas.org/htdocs/fleetmaster/index.php(25): Common->__construct()
#5 {main}
thrown in <b>/usr/local/apache2/vhosts/trifectas.org/htdocs/fleetmaster/libs/database.class.php</b> on line <b>109</b><br />
Line 1170 is the while ( $row = $this->db->fetch_row() ) line.
Any help is appreciated.
EDIT: The following change fixed the problem for me.
$sql = "SELECT `fleet_pilots`.`user_id`, `pilot_tracker`.`start_time`, `fleets`.`fleet_cta`
FROM `fleet_pilots`
LEFT JOIN `fleets` ON `fleets`.`fleet_id` = `fleet_pilots`.`fleet_id`
LEFT JOIN `pilot_tracker` ON `pilot_tracker`.`user_id` = `fleet_pilots`.`user_id` AND `pilot_tracker`.`end_time` = '0000-00-00 00:00:00'
WHERE `fleet_pilots`.`fleet_id` = :fleet_id AND `fleet_pilots`.`user_approved` = '1'";
$this->db->query($sql);
$args = array(
':fleet_id' => $this->input['fleet_id'],
);
$this->db->execute($args);
$row = $this->db->fetch_array();
$fleet_users = $result = array();
foreach ( $row AS $key => $value )
{
$result[$key] = $value;
}
foreach ( $result AS $row )
{
$fleet_users[] = $row['user_id'];
if ( isset($row['start_time']) && $row['fleet_cta'] )
{
$sql = "UPDATE `pilots`
SET `user_total_points` = `user_total_points` + :user_total_points, `user_points` = `user_points` + :user_points
WHERE `user_id` = :user_id";
$this->db->query($sql);
$args = array(
':user_id' => $row['user_id'],
':user_points' => $this->conf->Config['point_per_min'] * ((strtotime('now') - strtotime($row['start_time']))/60),
':user_total_points' => $this->conf->Config['point_per_min'] * ((strtotime('now') - strtotime($row['start_time']))/60),
);
$this->db->execute($args);
}
}
This is my PDO wrapper.
<?php
defined('IN_APP') || die('Hands off!');
class Database
{
protected $connection;
protected $result;
protected $params;
protected $executed = false;
protected $_queryCounter = 0;
public function __construct($dsn, $username = null, $password = null, $driver_options = null)
{
try
{
$this->connection = new PDO($dsn, $username, $password, $driver_options);
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e)
{
$this->get_error($e);
}
}
public function query($sql, $params = array())
{
$this->result = $this->connection->prepare($sql);
$this->params = is_array($params) ? $params : array($params);
$this->executed = false;
}
public function bind($pos, $value, $type = null)
{
if ( is_null($type) )
{
switch (true)
{
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
$this->result->bindValue($pos, $value, $type);
}
public function bindParam($pos, $value, $type = null)
{
if ( is_null($type) )
{
switch (true)
{
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
$this->result->bindParam($pos, $value, $type);
}
public function execute($vars = array())
{
$this->_queryCounter++;
$this->executed = true;
foreach ( $vars as $k => $v )
{
$this->bind($k, $v);
}
return $this->result->execute();
}
public function fetch_array($vars = array())
{
if ( !$this->executed )
{
$this->execute($vars);
}
return $this->result->fetchAll(PDO::FETCH_ASSOC);
}
public function fetch_row($vars = array())
{
if ( !$this->executed )
{
$this->execute($vars);
}
return $this->result->fetch(PDO::FETCH_ASSOC);
}
public function lastInsertId()
{
return $this->connection->lastInsertId();
}
public function rowCount($vars = array())
{
if ( !$this->executed )
{
$this->execute($vars);
}
return $this->result->rowCount();
}
public function beginTransaction()
{
$this->connection->beginTransaction();
}
public function commit()
{
$this->connection->commit();
}
public function queryCounter()
{
return $this->_queryCounter;
}
public function debugDumpParams()
{
return $this->result->debugDumpParams();
}
public function get_error($e)
{
$this->connection = null;
die($e->getMessage());
}
public function getConnection()
{
return $this->connection;
}
public function __destruct()
{
$this->connection = null;
}
}
PDO::fetchRow does not exist. Assuming your $this->db object is a PDO instance, this doesn't work. PDO::query returns a PDOStatement result object. You need to fetch rows from it, not from the PDO object. If this doesn't help, you need to show what your database class is doing.