Dreevoo.com | Online Learning and Knowledge Sharing
 
Home | Programs | Programming languages | PHP & MySQL | Use PHP to connect to MySql database
Guest
Click to view your profile
Topics
Programs
Languages
Recipes
Home
Shortcuts
 
 

Use PHP to connect to MySql database

In this lesson it will be shown how to use PHP to connect, run SQL query to retrieve data and insert a new record into MySql database.

 
  Author: podtalje | Version: PHP | 21st May 2013 |  
 
 
1.
 

If you are starting with PHP and MySql development I would recommend that you install WAMP server which includes complete environment for developing PHP applications and also has phpMyAdmin and MySql database included.

Installation instructions for WAMP server can be found on:
Setting up PHP and MySql testing server with WAMP

If you want to learn how to create a database, please take a look at lesson:
Create MySql database with phpMyAdmin

 
 
2.
 

For connecting to MySQL database we will use extension mysqli.

The syntax for creating a new object is simple:
$mysqli = new mysqli("host", "username", "password", "database");

The following example will connect to the my_db database on local computer with root username and empty password.
$mysqli = new mysqli("localhost", "root", "", "my_db");

After creating connection it is also recommended to check if the connection was successful:
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}



Besides mysqli there also exists extenstion called mysql which can also be used for connecting to MySql database. Because mysql extension is deprecated as of PHP 5.5, you should always use mysqli.

 
 
3.
 

Now we will prepare a new SQL query which will return all the data from table my_table.

$query = "SELECT * FROM my_table";
$result = $mysqli->query($query);

 
 
4.
 

The result of the query is now stored in the $result variable.

We will now use while loop to go through all the records which were returned from the database.

Each single record is stored in the variable $row and we can print out specific filed by also specifying the field name.

while ($row = $result->fetch_assoc()) {
  echo $row['number'].'<br />';
  echo $row['text'].'<br />';
  echo $row['date'].'<br />';
  echo '------------------------ <br />';
}

<br /> tag is added just for better formatting of output.

 
 
5.
 

Inserting data back to the database can also be done very easy.

First we need to prepare a proper SQL command for inserting data.

After that we just run query and a new record will be inserted.

This is shown in the example below:
$query="INSERT INTO my_table(id, number, text, date) VALUES(0,987,'hello','2013-05-21')";
$mysqli->query($query);

 
 
6.
 

Below you can find all the code from this lessons together, so it will be easier to copy it to your php program.

$mysqli = new mysqli("localhost", "root", "", "my_db");

if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

$query = "SELECT * FROM my_table";
$result = $mysqli->query($query);

while($row = $result->fetch_assoc()) {
  echo $row['number'].'<br />';
  echo $row['text'].'<br />';
  echo $row['date'].'<br />';
  echo '------------------------ <br />';
}

//insert new record
$query="INSERT INTO my_table(id, number, text, date) VALUES(0,987,'hello','2013-05-21')";
$mysqli->query($query);


And of course if you have any additional questions just ask on forum.

 
 
 
   
  Please login to post a comment
  Click to open user profile  
linnchinnn, 21st Dec 2025, 1:47 AM
Στην Ελλάδα, η σύνδεση της PHP με τη MySQL είναι το θεμέλιο για κάθε δυναμική ιστοσελίδα, επιτρέποντας την άμεση αλληλεπίδραση με τα δεδομένα. Το https://highflybet.com.gr χρησιμοποιεί παρόμοιες προηγμένες τεχνολογίες για να εξασφαλίσει ότι κάθε συναλλαγή και κάθε παιχνίδι εκτελείται με απόλυτη ακρίβεια και ταχύτητα. Είναι ο ιδανικός προορισμός για όσους αναζητούν μια πλατφόρμα που λειτουργεί άψογα, προσφέροντας την αξιοπιστία που χρειάζεται κάθε παίκτης για να απογειώσει τη διασκέδασή του.
 
 
  Click to open user profile  
abbaskhan8008, 22nd Dec 2025, 10:08 PM
They are welcomed with a powerful mix of entertainment categories that include movies from different countries, TV series in multiple languages, animated content, anime collections, and sometimes even live streaming features, all arranged in a way that feels natural and easy to scroll, because the homepage is not cluttered with useless ads or confusing buttons, instead it focuses on showing what matters most.
 
 
  Click to open user profile  
alexseen, 5th Jan 2026, 5:07 PM
Dlouho jsem měl pocit, že většina online casin není stavěná pro české uživatele. Zkoušel jsem dál, až jsem našel https://spin-mama.cz . Web působí jednoduše a přirozeně, bez zbytečného tlaku. Nabídky jsou přizpůsobené pro Česko a člověk ví, na čem je. To mi dalo větší klid při používání.
 
 
  Click to open user profile  
danieljack, 13th Jan 2026, 12:10 PM
Connecting PHP to a MySQL database is essential for any web developer. I'm excited to see the step-by-step guide on retrieving data and inserting new records. This knowledge is fundamental for building dynamic web applications.
 
 
  Click to open user profile  
harrydaily, 26th Jan 2026, 9:50 AM
In this lesson, you will learn how to use PHP to connect to a MySQL database, run a SQL SELECT query to retrieve data, and insert a new record into the database using an INSERT query. https://smashkarts76.com/
 
 
  Click to open user profile  
abbaskhan8008, 27th Jan 2026, 11:32 AM
As if someone truly understands what the reader is going through, whether it is fear, sadness, hope, dreams, mistakes, or gratitude, and these prayers often speak about protection, guidance, forgiveness, healing, success, family, peace, and strength, covering the most common feelings people experience every day, and by reading and sharing these blessings.
 
 
  Click to open user profile  
1q2w1q2w, 29th Jan 2026, 9:14 AM
 
 
  Click to open user profile  
john321rose, 29th Jan 2026, 9:20 AM
With EVERFI Login, you’re unlocking a world of digital learning that’s transformed my teaching. I’ve walked you through quick EVERFI Login steps, signing up, recovering credentials, and securing your account https://www.everfi.com.co
 
   
 
 
online learning made for people
Dreevoo.com | CONTRIBUTE | FORUM | INFO