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

[ad_1]
Facebook –
GitHub –
Google+ –
LinkedIn –
reddit –
Support –
thenewboston –
Twitter –


Posted

in

by

Comments

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

    <?php
    $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
    <?php

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

  4. VisualQuizLIVE Avatar

    The right code:
    <?php

    $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');
    }else{
    echo('Could Not Connect:(');
    }

    ?>

  5. binoy sarker Avatar

    my query says query failed
    connect.inc.php
    <?php
    $mysql_host='localhost';
    $mysql_user='root';
    $mysql_pass='';
    $mysql_db='admin';

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

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

    ?>

    and in my another php filel i run this
    <?php

    require('connect.inc.php');

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

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

    }
    else{
    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:

    <?php

    $conn_error = 'Couldn't connect.';

    $mysqli_host = 'localhost';

    $mysqli_user = 'root';

    $mysqli_pass = '';

    $mysqli_db = 'a_database';

    if(!@mysqli_connect($mysqli_host, $mysqli_user,
    $mysqli_pass)&&!@mysqli_select_db($mysqli_db)){

    die($conn_error);

    }

    ?>

  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.
    <?php
    $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';

    <?php

    /*
    * 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

    <?php

    $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

    <?php
    $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 🙂

    copied*

  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

    <?php

    #error_reporting(0);

    $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() {
    $host="localhost";
    $user="root";
    $password="…"
    $name = "a_database";
    $connect=mysql_connect($host,$user,$password);
    mysql_select_db($name,$connect) or die("Couldnt connect!");
    }

    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 *