Beginner PHP Tutorial – 114 – Connecting to a Server and Database Part 2

Facebook –
GitHub –
Google+ –
LinkedIn –
reddit –
Support –
thenewboston –
Twitter –





20 responses to “Beginner PHP Tutorial – 114 – Connecting to a Server and Database Part 2”

  1. Nigel Pallatt Avatar

    DO NOT WATCH THIS ONE, SKIP TO 115, a complete waist of time! All good but pointless.

  2. phantom 393 Avatar

    $conn_error = 'Could not connect.';

    $mysql_host = 'localhost';
    $mysql_user = 'root';
    $mysql_pass = '';

    $mysql_db ='a_database';

    if (@!mysqli_connect($mysql_host, $mysql_user, $mysql_pass , $mysql_db)) {
    die ($conn_error);
    } else {
    echo 'connected';


  3. Harsath Bill Gates Avatar

    Having MYSLI error try my code guys

    mysqli_connect("localhost","root","","people") or die("NOt connected to the database");
    echo "Access done to database";

  4. VisualQuizLIVE Avatar

    The right code:

    $host = 'localhost';
    $username = 'root';
    $password = '';
    $database = 'database';
    $link = @mysqli_connect($host, $username, $password);
    $database_connect = @mysqli_select_db($link, $database);

    if( $link && $database_connect){
    echo('Connected Successfully');
    echo('Could Not Connect:(');


  5. binoy sarker Avatar

    my query says query failed

    $con_error='could not connet to the database';

    if(!mysql_connect($mysql_host,$mysql_user,$mysql_pass) || !mysql_select_db($mysql_db)) {
    echo "connected";


    and in my another php filel i run this


    $query="SELECT 'Name', 'Class', 'Roll', 'Section' FROM 'student_info' ORDER BY 'Id'";

    if($query_run = mysql_query($query)){
    echo "query success";

    echo "query failed";


    and it says query failed
    why not query success

  6. siuyu` Avatar

    how can I get the connection of the byethost databse with php?

  7. Rivenatti Avatar

    If someone has trouble with this code, You should use mysqli functions instead of mysql.

    Working code 2016/06/24:


    $conn_error = 'Couldn't connect.';

    $mysqli_host = 'localhost';

    $mysqli_user = 'root';

    $mysqli_pass = '';

    $mysqli_db = 'a_database';

    if(!@mysqli_connect($mysqli_host, $mysqli_user,




  8. Darwin Pose Avatar

    In mysql_select_db you need 2 arguments now so this video is will not work

  9. Panayiotis Palmiris Avatar

    What about the following? Looks better to me.
    $conn_error = 'Could not connect.';

    $mysql_host = 'localhost';
    $mysql_user = 'root';
    $mysql_pass = '';
    $mysql_db = 'test';

    @$mysql_con=mysqli_connect($mysql_host,$mysql_user,$mysql_pass) or die ($conn_error);

    mysqli_select_db($mysql_con,$mysql_db) or die($conn_error);
    echo 'connected';

  10. Alvaro Franco Martins Avatar

    This code works for everyone (15/12/2015)
    You just have to change the $db variable for a database name that you actually have. In my case I have a database called 'test';


    * Connect php to mysql

    $servername = "localhost";
    $username = "root";
    $password = "";
    $db = 'test';

    // Create connection
    $conn = mysqli_connect($servername, $username, $password, $db);

    // Check connection
    if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
    echo "Connected successfully";

  11. Bart Sprengelmeijer Avatar


    $host = 'localhost';
    $name = 'root';
    $password = '';
    $database = 'users';

    if(@mysqli_connect($host, $name, $password)) {
    $mysql_conn = @mysqli_connect($host, $name, $password);

    if(@mysqli_select_db($mysql_conn, $database)) {
    echo 'Connected!';
    } else {
    die('Could not find database.');
    } else {
    die('Could not connect.');


  12. Omar Tarek Avatar

    $mysqlhost = 'localhost';
    $mysqluser = 'root';
    $mysqlpass = '';
    @$mysqlcon= mysqli_connect($mysqlhost,$mysqluser,$mysqlpass) or die('could not connect.');
    MySQLi_select_db($mysqlcon , 'a_database') or die('no such db');

    echo 'connected';

    There has been few changes
    now mysqli is used for the connection and the mysqlii_select_db function usese two arguments

    hope that helps 🙂


  13. V. Peters Avatar

    1000 keer die k*t Knab reclame in your face gaat er niet voor zorgen dat ik daar ga bankieren….
    in tegendeel, het werkt averechts…

    just sayin'

  14. enko goru Avatar

    just rename a_database, everything else it 's ok

  15. nokah vandenhoven Avatar



    $host       = "mysql:dbname=phpacademytutorial;host=localhost";
    $username   = "root";
    $password   = "xxxxxxx";

    $error      = "could not connect to database";

    try {
        $conn = new PDO($host, $username, $password);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } catch(PDOException $e) {
        echo 'ERROR: ' . $e->errorInfo.$error;

  16. S. Bass Avatar

    I swear I did everything perfect, but I keep getting Not Connected! Is it the link I'm using? localhost/mysql_connection.php  or something else? My code is fine I think.

  17. TheAxhol Avatar

    Why do things never work for me

  18. Samantha Gamage Avatar

    Tx lot.  one bye one leaning

  19. Murat Küçüktepe Avatar

    You can also connect by writing

    function connect() {
    $name = "a_database";
    mysql_select_db($name,$connect) or die("Couldnt connect!");


  20. robertcarl rodriguez Avatar

    whew!whew!whew!whew!whew!whew!!! LOL

Leave a Reply

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