PHP MySQL Tutorial: Connect to a MySQL Database -HD-


farren , labelle – 2 weeks and i still cannot get technical support on the phone. – Sucks!!!!


In this tutorial, you will learn how to connect to a MySQL database using PHP.

Be sure to SUBSCRIBE because there will be a new tutorial every week.

Follow me on Twitter @RiverCityGraphx

Like us on on facebook: http://on.fb.me/i8UQpt

Suggest tutorials at http://www.rivercitygraphix.com

For project files, help forums, and more check out the website at http://www.rivercitygraphix.com

For business related inquires contact us at rivercitygraphix@yahoo.com

RiverCityGraphix

20 Comments
  • Daniel Nicolas
    Posted at 03:31h, 22 February

    Hi, I don't seem to be able to connect to my db.  I followed everything to the letter.  Can I talk to you?

  • Dukez Venom
    Posted at 14:10h, 15 June

    the first part is working, but selecting my db wont work

  • Viral Thread
    Posted at 11:59h, 04 August

    i just get a blank screen. i am using filegator

  • orange18
    Posted at 09:35h, 25 September

    can u show how to create a database and how to configure database like where did u get the test123

  • Ruttna Samhället
    Posted at 01:45h, 14 December

    In order to make extend my db-connect script to take a username and password from form input and modify those values in the included file to I have to declare the form data variables as globals?

  • Apollyon
    Posted at 02:09h, 21 December

    🙂 so that means that any sign up form that i make in that page it will appear on my db?

  • Arkaidyn
    Posted at 02:29h, 27 March

    Can someone help me out? Got this error message; **(I"ve solved this now, resolution is at the bottom for anyone that needs it)**
    Fatal error: Uncaught Error: Call to undefined function mysql_connect() in C:xampphtdocstest_db.php:8 Stack trace: #0 {main} thrown in C:xampphtdocstest_db.php on line 8

    this is my php;
    1 <?php
    2
    3 $db_host = "localhost";
    4 $db_username = "root";
    5 $db_pass = "password";
    6 $db_name = "test_database";
    7
    8 @mysql_connect("$db_host","$db_username","$db_pass") or die ("Could not connect to MySQL.");
    9 @mysql_select_db("$db_name") or die ("Cannot find specified database.");
    10
    11 echo "You have successfully connected to the database.";
    12 ?>

    Resolution from here down 🙂
    I did a bit of googling and as of PHP version 5 the line "@mysql_connect" is not supported anymore, so instead i did the following to connect;
    1 <?php
    2
    3 $connection = mysqli_connect('localhost', 'root', 'password2016', 'test_database');
    4
    5 mysqli_query($connection, 'CEATE TEMPORARY TABLE `table`');
    6
    7 echo "You have successfully connected to the database.";
    8 ?>

  • Umut Karapınar
    Posted at 06:38h, 24 September

    selamlar olsun

  • Leon Safal
    Posted at 07:50h, 09 October

    it says "Parse error: syntax error, unexpected T_ECHO in C:connect.php on line 10" what can i do to fix it can anybody help me??

  • Felipe Cort
    Posted at 15:55h, 21 November

    thanks !! Good tutorial, it s very abstract until getting all working!

  • Jack Claudiu
    Posted at 10:51h, 05 February

    Doesn't work. I get this when I try to run localhost/mysql_connect.php –> Object not found!

    The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.

    If you think this is a server error, please contact the webmaster.

    Error 404

    localhost
    Apache/2.4.23 (Win32) OpenSSL/1.0.2h PHP/5.6.28

  • hannah ajayi
    Posted at 17:51h, 23 April

    What if you are using phpmyadmin? Would it be the same process?

  • Lovely Heart
    Posted at 10:06h, 02 June

    you are number 1

  • Michael Chambers
    Posted at 01:19h, 22 August

    //use the below in replacement of what is shown when working with version 7 and later as old code has been depreciated and will get an undefined error

    $con = mysqli_connect
    mysqli_select_db($con, "$db_name")

  • xXDeFy_The_WorldXx
    Posted at 13:47h, 07 October

    Can someone help me, i keep getting could not connect to MYSQL

  • Anis Mughal
    Posted at 18:34h, 12 December

    i got this error:-
    Fatal error: Uncaught Error: Call to undefined function mysql_connect() in C:xampphtdocsmysql_connect.php:7 Stack trace: #0 {main} thrown in C:xampphtdocsmysql_connect.php on line 7
    can anyone help me plz?

  • Tech The Trucker
    Posted at 17:55h, 21 December

    for anyone watching now here's the right code (also note it's mysqli with an "i"):

    <?php
    $Hostname = "localhost";
    $User = "yourusername";
    $Password = "yourpassword";
    $dbName = "yourDbName";

    $Link = mysqli_connect($Hostname, $User, $Password, $dbName) or die ("Could not connect to Server.");
    mysqli_select_db($Link, "$dbName") or die ("No Database Found.");

    echo "Connected Successfully.";
    ?>

    mysql_connect & mysql_select_db are both deprecated. all you have to do is add an i after the L. and with select_db, it takes 2 params, so all i did was give the mysqli_connect a name and drop it in mysqli_select_db with the db string.

    hope this helps.

  • KryptonianCuber
    Posted at 03:05h, 01 January

    my screen won't load 🙁

  • razashield2617
    Posted at 15:38h, 13 February

    thanks a bunch

  • E Kennedy
    Posted at 11:53h, 20 March

    Thanks. Much appreciated. Simple but effective for beginners.