pdo

PHP pdo instance as private static property

夙愿已清 提交于 2019-12-29 07:14:31
问题 I'm attempting to design my website based on OOP, but I'm having trouble with how to design the database connection. Currently, I'm creating a private static PDO object in an abstract class, Connector. Obviously, anything that needs to interact with the database will extend this class. I've been flipping back and forth on how to make sure that there is only ever one connection, or PDO object, in a script because some pages will need more than one class that extends Connector. Many people seem

LOAD DATA LOCAL INFILE does not work from php 5.5 using PDO

ぐ巨炮叔叔 提交于 2019-12-29 06:54:31
问题 I've recently moved web servers, and the new web server has a different version of PHP. New Server: PHP 5.5.3-1ubuntu2 (cli) Old Server: PHP 5.3.10 (cli) On the database server, my.cnf is set to allow local-infile. I can use load data local infile from: - HeidiSQL - The command line client running on the new web server using --local-infile=1 - PHP, using PDO, from the old web server However, when I try to connect from the new web server, using PHP with PDO, I get: A database problem has

extending PDO class

落花浮王杯 提交于 2019-12-29 06:44:09
问题 Below is the db connection class I came out with so far, but I am going to improve it by extending the PDO class itself, <?php class database { protected $connection = null; #make a connection public function __construct($hostname,$dbname,$username,$password) { try { # MySQL with PDO_MYSQL $this->connection = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password); $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (PDOException $e) { $this-

Why can't I use LOAD DATA LOCAL with PDO even though I can from cli client?

拈花ヽ惹草 提交于 2019-12-29 06:30:12
问题 I recently upgraded my Ubuntu 10.04 dev server to 14.04. It is actually a fresh install. PHP version was 5.4.15, and is now PHP 5.5.9. MySQL went from 5.1.67 to 5.5.37. I am trying to LOAD LOCAL DATA INFILE on a new server. It works running the command using the local mysql cli client. It does not work when executing using PDO: PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1148 The used command is not allowed with this

Auto connecting to PDO only if needed

依然范特西╮ 提交于 2019-12-29 06:29:29
问题 I have a section of code that depending on the URL requested, will include one of fourteen other files. Some of these fourteen files require a connection to one of three different databases, and additional files can be added at anytime. I don't want to open PDO connections by default to all three database as its a waste of resources and will slow the execution time down. So my thought is to wrap all SQL queries within a function. The first time that a query is executed on a non-open PDO

Dynamic queries with PHP PDO

浪子不回头ぞ 提交于 2019-12-29 06:29:07
问题 I'm trying to figure out how to convert my history script from mysql_query() to PDO. I have a form with 4 input fields which you can randomly select. Which means there can be 0, 1, 2, 3, 4 fields selected depending on which info you're trying to get. I've tried to query db like this: $q = $db->prepare('SELECT date, name, action FROM history WHERE name = :name AND action = :action'); $q->bindParam(':name', $Name, PDO::PARAM_STR, 20); $q->bindParam(':action', $Action, $PDO::PARAM_STR, 20); $q-

get number of rows with pdo

荒凉一梦 提交于 2019-12-29 06:15:49
问题 I have a simple pdo prepared query: $result = $db->prepare("select id, course from coursescompleted where person=:p"); $result ->bindParam(':p', $q, PDO::PARAM_INT); $result->execute(); $rows = $result->fetch(PDO::FETCH_NUM); echo $rows[0]; the echo seems to be returning the ID value of the record, not the number of records returned by the query? any idea or explanation for this? 回答1: PDO::FETCH_NUM: returns an array indexed by column number as returned in your result set, starting at column

get number of rows with pdo

眉间皱痕 提交于 2019-12-29 06:15:41
问题 I have a simple pdo prepared query: $result = $db->prepare("select id, course from coursescompleted where person=:p"); $result ->bindParam(':p', $q, PDO::PARAM_INT); $result->execute(); $rows = $result->fetch(PDO::FETCH_NUM); echo $rows[0]; the echo seems to be returning the ID value of the record, not the number of records returned by the query? any idea or explanation for this? 回答1: PDO::FETCH_NUM: returns an array indexed by column number as returned in your result set, starting at column

PDO Connection and abstract class

三世轮回 提交于 2019-12-29 05:35:11
问题 I am using abstract class with PDO. I want to know if it is necessary to null the $conn variable every time, or if it does so itself when the scripts ends? Can you also tell me, with this type of structure, what would be the best way to nullify $conn ? abstract class DB_Connection { protected static $tbl_admin = "prof_admin"; //protected static $tbl_admin = "prof_admin"; protected static function obj_db() { $servername = "localhost"; $username = "root"; $password = ""; $dbname = "salmanshahid

pdo lastInsertId returns zero(0)

Deadly 提交于 2019-12-29 01:46:14
问题 All queries execute successfully, when I check table in MySQL row inserted successfully without any error, but lastInsertId() returns 0 . why? My code: // queries executes successfully, but lastInsetId() returns 0 // the menus table has `id` column with primary auto_increment index // why lastInsertId return 0 and doesn't return actual id? $insertMenuQuery = " SELECT @rght:=`rght`+2,@lft:=`rght`+1 FROM `menus` ORDER BY `rght` DESC limit 1; INSERT INTO `menus`(`parent_id`, `title`, `options`,