How to query MySQL database using PHP?
First instantiate PDO in PHP code. The instantiation parameters need to be passed in DNS, data user name and database password;
$dbDatas = parse_ini_file(DB_FILE); $dbOptions = [ \PDO::ATTR_DEFAULT_FECTH_MODE => \PDO::FETCH_OBJ, \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION ]; $dsn = sprintf( 'mysql:dbname=%s;host=%s', $dbDatas['dbname'], $dbDatas['host'] ); $this->cn = new \PDO($dsn, $dbDatas['user'], $dbDatas['password'], $dbOptions); $this->cn->exec('SET CHARACTER SET UTF8');
Then write the database query statement template;
$sql = "SELECT * FROM table_name";
Then use the PDO object to execute the statement;
$stmt = $pdo->prepare($sql) ; $stmt->execute();
Finally get the result set.
$row = $stmt->fetchAll();
Recommended tutorial: "PHP Tutorial"
The above is the detailed content of How to query MySQL database using PHP?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version
SublimeText3 Linux latest version

Zend Studio 13.0.1
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
