pdo

What is the difference between PDO and MySQLi prepared statements?

不羁岁月 提交于 2021-02-06 13:58:31
问题 What is the difference between these two prepared statements? 1 $stmt = $pdo->prepare('SELECT * FROM employees WHERE name = :name'); $stmt->execute(array('name' => $name)); foreach ($stmt as $row) { // do something with $row } 2 $stmt = $dbConnection->prepare('SELECT * FROM employees WHERE name = ?'); $stmt->bind_param('s', $name); $stmt->execute(); $result = $stmt->get_result(); while ($row = $result->fetch_assoc()) { // do something with $row } i checked many courses about prepared

What is the difference between PDO and MySQLi prepared statements?

对着背影说爱祢 提交于 2021-02-06 13:58:14
问题 What is the difference between these two prepared statements? 1 $stmt = $pdo->prepare('SELECT * FROM employees WHERE name = :name'); $stmt->execute(array('name' => $name)); foreach ($stmt as $row) { // do something with $row } 2 $stmt = $dbConnection->prepare('SELECT * FROM employees WHERE name = ?'); $stmt->bind_param('s', $name); $stmt->execute(); $result = $stmt->get_result(); while ($row = $result->fetch_assoc()) { // do something with $row } i checked many courses about prepared

Mocking PDO with phpunit

笑着哭i 提交于 2021-02-06 09:58:23
问题 I am trying to mock PDO object to use when writing some tests with phpunit, but I find it pretty complicated and can't find too much documentation about it. I created this xml structure: <dataset> <table name="providers"> <column>id</column> <column>name</column> <column>description</column> <row> <value>1</value> <value>provdier_1</value> <value>phpunit first provider</value> </row> </table> </dataset> and now I want to query providers table and get the data back but I just cant figure out

Is it safe to get ID through lastInsertId()? [duplicate]

霸气de小男生 提交于 2021-02-05 12:13:43
问题 This question already has an answer here : MySQL and the chance of the wrong id being returned by LAST_INSERT_ID() (1 answer) Closed 6 years ago . I've a database with hundreds of records which inserted in each seconds, I have the below insert query and I want to know the insert id of this query: $currQuery = $pdo->prepare("INSERT INTO some_table (...... )"); $currQuery->execute("...."); $queryInsertId = $pdo->lastInsertId(); I just want to know that is it safe to use lastInsertId() while I

PDO displaying data from database foreach specific ID

社会主义新天地 提交于 2021-02-05 11:54:24
问题 if member logon they have url like index.php?id=5 $id = $_GET['id'] I can show the user data by doing this $pdo = Database::connect(); $sql = 'SELECT * FROM data WHERE id_member = "5" ORDER BY tgl DESC'; foreach ($pdo->query($sql) as $row) { echo '<td>'. $row['tgl'] . '</td>'; } but if i change to this, nothing happen. $pdo = Database::connect(); $q = $pdo->prepare('SELECT * FROM data WHERE id_member = $id ORDER BY tgl DESC'); $q->bindValue(':id', $id, PDO::PARAM_INT); foreach ($pdo->query($q

Remotely connecting to a MySQL database using PHP PDO

烈酒焚心 提交于 2021-02-05 11:24:06
问题 here i am trying to connect to MySQL database using PHP-PDO from remote server using IP address. when put ip address in place of host it gives me following error Warning: PDO::__construct(): php_network_getaddresses: getaddrinfo failed: No such host is known. in D:\xampp\htdocs\oppInsights\database\Database.php on line 32 Fatal error: Uncaught exception 'Exception' with message 'SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: No such host is known. ' in D:\xampp\htdocs

PHP, PDO, MySQL, Notice: Trying to get property of non-object

别等时光非礼了梦想. 提交于 2021-02-05 08:26:06
问题 I'm still trying to wrap my head around php, so sorry if this is a simple mistake (I've been searching for quite a while and can only manage to get different errors such as "undefined index") What I'm attempting to do is have a function that will get data from a table (it doesn't contain a great deal at the moment, but will eventually contain everything for each main webpage). It should take an id then give the correct template and content for that page (just hard coding the $pageName in for

Multiple Create Trigger query in PDO

╄→尐↘猪︶ㄣ 提交于 2021-02-05 08:09:16
问题 I am trying to run the following SQL string in PDO. I can confirm that the code does execute in MySQL directly but running it in PHP PDO it throws an error. I understand that the DELIMITER $$ is not available on the PDO interface. And according to how to execute mysql command DELIMITER it should be fine to just leave the delimiter out of the query. QUERY STRING: CREATE DEFINER=CURRENT_USER TRIGGER `M5_tblMVTransactionVat_VatInsert` AFTER INSERT ON `M2_tblVatRevisions` FOR EACH ROW BEGIN

In PHP PDO how to get “RETURNING” clause values of PostgreSQL upsert query

时光总嘲笑我的痴心妄想 提交于 2021-02-05 07:22:05
问题 I have this upsert query written in postgreSQL $statement = 'INSERT INTO "CharactersUnlockToBuyLevels" ("CharacterId", "LevelId", "AmountToBuy", "EagleStatueId", "Location", "MapCoordinateTop", "MapCoordinateLeft") VALUES (:CharacterId, :LevelId, :AmountToBuy, :EagleStatueId, :Location, :MapCoordinateTop, :MapCoordinateLeft) ON CONFLICT ("CharacterId") DO UPDATE SET "LevelId" = EXCLUDED."LevelId", "AmountToBuy" = EXCLUDED."AmountToBuy", "EagleStatueId" = EXCLUDED."EagleStatueId", "Location" =

Insert BIT value in MySQL using PDO Prepared Statement

眉间皱痕 提交于 2021-02-05 04:55:47
问题 How do I insert a BIT value in MySQL using a PDO Prepared Statement? Below is what I tried and my results. <?php function testIt($value) { $sql='INSERT INTO test(id,data) VALUES(?,?)'; $stmt=db::db()->prepare($sql); $stmt->execute(array(0,$value)); $id=db::db()->lastInsertId(); $sql='SELECT * FROM test WHERE id='.$id; $stmt=db::db()->query($sql); $rs=$stmt->fetch(PDO::FETCH_ASSOC); echo("Test for {$value} returns id {$rs['id']} and data {$rs['data']}<br>"); } date_default_timezone_set(