lincanbin / php-pdo-mysql-class Goto Github PK
View Code? Open in Web Editor NEWA PHP MySQL PDO class similar to the the Python MySQLdb, which supports iterator and parameter binding when using "WHERE IN" statement.
License: Apache License 2.0
A PHP MySQL PDO class similar to the the Python MySQLdb, which supports iterator and parameter binding when using "WHERE IN" statement.
License: Apache License 2.0
Hi, how can i set utf8?
Work nice this library, however some rows give me this: restricción de movilidad por cédula y género en Panamá
In PDO is used SET NAMES UTF8, but in this library what should I use?
I use your code show errow
我检查过数据库权限,发现并没有设置错
I'm looking to run a union query, here's what I've got:
$get_posts = $DB->query( "SELECT * FROM op_posts WHERE post_author_id IN (:ids) UNION SELECT * FROM op_post_shares WHERE share_user_id IN (:ids)", array('ids' => array(1, 2, 54)) );
But it spits out the error:
Cardinality violation: 1222 The used SELECT statements have a different number of columns
Maybe someone could find it interesting ...
public function call($query, $params = null, $fetchmode = PDO::FETCH_ASSOC)
{
$this->Init($query, $params);
$result = $this->sQuery->fetchAll($fetchmode);
$this->sQuery->nextRowset();
return $result;
}
How to do sorting ?
ORDER BY ASC DESC
https://code.csdn.net/bluewjzhhr/somestuff/tree/master/db.pdo.class.php
由于我这里上github.com经常抽风,所以我的代码都在csdn上。
里面也有部分的注解。
This opportunity is missing:
INSERT INTO tbl (key1, key2) VALUES ('r1v1', 'r1v2'),('r2v1', 'r2v2'),...
I tried to create an array but get a PHP error.
$params=[];
$params[] = ["key1" => "value1", "key2 => "value2_1"];
$params[] = ["key1" => "value2", "key2 => "value2_2"];
$params[] = ["key1" => "value3", "key2 => "value2_3"];
$DB->query($query, $params);```
> PHP Fatal error: Uncaught ValueError: PDOStatement::bindParam(): Argument #1 ($param) must be greater than or equal to 1 in /lincanbin/php-pdo-mysql-class/src/PDO.class.php:137
函數內有沒有需要新增跳脫字元的函數?
這只是一個增加安全性的選項
另外你寫的程式真的很棒
簡單易懂,功能強大,學習很多!!
public function escape($str)
{
return substr($this->pdo->quote($str), 1, -1);
}
Deprecated
: Return type of PDOIterator::rewind() should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in
C:\laragon\www\eshool\phplib\src\PDO.Iterator.class.php
on line
14
$stmt = $db->prepare("SELECT * FROM child WHERE first_name=? AND password=?");
$stmt->execute(array($name, $Password));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
print $rows;
When init PDO then add an option (PDO::MYSQL_ATTR_FOUND_ROWS => true) to disable this unexpected behaviour.
Ex:
$this->pdo = new PDO($dsn,
$this->DBUser,
$this->DBPassword,
array(
//For PHP 5.3.6 or lower
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8",
PDO::ATTR_EMULATE_PREPARES => false,
//长连接
//PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
PDO::MYSQL_ATTR_FOUND_ROWS => true //Here
)
);
i was using old version of PDO.class.php , just copied fresh copy of PDO.class.php and replaced my old one with it , and noticed i started getting 500 error. then reverted back .
please check
There is a need to INSERT or UPDATE values if they exist.
INSERT INTO TABLE (id, name, age) VALUES (1, "A", 19), (2, "B", 17), (3, "C", 22)
ON DUPLICATE KEY UPDATE
age = 20;
I would suggest you add binding paramters in a more readable way. e.g
$db->bind('par1', 'xyz' );
$db->bind('par2', 'abc' );
$db->query('SELECT *
FROM table1
WHERE table1.a = :par1 AND table1.b = :par2 ');
PHP>=5.4
$db->query("select * from item where cc like '%,?,%'",[$aid]);
error log:SQLSTATE[42000]: Syntax error or access violation: 1065 Query was empty
I want to turn off logs, as I don't really want to be accessing this from the front end of my website.
How do I do this?
SELECT * FROM fruit WHERE name IN (apple','banana) AND color = 'red'
how to add "red" ?
$DB->query("SELECT * FROM fruit WHERE name IN (?) AND color = ? ",array('apple','banana'));
The IN statement for UPDATE seems to be interpreted wrong. It only executes for the first value:
$result= $this->DB->query("UPDATE table SET column=0 WHERE id IN (?)",array( "1, 3, 5, 6"));
This only sets column for the id=1, but there are rows with id 3,5 or 6
Otherwise the SELECT statement is working:
$result= $this->DB->query("SELECT column FROM table WHERE id IN (?)",array( "1, 3, 5, 6"));
This gets all rows of column for the selected IDs.
Do I miss something or is it a bug?
These small changes would be nice to add to the repo, so that the log path can be changed.
PDO.class.php
public function __construct($Host, $DBPort, $DBName, $DBUser, $DBPassword, $LogPath = '')
{
$this->logObject = new PDOLog($LogPath);
PDO.Log.class.php
public function __construct($LogPath = '')
{
if (empty($LogPath)) $this->path = DIR . $this->path;
else $this->path = $LogPath;
}
I used this code snippet to get the column names $result = $DB->query('DESCRIBE '.$table);
Did not work, tyried different ways, no luck.
I changed something in the class to get it to work, I added inside the function query($query, $params = null, $fetchMode = PDO::FETCH_ASSOC)
a extra statement.
if ($statement === 'select' || $statement === 'show' || $statement === 'describe')
Now it works and my function will return either a form, list or just the names.
Maybe to consider for the next update in the class to add this extra statement?
Very nice class, love it.
A good job well done is half the worries. -;)
how can i determine query are success or not ?
some like if ($execute = DB->query(blablabla)){} ?
<?php
define('DBHost', '127.0.0.1');
define('DBName', 'Database');
define('DBUser', 'root');
define('DBPassword', '');
require(dirname(__FILE__)."/src/PDO.class.php");
$DB = new Db(DBHost, DBName, DBUser, DBPassword);
?>
Why can't use ini file that puts credentials to main db function and link with main db php class, which won't require parameters to be required everytime. Just execute db() and go.
My problem is that i will check is value x in the column a or b exist.
How I can do this?
I get: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined
I tried:
$DB->query("SELECT * FROM accounting_entries WHERE soll=:nr OR haben=:nr", array('nr'=>$this->nr));
Just a little suggestion
PDO.class.php @345
// add query parameters info, because using prepared statement can't view parameters logs
if (isset($parameters['parameters'])) {
$message .= "\r\nParamters : " . print_r($parameters['parameters'], true);
}
Please change this
if ($statement === 'select' || $statement === 'show' || $statement === 'call' || $statement === 'describe') {
$fetchReturn = $this->sQuery->fetchAll($fetchMode);
$this->sQuery->closeCursor();
return $fetchReturn;
} elseif ($statement === 'insert' || $statement === 'update' || $statement === 'delete') {
return $this->sQuery->rowCount();
} else {
return NULL;
}
Integrity constraint violation: 1048 Column parentId cannot be null
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.