pdo

pdo how to check if it is 1st record that retrieve from database?

China☆狼群 提交于 2020-01-11 13:23:16
问题 $sql3 = "SELECT member FROM levels where upline = ? AND level=1"; $q3 = $conn->prepare($sql3); $q3->execute(array("$level2downlines")); while($r3 = $q3->fetch(PDO::FETCH_ASSOC)){ $level3downlines = $r3['member']; if (it is 1st record){ echo "make orange juice"; }else{ echo "make all juice"; } } Let say output are 3 records from database "Zac", "Michelle", Andrew". "Zac" is the 1st record get from database, how to write the "if" statement to check if the records is 1st record or not? 回答1:

PHP PDO class programming:Fatal error: Call to a member function fetchAll() on boolean

落花浮王杯 提交于 2020-01-11 13:14:31
问题 I am new to the class programming in php ,Here is my database class. class Database { private $_connection; private static $_instance; //The single instance private $_host = 'localhost'; private $_username = 'root'; private $_password = ''; private $_database = 'admission_portal'; //connect to database public function connectDb() { try { $this->_connection = new \PDO("mysql:host=$this->_host;dbname=$this->_database", $this->_username, $this->_password); /*** echo a message saying we have

How to debug SQLSTATE[HY000]: General error: 2031 in prepared statements

泪湿孤枕 提交于 2020-01-11 13:12:30
问题 I have this prepared statement query $stmt = $conn->prepare(" UPDATE language SET lang_alias=:lang_alias , lang_name=:lang_name WHERE lang_id=:lang_id" ); If I set an array to bind the values $query_array = array ( ":lang_alias" => "en", ":lang_name" => "English (UK)", ":lang_id" => 1 ) ; and then execute it $stmt->execute(array($query_array)); it wont work, I get Notice: Array to string conversion referring to $stmt->execute(array($query_array)); and Uncaught exception 'PDOException' with

How do I stop MySQL from duplicating every column's entry in returned arrays?

这一生的挚爱 提交于 2020-01-11 12:22:48
问题 My MySQL queries are returning arrays with duplicate entries: numbered keys and labeled keys with the same data inside. This may be standard, but it seems like a waste, and something that could cause problems if I'm printing values. I mean, not a huge problem, obviously. But I'm just curious if I can stop that. It seems unnecessary. For example: Array( [0] => "Ted", [first_name] => "Ted", [1] => "Schmidlap", [last_name] => "Schmidlap" ) And so on. I'm pretty new to a lot of this, so this may

'PDOException' with message 'SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active

好久不见. 提交于 2020-01-11 09:41:51
问题 I know this question has been asked many times, but the commonly-accepted answers didn't help me. But quite by accident I stumbled on an answer. Here's the setup: I had a load of queries (mostly CREATE TABLE) going through a connection. But a CREATE TRIGGER kept throwing up the dreaded 2014 error. This was nothing to do with open cursors, becasue it happened even when it was the only command in the program. This, for example, failed: <?php $db = new PDO ($cnstring, $user, $pwd); $db-

PDO SQLite query zero result issue

随声附和 提交于 2020-01-11 09:16:52
问题 I've had a look around but can't seem to find any information on this. I'm not sure if it's an issue with my code or a known issue with in-memory SQLite databases and PDO. Basically, after inserting a single row into an in-memory SQLite database table, I'd expect that a query that doesn't match the inserted item to return zero rows. However, the following code gives a single row (false) but no actual PDO error code. <?php // Create the DB $dbh = new PDO('sqlite::memory:'); $dbh->setAttribute

Opening SQLite3 as READONLY with PDO?

北慕城南 提交于 2020-01-11 08:12:30
问题 The SQLite3 Class has an option like this. $db = new SQLite3('mysqlitedb.db', SQLITE3_OPEN_READONLY); In PDO you would simply open with: $db = new PDO('sqlite:mysqlitedb.db'); My question is however, is there a way to open a database with PDO, in READONLY mode? 回答1: This will become possible with the release of PHP 7.3 (estimated for release in late 2018). Thes syntax is as follows: $db = new PDO('sqlite:mysqlitedb.db', null, null, [PDO::SQLITE_ATTR_OPEN_FLAGS => PDO::SQLITE_OPEN_READONLY]);

PHP, MySQL, PDO - Get result from UPDATE query?

我的梦境 提交于 2020-01-11 08:06:07
问题 I am updating a row in a table, and trying to return the updated row, as per this SO answer. My code is the following: $sql = "SET @update_id := ''; UPDATE testing SET status='1', id=(SELECT @update_id:=id) WHERE status='0' LIMIT 1; SELECT @update_id;"; $db->beginTransaction(); try{ $stmt = $db->prepare($sql); $stmt->execute(); echo count($stmt->fetchAll()); $db->commit(); }catch(Exception $e){ echo $e->getMessage(); exit(); } But I always get the following error SQLSTATE[HY000]: General

PHP PDO and Stored Function

↘锁芯ラ 提交于 2020-01-11 07:59:09
问题 I have just started looking into php pdo and have connected to mysql database and ran a simple SELECT statement. I have a stored function I created before using pdo, do I actually need to use stored functions/procedures while using PDO? BEGIN DECLARE new_username VARCHAR(32); SELECT `username` INTO new_username FROM `users` WHERE `userID` = ID; RETURN COALESCE(new_username, 'Invalid ID'); END Is there any point in using the above function, if I'm doing this using PDO? The function will be

php: loading oracle driver gives error “Unable to load dynamic library - The specified procedure could not be found.”

ぐ巨炮叔叔 提交于 2020-01-11 05:42:05
问题 I seem to have a strange problem with PHP. I've migrated a bunch of software from one server to another. On the server some scripts make a connection to Oracle, so to be able to make a connection the oracle client is installed and a tnsnames file is in place. Connection from sql*plus works. Some of the scripts use PDO, so for that I've enabled extension=php_pdo_oci.dll in php.ini. These scripts work like a charm. Some other scripts use functions like oci_connect, and these need a extension