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




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 …

Original source


25 responses to “PHP MySQL Tutorial: Connect to a MySQL Database -HD-”

  1. 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

  2. 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 ?>

  3. I tried connect to my database and it did work. Please tell me whats wrong with my code.

    <?php
    $Name= $_POST ['Name'];
    $Number= $_POST ['Number'];
    define ( 'DB_NAME', 'counter');
    define ( 'DB_USER', 'bartleys_latty');
    define ( 'DB_PASSWORD', 'romans828');
    define ( 'DB_HOST', 'localhost');

    $link = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD)
        or die (mysql_error());
    if ($link == true){
    echo "connected successfully";    
    }

    $select_db = mysql_select_db('DB_NAME');

    if ($select_db == false){
        
    echo "did not connect";    
    }

    mysql_query($sql, $link);
    ?>

  4. Hi, i'd like to know when we connect PHP to the Database, 
    Do you need more than one connection to it, i mean when you have a Website that should store data such as: (registered users, orders, comments, so on). Is MySQL database that smart to store every peace of database itself after defining one connection to it with PHP?
    I'm actually using wordpress, but i'd like to know the basics behind it.

Leave a Reply