pdo

Where to put database connection settings?

二次信任 提交于 2020-02-01 00:39:11
问题 Where do you put the connection settings for a database connection (things like host, dbname, user, password)? Is it placed in the database class or file, or outside in a config file, or somewhere else? 回答1: Ideally, you should place it in a config file, which can be something as simple as a PHP array. For example: db_config.php $db_config = array( 'host' => 'localhost', 'user' => 'username', 'password' => 'qwerty' ); You should then place this file outside your document root for maximum

PHP PDO Fetch row and print values

独自空忆成欢 提交于 2020-01-30 13:15:16
问题 I couldn't understand the exact working of the fetch/fetchall in PDO.can i get a simple example for PDO fetch for fetching each row and displays its values iteratively? 回答1: There are a lot of examples around.. You can do a simple fetch using a while loop too <?php //your PDO connection goes here..... $stmt = $db->query('SELECT username FROM yourtable'); while($row = $stmt->fetch(PDO::FETCH_ASSOC)) { echo $row['username']; } Read the PHP Manual on PDOStatement::fetch By using a fetchAll , you

PHP PDO Fetch row and print values

梦想与她 提交于 2020-01-30 13:14:27
问题 I couldn't understand the exact working of the fetch/fetchall in PDO.can i get a simple example for PDO fetch for fetching each row and displays its values iteratively? 回答1: There are a lot of examples around.. You can do a simple fetch using a while loop too <?php //your PDO connection goes here..... $stmt = $db->query('SELECT username FROM yourtable'); while($row = $stmt->fetch(PDO::FETCH_ASSOC)) { echo $row['username']; } Read the PHP Manual on PDOStatement::fetch By using a fetchAll , you

Conditional query with PDO prepare and bind statement

☆樱花仙子☆ 提交于 2020-01-30 12:19:06
问题 I am converting all my queries from mysql to PDO, and in this process I found a conditional query like a follows if (isset($parameters['searchTerm'])) { $where =" And title LIKE '%{$parameters['searchTerm'] }%'"; } $sql = "Select * from table data Where tableId = 5 {$where} "; and when I am trying to convert this query in PDO the expected syntax is as follows if (isset($parameters['searchTerm'])) { $where =" And title LIKE :searchTerm"; } $sql = $dbh->prepare("Select * from table data Where

Select case insensitive using mysql, php and pdo

陌路散爱 提交于 2020-01-30 11:40:06
问题 I'm trying to select some data from a mysql table but I cannot get the Where comparison to be case insensitive, I tried using LOWER: $wildcard = $_GET['q']; $query = "SELECT id, name, departamento FROM gestionDoc_cargos WHERE (LOWER(name) LIKE '%' LOWER(:wildcard) '%' OR LOWER(departamento) LIKE '%' LOWER(:wildcard) '%')"; try{ $result = DB::getInstance()->prepare($query); $result->bindParam(':wildcard', $wildcard, PDO::PARAM_STR); $result->execute(); $result = $result->fetchAll(PDO::FETCH

Call to a member function fetchAll() on boolean

旧时模样 提交于 2020-01-30 11:25:30
问题 I have the following problem. This error persisting in accompanying me Fatal error: Uncaught Error: Call to a member function fetchAll() on boolean in C:\xampp\htdocs\certificado\functions.php:49 Stack trace: #0 C:\xampp\htdocs\certificado\index.php(11): get_info_from_email('amanda_pandoka@...') #1 {main} thrown in C:\xampp\htdocs\certificado\functions.php on line 49 In the code below I can not understand the error. Can someone help me? function connect() { $socket = new PDO('mysql:host=' . @

Working with MS SQL SERVER on PHP5.6

大兔子大兔子 提交于 2020-01-30 10:36:28
问题 So I have a trouble here. Tried use mssql_connect() but interpeter says that function is undefinied. PHP.net advices this, but it suppports only older versions of PHP. Is it possible to connect to MS SQL Server database from PHP5.6? What extensions should I add, what functions can I use? Configuration details: localhost, Apache 2.4, PHP 5.6, OS Windows 8. 回答1: You need to enable the MSSQL extension in the PHP.ini file. If this extension is not installed, you need to install it in PHP. Please

Working with MS SQL SERVER on PHP5.6

跟風遠走 提交于 2020-01-30 10:34:08
问题 So I have a trouble here. Tried use mssql_connect() but interpeter says that function is undefinied. PHP.net advices this, but it suppports only older versions of PHP. Is it possible to connect to MS SQL Server database from PHP5.6? What extensions should I add, what functions can I use? Configuration details: localhost, Apache 2.4, PHP 5.6, OS Windows 8. 回答1: You need to enable the MSSQL extension in the PHP.ini file. If this extension is not installed, you need to install it in PHP. Please

Reliable or not PDO lastInsertId() when using transactions

核能气质少年 提交于 2020-01-30 07:00:07
问题 I use PDO transaction try { DB::$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); DB::$db->beginTransaction(); $db->prepare( insert query ); $db->execute(); $last_insert_id = $db->lastInsertId(); ... ... Multiple concurrent requests are expected on this script. Question: is it possible that lastInsertId() return incorrect value for the user, who actually inserted the row? (by "incorrect value" i mean: id that is inserted by some other user). 回答1: You're safe. The ID you get will

Reliable or not PDO lastInsertId() when using transactions

狂风中的少年 提交于 2020-01-30 07:00:05
问题 I use PDO transaction try { DB::$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); DB::$db->beginTransaction(); $db->prepare( insert query ); $db->execute(); $last_insert_id = $db->lastInsertId(); ... ... Multiple concurrent requests are expected on this script. Question: is it possible that lastInsertId() return incorrect value for the user, who actually inserted the row? (by "incorrect value" i mean: id that is inserted by some other user). 回答1: You're safe. The ID you get will