简体   繁体   English

如何使用PDO连接到MySQL

[英]How to connect to mysql using pdo

I am working on my php as I want to connect to the mysql database using PDO. 我正在使用我的PHP,因为我想使用PDO连接到mysql数据库。 I have stored the username, password and database in the config file, but I have got a problem with connecting to the mysql database because I keep getting an error. 我已经将用户名,密码和数据库存储在配置文件中,但是由于不断出现错误,我在连接mysql数据库时遇到了问题。

When I try this: 当我尝试这个:

<?php

//Connect to the database
include('config.php');

$smtps = $link->query('SELECT * FROM sent');
$smtps->execute();
$db = $smtps->get_result();

print($db);
?>

I am getting an error: 我收到一个错误:

Fatal error: Uncaught Error: Call to undefined method PDOStatement::get_result() in /home/username/public_html/test_pdo.php:17 Stack trace: #0 {main} thrown in /home/username/public_html/test_pdo.php on line 17 致命错误:未捕获错误:调用/home/username/public_html/test_pdo.php:17中未定义的方法PDOStatement :: get_result():堆栈跟踪:#0 {main}抛出于/home/username/public_html/test_pdo.php中第17行

Here is the line 17: 这是第17行:

$db = $smtps->get_result();

Here is the config: 这是配置:

<?php
/* Database credentials. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
define('DB_HOST', 'localhost');
define('DB_USER', 'username');
define('DB_PASS', 'password');
define('DB_NAME', 'dbtablename');
//$errflag = false;
$link = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME.'', DB_USER, DB_PASS);

?>

Can you please show me an example how do you connect to mysql database using PDO when you stored the username, password and database name in config.php? 您能给我一个例子,当您在config.php中存储用户名,密码和数据库名称时,如何使用PDO连接到mysql数据库吗?

Thank you. 谢谢。

This is happening because get_result() is not a PDO method. 发生这种情况是因为get_result()不是PDO方法。

In this situation you should just use fetch() ( link ) if you just want the first result or fetchAll() ( link ) if you want an array of the results 在这种情况下,如果只需要第一个结果,则应仅使用fetch()link );如果要结果的数组,则应使用fetchAll()link

Try this: 尝试这个:

$smtps = $link->query('SELECT * FROM sent');
$result = $smtps->fetchAll();

print($result);

You only need to use the excute() when using parameters in your select: 在选择中使用参数时,只需要使用excute()即可:

SELECT * FROM sent where id = ?

would be 将会

$smtps = $link->prepare('SELECT * FROM sent where id = ?');
$smtps->execute([$id]);
$result = $smtps->fetch();

print($result);

If u use query method u can without execute method get result as as below cod 如果您使用查询方法,则无需执行方法就可以得到如下cod的结果

include('config.php');

$result = $link->query('SELECT * FROM sent')->fetchAll(PDO::FETCH_ASSOC);


print_r($result);

get_results is not a method of the PDO class. get_results不是PDO类的方法。 If you want to fetch data from the database in this case, use fetchAll() method of the query object. 如果要在这种情况下从数据库中获取数据,请使用查询对象的fetchAll()方法。


<?php

//Connect to the database
include('config.php');

$smtps = $link->query('SELECT * FROM sent');
$smtps->execute();
$db = $smtps->fetchAll();

print($db);
?>

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM