How to create a database and access specific data from it?

王林
Release: 2024-07-16 16:55:00
Original
922 people have browsed it

How to create a database and access specific data from it?

As I learned last time, how to create database using query in terminal.

mysql -uroot
/*Press enter key and then give input to terminal to create database.*/
create database myapp;
Copy after login

Now, I need to connect to the database so I can access the table and fetch the specific name.

To connect to the database, you'll need to use PHP code to establish a connection using PDO (PHP Data Objects) or mysqli (MySQL Improved Extension). Here's an example using PDO, firstly we have to know about PDO

what are PDO's in PHP?

PDO (PHP Data Objects) is a PHP extension that provides a consistent interface for accessing and manipulating databases.
Here are some basic steps to get database accessible and to retrieve data

Create a new PDO object:

$pdo = new PDO('dsn', 'username', 'password');
Copy after login

Here:

  • 'dsn' (Data Source Name) specifies the database connection details (e.g. database type, host, database name)
  • 'username' and 'password' are the credentials to authenticate by the database

Example:

$pdo=newPDO('mysql:host=localhost;dbname=myapp', 'root', 'password');
Copy after login

This creates a connection to a MySQL database named "myapp" on the local host with the username "root" and password "password".

Prepare a statement to retrieve the name

Here is the statement for retrieving the specific name of an applicant through database.

$stmt = $pdo->prepare('SELECT name FROM applicants WHERE name = :name');
Copy after login

Bind the parameter:

The specific name you're looking for

$stmt->bindParam(':name', $name);

Copy after login

Set the value of $name

Input the name that you want to search in database

$name = 'Ali Hasan';
Copy after login

Execute query to fetch result

After inputing the name in $name variable then next step is execution of statement. As the execution is completed then fetch the result

$stmt->execute();
$result = $stmt->fetch();

Copy after login

Display the result

Now you have to write the statement to display the result that you have fetched through database

echo $result['name']; 
Copy after login

Close the database connection

At the end of search you have to close the connection through database using PDO variable with null value.

$pdo = null;

Copy after login

I hope that you have understand it completely.

The above is the detailed content of How to create a database and access specific data from it?. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template