search
HomeBackend DevelopmentPHP ProblemHow to query MySQL database using PHP?

How to query MySQL database using PHP?

Jun 09, 2020 am 11:41 AM
mysqlphp

How to query MySQL database using PHP?

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!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Safe Exam Browser

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.