pdo

pdo variable is undefined in mysql function

大憨熊 提交于 2020-02-26 04:23:50
问题 I have an index.php which is like: require_once("../resources/config.php"); require_once(LIBRARY_PATH . "/mysql.php"); ... if(checkIfMailExists($email)) { $error = true; $errormsg = "This e-mail is already in use!"; } mysql.php is: try { $pdo = new PDO('mysql:host=' . $config['db']['db1']['host'] . ';dbname=' . $config['db']['db1']['dbname'], $config['db']['db1']['username'], $config['db']['db1']['password']); }catch(PDOException $e){ echo "Cant connect to mysql DB!"; } function

PHP / PDO - Use PDO statement multiple times when using multiple foreach loops

感情迁移 提交于 2020-02-25 13:51:15
问题 I have the following code: if(!empty($postCountryAdd)) { $sqlQueryLocalityAdd = $dbh->prepare("SELECT DISTINCT locality_add FROM table WHERE country_add = :country_add ORDER BY locality_add ASC"); $sqlQueryLocalityAdd->execute(array(':country_add' => $postCountryAdd)); echo '<option value="">Select locality</option>'; foreach($sqlQueryLocalityAdd as $localityAddRow) { //while ($localityAddRow = $sqlQueryLocalityAdd->fetch()){ echo '<option value="'; echo $localityAddRow["locality_add"]; echo

PHP / PDO - Use PDO statement multiple times when using multiple foreach loops

我们两清 提交于 2020-02-25 13:50:22
问题 I have the following code: if(!empty($postCountryAdd)) { $sqlQueryLocalityAdd = $dbh->prepare("SELECT DISTINCT locality_add FROM table WHERE country_add = :country_add ORDER BY locality_add ASC"); $sqlQueryLocalityAdd->execute(array(':country_add' => $postCountryAdd)); echo '<option value="">Select locality</option>'; foreach($sqlQueryLocalityAdd as $localityAddRow) { //while ($localityAddRow = $sqlQueryLocalityAdd->fetch()){ echo '<option value="'; echo $localityAddRow["locality_add"]; echo

php -- PDO

心已入冬 提交于 2020-02-25 12:38:05
PDO:php data object,php数据对象 1、new PDO    PDO::__construct ( string $dsn [, string $username [, string $password [, array $driver_options ]]] )   $dsn:服务器相关信息,包括     1)选取哪一种数据库     2)服务器域名     3)连接到数据库的端口号     4)连接的数据库   $username:登陆的用户名   $password:登陆密码   例如:     $pdo = new PDO('mysql:host=localhost;port=3306;dbname=project','root','root'); 2、exec(),执行没有 结果集 返回的语句,返回受影响的行数   如:增、删、改、设置字符集 等等...   例如:     2.1)设置字符集       $sql = "set names utf8";       $pdo->exec($sql);     2.2)增       $sql = "insert into pro_student values(null,'陶丹凤','itcast0013',1,19,2)";       $pdo->exec($sql);     2.3)删     

防止sql注入的方式

谁都会走 提交于 2020-02-25 03:48:01
如 果用户输入的是直接插入到一个SQL语句中的查询,应用程序会很容易受到SQL注入,例如下面的例子: $unsafe_variable = $_POST['user_input']; mysql_query("INSERT INTO table (column) VALUES ('" . $unsafe_variable . "')"); 这是因为用户可以输入类似VALUE"); DROP TABLE表; - ,使查询变成: INSERT INTO table (column) VALUES('VALUE'); DROP TABLE table;' 1.使用PDO(PHP Data Objects ) $stmt = $pdo->prepare('SELECT * FROM employees WHERE name = :name'); $stmt->execute(array(':name' => $name)); foreach ($stmt as $row) { // do something with $row } 2.使用mysqli 复制代码 代码如下: $stmt = $dbConnection->prepare('SELECT * FROM employees WHERE name = ?'); $stmt->bind_param('s', $name);

How to prevent showing PDO error?

北战南征 提交于 2020-02-24 10:33:50
问题 When there is an error from PDO execute(), for some reason it outputting the error? How do I prevent that.... I would like to store an error into $data['error'] if (!$query->execute()) { $data['success'] = 'false'; echo json_encode($data); return; } From Console Log: <br /> <b>Warning</b>: PDOStatement::execute() [<a href='pdostatement.execute'>pdostatement.execute</a>]: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in <b>C:\wamp\www\site

PDO exec() query() prepare() PDOException 事务处理

给你一囗甜甜゛ 提交于 2020-02-23 21:36:21
来源: http://hi.baidu.com/gaolamp/item/4bbfe657ba16f7c79e266763 基于驱动的。 php_pdo.dll php_pdo_mysql.dll php.ini 打开扩展。 三个类: PDO类 PDOsatement PDOexcaption 常量 PDO __construct(); 一,创建PDO对象 dsn(data source name)数据源名 $pdo=new PDO(); $pdo=new PDO("url=pash","",""); php.ini 配置pdo.dsn.mysqlpdo=mysql:host= ;dbname= try{ $pdo=new PDO("mysql:host= ;dbname= ","",""); }catch(PDOException $w){ echo "数据库连接成功:".$w->getMessage(); exit; } ---------------------- <?php try{ //调优参数 $driver_opts=array(PDO::ATTR_AUTOCOMMIT=>0, PDO::ATTR_PERSISTENT=>true); $pdo=new PDO("mysql:host=localhost;dbname=xsphpdb", "root", "123456

PHP PDO fetch all tables

一曲冷凌霜 提交于 2020-02-23 09:07:26
问题 So I know with a standard mysql call we can do mysql_list_tables , however is there an equivalent while using PDO? If so, does this return an array? Thanks! 回答1: Execute the query with PDO::query() : SHOW TABLES; If you fetch an associative array, the name of the column will be: Tables_in_databasename Note: this will list both tables and views. If you must get only tables , use this instead: SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_TYPE='BASE TABLE' AND TABLE_SCHEMA=

PHP PDO fetch all tables

a 夏天 提交于 2020-02-23 09:06:10
问题 So I know with a standard mysql call we can do mysql_list_tables , however is there an equivalent while using PDO? If so, does this return an array? Thanks! 回答1: Execute the query with PDO::query() : SHOW TABLES; If you fetch an associative array, the name of the column will be: Tables_in_databasename Note: this will list both tables and views. If you must get only tables , use this instead: SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_TYPE='BASE TABLE' AND TABLE_SCHEMA=

PDO中的bindParam和bindValue有什么区别?

北城以北 提交于 2020-02-21 19:36:59
bindParam $sex = 'male' ; $s = $dbh - > prepare ( 'SELECT name FROM students WHERE sex = :sex' ) ; $s - > bindParam ( ':sex' , $sex ) ; // use bindParam to bind the variable $sex = 'female' ; $s - > execute ( ) ; // executed with WHERE sex = 'female' bindValue $sex = 'male' ; $s = $dbh - > prepare ( 'SELECT name FROM students WHERE sex = :sex' ) ; $s - > bindParam ( ':sex' , $sex ) ; // use bindParam to bind the variable $sex = 'female' ; $s - > execute ( ) ; // executed with WHERE sex = 'female' PDOStatement::bindValue — 把一个值绑定到一个参数 绑定一个值到用作预处理的 SQL 语句中的对应命名占位符或问号占位符。 PDOStatement::bindParam