pdo

PDO FETCH_CLASS and Namespacing issue

匆匆过客 提交于 2020-01-30 05:12:34
问题 I am trying to use PDO::FETCH_CLASS on an object. I am using namespacing and just entering: $result = $query->fetchAll(\PDO::FETCH_CLASS, 'Product'); or $result = $query->fetchAll(\PDO::FETCH_CLASS, '\Product'); results in PHP looking for Product.php in the root of the app. I can successfully instantiate a new Product though using: $product = new Product(); So I know my name spacing is working. Is this not possible? Or do I need to instantiate a Product first then populate it after from the

Equivalent of mysql_list_tables in PHP PDO?

你。 提交于 2020-01-30 05:10:15
问题 When using PHP PDO to access the database, is there a way to list all the tables in a database? Something like mysql_list_tables() is whats needed. 回答1: What about using a SQL query that does something like this : show tables Or, if needed, specifying the database : show tables from crawler And, if you only want to get some tables : show tables from crawler like 'site%' Actually, even if the mysql_list_tables() function exists, its manual page states : This function is deprecated. It is

PDO: Database connection on remote website

元气小坏坏 提交于 2020-01-30 03:31:22
问题 This is an example of the database connection I'm using with my PDO queries: $dsn = "mysql:host=localhost;dbname=some_db;charset=utf8"; $opt = array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC ); $pdo = new PDO($dsn,'Username','Password', $opt); How would I modify it if my database is located on www.mysite.com and I want to access the database from a different website? 回答1: You need to open up port 3306 to accept connections. $dsn = "mysql

PHP PDO only last value of array gets inserted using bindValue & bindParam

走远了吗. 提交于 2020-01-30 02:56:16
问题 When below code is executed, only the last value of the array charlie gets inserted in the table. $this->array = $array; //Array ( [0] => alpha [1] => bravo [2] => charlie ) $query = "INSERT INTO test SET Name = :Name"; $sql = $this->conn->prepare($query); foreach($this->array as $k => &$v) { $sql->bindValue(":Name" , $v , PDO::PARAM_STR); } $sql->execute(); Im getting the same result using bindParam as well. Can someone help me point out what I'm missing. I'm totally baffled. 回答1: This is

0719PHP基础:PDO

不打扰是莪最后的温柔 提交于 2020-01-28 04:30:01
PDO是一个“数据库访问抽象层”,作用是统一各种数据库的访问接口,与mysql和mysqli的函数库相比,PDO让跨数据库的使用更具有亲和力 PDO操作mysql================================================================ 链接数据库:$pdo = new PDO('mysql:host=localhost;dbname=ceshi', $user, $pass) 设置字符集:$pdo->exec("set names utf8") 写sql语句,并执行,mysql服务器返回结果集(预处理对象):   $sql = "select * from student"   $ycl = $pdo->query($sql) 预处理对象调用数据(fetchAll(查询格式)查所有数据):$attr = $ycl->fetchAll(PDO::FETCH_ASSOC) PDO类常用方法================================================================ exec():用来执行增删改和设置的语句(例如设置字符集) query():用来执行查询语句,并返回与处理结果集(再调用预处理类的方法进行下面的操作) lastInsertId():最后一次插入的id

PDO增删改查

馋奶兔 提交于 2020-01-26 01:45:27
//首先连接数据库$dbms='mysql'; //数据库类型$host='localhost'; //数据库主机名$dbName='user'; //使用的数据库$user='root'; //数据库连接用户名$pass=''; //对应的密码$dsn="$dbms:host=$host;dbname=$dbName";$dbh = new PDO($dsn, $user, $pass); //初始化一个PDO对象//增删改查都可以使用预处理方式//增$stmt = $pdo->prepare("INSERT `user` SET `name`= :name , `password`= md5(:password)");$stmt -> execute(['name'=>'张三','password'=>'123123']);echo '成功添加了:'.$stmt->rowCount().'条数据'; //删$stmt = $pdo->prepare('DELETE FROM `user` WHERE `id` = :id');if ($stmt->execute(['id'=>4])){ echo '成功删除:'.$stmt->rowCount().'条数据';}else{ exit($stmt->errorInfo());}$stmt = null;$pdo = null;/

Copying data from two different database server using PDO php

雨燕双飞 提交于 2020-01-25 07:25:12
问题 I'm trying to make a script that will copy data in tables from one database server to another database server. This is what I've tried using PDO. So here is how I get both connections using PDO (sample.php): <?php class DBOne { static $db ; private $dbh ; private function PDO_DBConnect(){ $db_type = 'mysql'; $db_name = 'database1'; $user = 'guest' ; $password = 'guest' ; $host = 'server1' ; try { $dsn = "$db_type:host=$host;dbname=$db_name"; $this->dbh = new PDO ( $dsn, $user, $password);

Whats the proper way to use password_verify with PDO?

无人久伴 提交于 2020-01-24 20:12:56
问题 I can't seem to get password_verify to work w/in my php PDO code. My pass field is stored as varchar(255). I've been reading similar questions, but from what I can tell I have it set up right. I must still be missing something. My registration page is as follows.. $user = $_POST['username'] $pass = $_POST['pass']; $passH = password_hash($pass, PASSWORD_DEFAULT); $query = $con->prepare("INSERT INTO emps (user, pass) VALUES (?, ?)"); $query->execute([$user, $passH]); An encrypted password is

Something wrong with Singleton class, no idea what

江枫思渺然 提交于 2020-01-24 17:04:07
问题 I have never done any Singleton class before and now I figured that for this DB connection it will be a good idea to make one, but I have no clue why it is not working. I really would appreciate if someone would help me out with this one since I want to learn how OOP works... Anyway, I fixed it with just updating my PHP to latest version, now $DBH = new static(); works fine, thanks people. I tried to use $DBH = new static(); isntead of $DBH = new self(); but then I have this error: Parse

How to set up and inject multiple PDO database connections in slim 4?

我与影子孤独终老i 提交于 2020-01-24 09:39:24
问题 I could make an instance of PDO and inject it successfully. I defined the PDO::class directly and injected it in the constructor with __construct(PDO $pdo) . I would need something like PDO1::class and PDO2::class to inject it like follows: __construct(PDO1 $pdo1, PDO2 $pdo2) but that obviously doesn't work. There is only one PDO class and what I need to do is 2 instances of it with different database credentials. What is the best way to do it? I set up one definition of a database via PDO