get row count in pdo class - after fetching data - php

this is my pdo class
<?php
class Database
{
protected $dbh;
protected $query;
public $rows;
public function __construct($host,$user,$pass,$dbname)
{
// Set DSN
$dsn = 'mysql:host=' . $host . ';dbname=' . $dbname;
// Set options
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
// Create a new PDO instanace
try
{
$this->dbh = new PDO($dsn, $user, $pass, $options);
}
catch(PDOException $e)
{
echo $this->error = $e->getMessage();
die();
}
}
public function query($query)
{
$this->query = $this->dbh->query($query);
}
public function fetchAllQuery()
{
return $this->query->fetchAll(PDO::FETCH_ASSOC);
}
public function rowCountQuery()//--------------------------------(1)
{
//for get all the rows when query execute
$arr = $this->fetchAllQuery()
$this->rows = count($arr);
}
}
$database = new Database('localhost','root','','speed');
$area='aa';
$price=200;
$query = $database->query("SELECT * FROM tbl_deliverygrid");
$result = $database->fetchAllQuery();///-------------------------------(2)
echo '<pre>',print_r($result),'</pre><br>';
echo 'row count query [['.count($result).']]<br>';
$database->rowCountQuery();//---------------calling (1)
echo '['.print_r($database->rows).']';
echo '<br>';
i want to get row count of a query
for that instead of using rowCount() method i use fetch all the rows to variable as a array and then count the elements of that array
rowCountQuery method consist above functionality
but i cant get row count now??? method returns null output
but if i remove (2) i can get and echo the output from rowCountQuery method
corrrectly
i want to know how to get row count not removing (2)

<?php
class Database
{
protected $dbh;
protected $query;
public $rows;
protected $count;
public function __construct($host,$user,$pass,$dbname)
{
// Set DSN
$dsn = 'mysql:host=' . $host . ';dbname=' . $dbname;
// Set options
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
// Create a new PDO instanace
try
{
$this->dbh = new PDO($dsn, $user, $pass, $options);
}
catch(PDOException $e)
{
echo $this->error = $e->getMessage();
die();
}
}
public function query($query)
{
$this->query = $this->dbh->query($query);
}
public function fetchAllQuery()
{
$result = $this->query->fetchAll(PDO::FETCH_ASSOC);
$this->count = count($result);
return $result;
}
public function rowCountQuery()//--------------------------------(1)
{
return $this->count;
}
}
$database = new Database('localhost','root','','speed');
$area='aa';
$price=200;
$query = $database->query("SELECT * FROM tbl_deliverygrid");
$result = $database->fetchAllQuery();///-------------------------------(2)
echo '<pre>',print_r($result),'</pre><br>';
echo 'row count query [['.count($result).']]<br>';
echo '['.print_r($database->rowCountQuery()).']';
echo '<br>';

You are consecutively fetching a result set twice in the same script, which will result in a null result for the 2nd request. Since you already have the result set after (2), you can execute count() on $result.

Related

Insert multiple data into different tables in a single transaction

This has nothing to do with Frameworks because it is an external project, I am currently developing to learn, some things about database management or how databases behave...
I need help with some php and PDO... Previously I dev this script:
<?php
class DataBaseManager
{
public function GetData($query, $user, $pass)
{
try {
$db_result = [];
$conn = new PDO("mysql:host=localhost;dbname=test", $user, $pass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->exec("set names utf8");
reset($query);
$db_name = key($query);
$conn->exec('USE ' . $db_name);
$db_result['r'] = $conn->query($query[$db_name], PDO::FETCH_ASSOC);
$count = $db_result['r']->rowCount();
$db_result['c'] = $count;
if (0 == $count) {
$db_result['r'] = null;
} elseif (1 == $count) {
$db_result['r'] = $db_result['r']->fetch();
}
return $db_result;
} catch (PDOException $e) {
echo $e->getMessage();
return false;
}
}
public function DeleteData($query, $user, $pass)
{
try {
$conn = new PDO("mysql:host=localhost;dbname=test", $user, $pass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->beginTransaction();
$conn->exec("set names utf8");
foreach ($query as $db_name => $query_arr) {
$conn->exec('USE ' . $db_name);
foreach ($query_arr as $key => $query_string) {
$conn->exec($query_string);
++$ct;
}
}
$conn->commit();
$conn = null;
return '<b>' . $ct . ' Records Deleted Successfully.</b>';
} catch (PDOException $e) {
$conn->rollback();
echo $e->getMessage();
return false;
}
}
public function SetData($query, $user, $pass)
{
try {
$db_result = [];
$conn = new PDO("mysql:host=localhost;dbname=test", $user, $pass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->beginTransaction();
$conn->exec("set names utf8");
$count = ['u' => 0, 'i' => 0];
foreach ($query as $db_name => $query_arr) {
$conn->exec('USE ' . $db_name);
foreach ($query_arr as $key => $query_string) {
$cq = $conn->exec($query_string);
if (strpos($query_string, 'UPDATE') !== false) {
$count['u'] += $cq;
}
if (strpos($query_string, 'INSERT') !== false) {
$count['i'] += $cq;
}
}
}
$conn->commit();
$db_result['r'] = true;
$db_result['t'] = 'Updates: ' . $count['u'] . ', Inserts: ' . $count['i'];
return $db_result;
} catch (PDOException $e) {
$conn->rollback();
echo $e->getMessage();
return false;
}
}
}
I would like to be able to insert data by volumes in multiple tables in a single commit...
The idea of doing it that way is because if something fails I want it to be automatically rolled back in all table instances...
So I have a data structure in an array with the following content in my new class:
$dbquery =[
'test'=>[
'0' =>[
0 => 'INSERT INTO table_name_1(column1,column2) VALUES (?,?)', //mean it is the query
1 =>[value11,value12], // mean it is a row
2 =>[value21,value22], // mean it is a row
],
'1' =>[
0 => 'INSERT INTO table_name_2(column1,column2,column3,column4) VALUES (?,?,?,?)', //mean it is the query
1 =>[value11,value12,value13,value14], // mean it is a row
2 =>[value21,value22,value23,value24], // mean it is a row
],
],
];
but i have my first try with bindValue, this is my new class mentioned:
<?php
class DataBase
{
private static ?DataBase $instance = null;
public static function getInstance(): DataBase
{
if (!self::$instance instanceof self) {
self::$instance = new self();
}
return self::$instance;
}
private array $config;
public function __construct()
{
$this->config = ['DB_HOST'=>'test','DB_NAME'=>'test','DB_USER'=>'test','DB_PASSWORD'=>''];
$this->setConnection(new PDO("mysql:host=" . $this->config['DB_HOST'] . ";dbname=" . $this->config['DB_NAME'], $this->config['DB_USER'], $this->config['DB_PASSWORD']));
}
private PDO $connection;
/**
* #return PDO
*/
private function getConnection(): PDO
{
return $this->connection;
}
/**
* #param PDO $connection
*/
private function setConnection(PDO $connection): void
{
$this->connection = $connection;
}
public function changeConnectionServer(string $host, string $db_name, string $user, string $password): void
{
$this->setConnection(new PDO("mysql:host=" . $host . ";dbname=" . $db_name, $user, $password));
}
private array $query;
public function setDataBaseTarget(string $db_name)
{
if (empty($this->query)) {
$this->query = [];
}
$this->query[$db_name] = [];
}
public function buildQuery(string $query)
{
if (empty($this->query)) {
$this->query = [];
$this->query[$this->config['DB_NAME']] = [];
}
$target = array_key_last($this->query);
$this->query[$target][] = [$query];
}
public function addQueryData($data)
{
$target = array_key_last($this->query);
$key = array_key_last($this->query[$target]);
$this->query[$target][$key][] = $data;
}
private function getQuery(): array
{
return $this->query;
}
/**
* #throws Exception
*/
public function setData(): array
{
try {
$time = -microtime(true);
$con = $this->getConnection();
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$con->beginTransaction();
$con->exec("set names utf8;");
foreach ($this->getQuery() as $db_name => $query_arr) {
$con->exec('USE `' . $db_name . '`;');
$ct = 0;
// on this section have proble with code and logic .... i dont know what i need to dev to insert the data
foreach ($query_arr as $query_structure) {
foreach ($query_structure as $key => $raw) {
if ($key === 0) {
$ct++;
$stmt[$ct] = $con->prepare($raw);
} else {
if (is_array($raw)) {
$c = 0;
foreach ($raw as $value) {
$c++;
$stmt[$ct]->bindValue($c, $value, $this->getParamType($value));
}
}
}
}
$stmt[$ct]->execute();
}
//end section
}
//$con->commit();
return true;
} catch (PDOException $e) {
$con->rollback();
echo $e->getMessage();
return false;
}
}
private function getParamType($value)
{
if (is_int($value)) {
return PDO::PARAM_INT;
} elseif (is_bool($value)) {
return PDO::PARAM_BOOL;
} elseif (is_null($value)) {
return PDO::PARAM_NULL;
} elseif (is_string($value)) {
return PDO::PARAM_STR;
} else {
return false;
}
}
}
$db_handler = DataBase::getInstance();
$db_handler->buildQuery("INSERT INTO `client_list`(`email`,`mobile`) VALUES ('?','?');");
$db_handler->addQueryData(['mail1#test.com', '35634634636546']);
$db_handler->addQueryData(['mail2#test.com', '35634634636546']);
$db_handler->addQueryData(['mail3#test.com', '35634634636546']);
$db_handler->setData();
I can't figure out, develop the part that allows me to package everything in a single transaction... what I have is a stm[] ...
Can someone help me with this development?
I can't make heads or tails of all the things your class is doing, but here's a generalized version according to the bits that seem obvious:
public function runMultipleQueries() {
$dbh = $this->getConnection();
// ... setup stuff
$dbh->beginTransaction();
try {
foreach($this->queries as $query) {
$stmt->prepare($query->queryString);
$param_id = 1;
foreach($query->params as $param) {
$stmt->bindValue($param_id++, $param);
}
$stmt->execute();
}
} catch( \Exception $e ) {
$dbh->rollback();
throw $e;
// IMO if you cannot fully/meaningfully recover, just re-throw and let it kill execution or be caught elsewhere where it can be
// otherwise you're likely having to maintain a stack of if(foo() == false) { ... } wrappers
}
$dbh->commit();
}
Additionally, singleton DB classes have the drawbacks of both being limiting if you ever need a second DB handle, as well as boiling down to being global state, and subject to the same "spooky action at a distance" problems.
Consider using dependency inversion and feeding class dependencies in via constructor arguments, method dependencies via method arguments, etc. Eg:
interface MyWrapperInterface {
function setQueries(array $queries);
function runQueries();
}
class MyDbWrapper implements MyWrapperInterface {
protected $dbh;
public function __construct(\PDO $dbh) {
$this->dbh = $dbh;
}
public function setQueries(array $queries) { /* ... */ }
public function runQueries() { /* ... */ }
}
class MyOtherThing {
protected $db;
public function __construct( MyWrapperInterface $db ) {
$this->db = $db;
}
// ...
}
$wrapper1 = new MyDbWrapper(new PDO($connstr1, $options));
$wrapper2 = new MyDbWrapper(new PDO($connstr2, $options));
$thing1 = new MyOtherThing($wrapper1);
$thing2 = new MyOtherThing($wrapper2);
$thing1_2 = new MyOtherThing($wrapper1);
// etc

Master and slave design pattern work with transaction

<?php
class Database()
{
public function __conscruct()
{
$dsnMaster = 'mysql:host=' . $config['host']['master'] . ';dbname=' . $config['database'] . ';charset=utf8';
$this->dbhMaster = new PDO($dsnMaster, $config['username'], $config['password'], $options);
$dsnSlave = 'mysql:host=' . $config['host']['slave'] . ';dbname=' . $config['database'] . ';charset=utf8';
$this->dbhSlave = new PDO($dsnSlave, $config['username'], $config['password'], $options);
}
public function query($query)
{
if (preg_match('/^select /i', $query) > 0) {
$this->dbh = $this->dbhMaster;
} else {
$this->dbh = $this->dbhSlave;
}
$this->stmt = $this->dbh->prepare($query);
}
public function execute()
{
return $this->stmt->execute();
}
public function beginTransaction()
{
return $this->dbh->beginTransaction();
}
}
$db = new Database;
try {
$db->beginTransaction();
$db->query('SELECT * FROM `tables`');
$db->execute();
} catch (Exception $e) {
// rollback here
}
This is how I design my pdo function with master and slave connection configs, I check query strings to use SELECT to master and other actions to slave, but the transaction will be failed because I didn't declare $this->dbh in the __construct, how to fix that?
Try just adding the line:-
$this->dbh = $this->dbhMaster
in the _construct function
You could use like below
public function query($query)
{
if (strpos($query,'select') == 0) {
$this->dbh = $this->dbhMaster;
} else {
$this->dbh = $this->dbhSlave;
}
$this->stmt = $this->dbh->prepare($query);
}

Fetched SQL statement (PDO) returns an object at the 0 index o the array

I fetch some results from an sql statement I did using PDO and whenever I put it on an array it goes like this:
initialize Object ( [conn:protected] => PDO Object ( ) [container:protected] => Array ( [0] => 1,Philippine Tax,2 ) )
Whenever I FOREACH the returned array, it gives an error that it can't display an object. Here is my code:
<?php
class initialize{
protected $conn = NULL;
protected $container = array();
function __construct(){
$this->conn = $this->connect();
$ed_fetch = $this->conn->prepare("SELECT * FROM table");
if ($ed_fetch->execute()){
while ($row = $ed_fetch->fetch( )){
array_push($this->container, $row[0] . ',' . $row[1] . ',' . $row[2]);
}
}
return $this->container;
}
protected function connect(){
$this->conn = new PDO("mysql:host=localhost;dbname=db", 'root');
if (!$this->conn){
echo "Error, could not connect to the database, contact the administrator";
}
else{
return $this->conn;
}
}
}
?>
My display:
$new = new initialize();
print_r($new);
Try something like this instead. The reason you are getting what you are is because you are trying to get info on the object, essentially a mapping of the object. If you are trying to return an array, you need to return it through a method, not so much through the __construct as a return though.
class Initialize
{
protected $conn = NULL;
protected $container = array();
protected $creds;
protected $username;
protected $password;
public function __construct($creds = "mysql:host=localhost;dbname=db", $username = 'root', $password = '')
{
$this->creds = $creds;
$this->username = $username;
$this->password = $password;
$this->conn = $this->connect();
}
public function Fetch($sql)
{
$ed_fetch = $this->conn->prepare($sql);
$ed_fetch->execute();
if ($ed_fetch->rowCount() > 0){
while ($row = $ed_fetch->fetch()){
$this->container[] = $row[0].','.$row[1].','.$row[2];
}
}
return $this->container;
}
protected function connect()
{
try {
$this->conn = new PDO($this->creds,$this->username,$this->password);
} catch (PDOException $e) {
echo "Error, could not connect to the database, contact the administrator<br/>"; //. $e->getMessage() .
die();
}
}
}
$query = new Initialize();
$array = $query->Fetch("SELECT * FROM table");
print_r($array);

No records returned in PHP using OOP

I am making a module in which I am fetching users from Database Using OOP.
But due to some reason , records are not fetching, and there is no mysql error.
Here is my Code:
dbsetup_class.php :
<?php
class mySQL{
var $host;
var $username;
var $password;
var $database;
public $dbc;
public function connect($set_host, $set_username, $set_password, $set_database)
{
$this->host = $set_host;
$this->username = $set_username;
$this->password = $set_password;
$this->database = $set_database;
$this->dbc = mysqli_connect($this->host, $this->username, $this->password, $this->database) or die('Error connecting to DB');
}
public function query($sql)
{
/* echo "<pre>";
var_dump($this->dbc);
*/
//echo $sql;
return mysqli_query($this->dbc, $sql) or die('Error querying the Database');
}
public function fetch($sql)
{
$array = mysqli_fetch_array($this->query($sql));
return $array;
}
public function close()
{
return mysqli_close($this->dbc);
}
}
?>
And here is my index.php:
<?php
require_once("dbsetup_class.php");
$connection = new mySQL();
$connection->connect('localhost', 'admin', 'admin', 'oop_test');
//die('success');
$myquery = "SELECT * FROM users";
$query = $connection->query($myquery);
$array = $connection->fetch($query);
while($array)
{
echo $array['first_name'] . '<br />';
echo $array['last_name'] . '<br />';
}
$connection->close();
?>
What I am doing here?
Your fetch method expects SQL query, and not the result of a query. You should redefine it as (assuming that the client code is what you want as an interface):
public function fetch($resource)
{
$array = mysqli_fetch_array($resource);
return $array;
}
Also if you have results, your while will be infinite.

PDO query class

I'm tinkering with a class that 'should' allow me to easily execute a fetchall query and display the results within a foreach statement. I assume all is working correctly as I have no errors. As for the foreach - this must be the problem? How would I foreach the results gained from the $connect->query()? I'm new to using any database OOP framework in my functions so I could be along the wrong lines completely.
<?
error_reporting(1);
class dbconnect {
private $host;
private $database;
private $username;
private $password;
private $pdo;
private $error;
public function __construct() {
$this->host = "localhost"; // Host
$this->database = "images"; // Database Name
$this->username = "*"; // Username
$this->password = "*"; // Password
$options = array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
);
try {
$this->pdo = new PDO("mysql:host={$this->host};dbname={$this->dbname};charset=utf8", $this->username, $this->password, $options);
}
catch(PDOException $e) {
$this->error = $e->getMessage();
}
}
public function query($query) {
try {
$stmt = $this->pdo->prepare($query);
$stmt->execute();
} catch(PDOException $ex) {
die("Failed to run query: " . $ex->getMessage());
}
$rows = $stmt->fetchAll();
return $rows;
}
}
$connect = new dbconnect;
$rows = $connect->query("select * from photos");
foreach($rows as $row):
print $row['id'];
endforeach;
?>
The $rows variable you're declaring inside query is not accessible to the outside, it is local to that function. Most likely, you simply want to return those results to the caller:
$rows = $stmt->fetchAll();
return $rows; // return value from function...
and have the caller capture that return value in its own variable:
$rows = $connect->query("select * from images"); // ... is received by caller
foreach($rows as $row):
Also check out dougjore's answer, you're mixing $this->stmt and $stmt inside your query method.
Pretty sure you aren't ever actually executing the query:
$this->stmt = $this->pdo->prepare($query);
$stmt->execute();
I believe (I could be wrong, I'm rather new to PDO myself and I haven't built a class for it), that you need to say $this->stmt->execute();
You could do
//PDO::FETCH_ASSOC: returns an array indexed by column name as returned in your result set
$this->stmt = $this->pdo->prepare($query);
$this->stmt->execute();
while ($result = $this->stmt->fetch(PDO::FETCH_ASSOC))
{
//do something with the result
}
Have a look here for more options to fetch PDO query results:
http://php.net/manual/en/pdostatement.fetch.php
$connect = new dbconnect;
$sql="select * from photos";
$stmt=$connect->pdo->prepare($sql);
$stmt->execute();
$result=$stmt->fetch(PDO::FETCH_ASSOC);
foreach($result as $key => $value) {
echo $key . "-" . $value . "<br/>";
}

Categories