• Home
  • Technology
    • Ebullientech
    • Entrepreneurship
    • Video Technology
    • Cloud Computing
    • Business Analysis
  • Healthcare
    • Pharmacology
  • Thoughts
    • Food
    • General
    • Stories
    • Places
    • Workplace
    • Social Issues
    • Reviews
  • Quotes
  • Ebullientech News and Events
  • About

Kamalika's Notebook

thinking nothing...............!!

You are here: Home / Kamalika / Technology / MySQL / How to connect to MySQL database using PHP

How to connect to MySQL database using PHP

August 21, 2014 By Kamalika Leave a Comment

Connect to MySQL using PHP and fetch / retrieve data from the database

We have reached the final installment of the article where we are going to fetch / retrieve data from the MySQL database using PHP code.

Here is the code:

[php]

<?php
$db_username = “root”;
$db_password = “”;
$db_hostname = “localhost”;

//connection to the database
$db_resource = mysql_connect($db_hostname, $db_username, $db_password)
or die(“Unable to connect to MySQL”);
echo “Connected to MySQL<br>”;

//select a database
$db_select = mysql_select_db(“bittu”,$db_resource)
or die(“Could not select database”);

//execute the SQL query and return records
$result = mysql_query(“SELECT uid, firstname,lastname,email, created_time,active FROM users”);

//fetch data from the database
while ($row = mysql_fetch_array($result)) {
echo “UID:”.$row{‘uid’}.” First Name:”.$row{‘firstname’}.” Last Name: “. $row{‘lastname’}.” Email: “.$row{’email’}.” Created Time:”.$row{‘created_time’}.” Active: “.$row{‘active’}. “<br>”;
}
//close the connection
mysql_close($db_resource);
?>

[/php]

Running this script on browser will output the following:

Connected to MySQL
UID:1 First Name:Bittu Last Name: Sharma Email: bsharma@xyz.com Created Time:2014-08-21 18:22:51 Active: 1
UID:2 First Name:Kamalika Last Name: Roy Email: kroy@abcd.com Created Time:2014-08-21 18:22:52 Active: 1
UID:3 First Name:Bittu Last Name: Sharma Email: bsharma@xyz.com Created Time:2014-08-21 18:22:53 Active: 1

So, that was all about connecting to MySQL and fetching / retrieving data using PHP. Hope you liked it.

Pages: 1 2 3

Filed Under: MySQL, PHP, Technology Tagged With: Connecting to MySQL using PHP, How to use PHP to connect to and retrieve data from MySQL, PHP & MySQL, sample php code to connect to mysql database

« Write your first PHP program…
What is a CMS…and WordPress »

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Connect with Facebook

Let’s connect…

  • Facebook
  • Instagram
  • LinkedIn
  • Twitter
  • YouTube

Hello There…

I'm Kamalika, a techpreneur & startup mentor, blogger, hobbyist photographer, Netflix & Kindle indulgent, food connoisseur, Starbucks aficionado and former Disney employee ...next

Looking for something?

Tags

All in One SEO Pack AWS Banaras Blogger Blogging Blogs Dasaswamedh Ghat Ebullientech Ebullientech Interactive LLP Facebook Facebook Developers father's day father's day wishes father's day wishes for dad fathers day love Fathers Day Special Fathers Day wishes from daughter Father’s day memories GeoTech GeoTech Informatics HipHop for PHP Kamalika Guha Roy Kashi Ganga LAMP MySQL Nabaneeta Guha Roy Online Marketing PayPal PayPal's suspension of Indian bank transactions PHP PHP & MySQL Saraswati Puja Search Engine Optimization SEO Startup The Ganges TypePad UCO HUT Varanasi Web 2.0 Web 2.0 Design Web Design Web Marketing WordPress WordPress.COM

Timeline

  • 2024 (1)
  • 2021 (3)
  • 2020 (7)
  • 2019 (4)
  • 2018 (1)
  • 2015 (6)
  • 2014 (13)
  • 2013 (9)
  • 2010 (7)
  • 2009 (2)

Topics

Copyright © 2021 · Kamalika Guha Roy, Powered by: Ebullientech