Starting with PHP & MySQL

Use MySQLi or PDO in current PHP

The original mysql_* extension was removed from PHP and should not be used in new applications. Current PHP applications normally use MySQLi or PDO. Both support prepared statements, transactions and modern MySQL authentication.

<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

$connection = new mysqli('localhost', 'db_user', 'db_password', 'database_name');
$connection->set_charset('utf8mb4');

echo 'MySQL connection is ready.';
?>
Legacy code note: mysql_connect() and mysql_query() are from the removed mysql_* extension. Historical examples can help you understand old applications, but use MySQLi or PDO for new projects.

PHP MySQL database
MySQL is part of famous LAMP ( Linux , Apache, MySQL, PHP) combination and is most popular database in use for the web applications. PHP and MySQL gets integrated easily in any platforms. Scripts developed in PHP using Window platform works perfectly in when used in Linux environment. Many PHP MySQL developers work in Windows platform and run the script in Linux web server.

PHP: It is the script or language

PHP is used to connect to database, display the results or manage the records of the database. PHP is the server side scripting language and to know more about it and how PHP work you can visit our PHP introduction page. To get the full detail of PHP components working in the server you can use PHP command phpinfo.  PHP works equally well with MSSQL, Oracle and other databases available. We will discuss more on MySQL here.
Client connected to web server and Database via Internet

MySQL: It is the database

It stores tables, support queries to manage its data. We keep all our records in MySQL database. MySQL Community Server and its documentation are available from the MySQL downloads page.  MySQL database  is very powerful and can be used in real word demanding projects. Different version of MySQL is available for different platforms.

MySQL once installed runs as a server in the local machine or in any remote server. User can connect to the local or remote MySQL server by using the server name or IP address based on the privilege they have. Once connected users can manipulate the MySQL database by using different PHP commands. You will learn many such PHP commands here.

Installing both depends on the platform you use. In windows system like any other software you can install both. Read the PHP section on how to install

Testing MYSQL drivers and PHP support

In your PHP installation you must have supported DLL or drivers to manage database. Here is the code you can run in your PHP installed directory or on your localhost to find out the supports for MySQL database.

Download the MySQL driver checker ZIP

Download and extract the checker into your local development server, then open mysql.php to check MySQLi and PDO drivers. The separate phpinfo.php diagnostic exposes configuration details: use it only locally and remove it after testing.
<?php
$mysqliEnabled = extension_loaded('mysqli');
$pdoEnabled = class_exists('PDO') && in_array('mysql', PDO::getAvailableDrivers(), true);
echo 'MySQLi: ' . ($mysqliEnabled ? 'available' : 'not available') . '<br>';
echo 'PDO MySQL: ' . ($pdoEnabled ? 'available' : 'not available');
// The removed mysql_* extension is not required for current PHP.
?>

Managing MySQL

After installing MySQL and your localhost, you can manage MySQL database by using PHPmyAdmin . Once our setup is ready we will try to run our SQL from PHP scripts

Executing query in PHP

We will learn how to write a query and using PHP function how to execute the query. This process will ensure that all data connections are established and from PHP script we can manage the data stored in MySQL.

Displaying Records

We will start with the most simple job of displaying records or data from MySQL table by using PHP. There are several ways to do this and as per our requirement we can collect single or multiple records from the tables to display.

Displaying single Records

We can display all details of a record in a page. Displaying a single product or showing profile of a member can be done by using MySQLi or PDO connections.

Displaying records with Edit option

Each record will have one Edit button for the user to update data

Showing more details of record on click ( expand ) by user

We can display minimal information line name or id to the user and on click we can further expand it to display full details of the record.

Paging of records

When we have more records to display we cant show all at same time. We can display fixed numbers or a set of records per page by giving navigation to previous or next set of records. We call it paging.

PDO: Portable Data Object

We can use PDO to connect to various data sources and we will discuss more on MySQL connection here

MYSQLI: MySQL Improved

Manage MySQL database using MySQLi functions

Displaying Images from MySQL Records

Display images with other data from MySQL table by using MySQL or PDO

Delete Images from MySQL Records


Add Images with details to MySQL Records


Displaying records without repetitions of same field data


phpinfo() to get MySQL support


Legacy php mysql connection



Exercise on PHP MySQL


Subscribe to our YouTube Channel here



plus2net.com







abc

16-11-2014

how can i pass textbox value in another php script with using url when click on submit button
Smo

31-05-2015

This is done by using Form. You can read more on this in our HTML form section
prags

27-01-2016

how can i fetch the records from one table of dropdown list and store it in another table using join in php
smo1234

31-01-2016

You can collect records by using select query and then insert the same in another table, you can also directly use insert into command to use data from one table and store in another table.
gin

13-02-2016

When selecting a name from a drop down menu of my DB table, how can I display the remaining names of my same DB table on another drop down menu? For example, if I select John from the drop down menu, his name won't be available on the second drop down menu.
smo1234

13-02-2016

You can remove that name by using WHERE condition in the query your use to populate second list.

SELECT * from Table_list WHERE name !='Name_to_remove'
osei pious

01-05-2017

how many data base do I need to make a complete website?
smo1234

02-05-2017

One database. Need more tables to store different type of records.




PHP video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer