Opening a Connection to MySQL Database From PHP is Easy. Just Use the Mysql_connect() Function Like This

Embed Size (px)

Citation preview

  • 8/9/2019 Opening a Connection to MySQL Database From PHP is Easy. Just Use the Mysql_connect() Function Like This

    1/28

    Opening a connection to MySQL database from PHP is easy. Just use the mysql_connect() functionlike this

    $dbhost is the name of MySQL server. When your webserver is on the same machine with theMySQL server you can use localhost or 127.0.0.1 as the value of $dbhost. The $dbuser and $dbpass

    are valid MySQL user name and password. For adding a user to MySQL visit this page : MySQLTutorial

    Don't forget to select a database using mysql_select_db() after connecting to mysql. If no databaseselected your query to select or update a table will not work.

    Sometimes a web host will require you to specify the MySQL server name and port number. Forexample if the MySQL server name is db.php-mysql-tutorial.com and the port number is 3306 (thedefault port number for MySQL) then you you can modify the above code to :

    It's a common practice to place the routine of opening a database connection in a separate file. Theneverytime you want to open a connection just include the file. Usually the host, user, password anddatabase name are also separated in a configuration file.

    An example of config.php that stores the connection configuration and opendb.php that opens theconnection are :

    Source code : config.phps , opendb.phps

  • 8/9/2019 Opening a Connection to MySQL Database From PHP is Easy. Just Use the Mysql_connect() Function Like This

    2/28

    So now you can open a connection to mysql like this :

    Closing the ConnectionThe connection opened in a script will be closed as soon as the execution of the script ends. But it's

    better if you close it explicitly by calling mysql_close() function. You could also put this functioncall in a file named closedb.php.

    Source code : closedb.phps

    Now that you have put the database configuration, opening and closing routines in separate filesyour PHP script that uses mysql would look something like this :

  • 8/9/2019 Opening a Connection to MySQL Database From PHP is Easy. Just Use the Mysql_connect() Function Like This

    3/28

    include 'closedb.php';?>Recent CommentsBy: phpmsqltutor Posted on 12-02-2009 10:29 PM

    The IP address. e.g. 192.168.1.5:3306

    If you don't know, 3306 is the default mysql port.

    By: Bright-Tutorials Posted on 12-21-2009 8:42 PMReal awesome tutorial, between this one and the one at http://bright-tutorials.com I was able tounderstand some basic php code!

    By: Bright-Tutorials Posted on 12-21-2009 9:21 PMGreat tutorial. I really appreciate it. This tutorial and the ones over at http://bright-tutorials.comreally helped me with php and mysql. Thanks!

    By: zeroge Posted on 01-01-2010 4:45 AMGreat tutorial and great idea ... just in my way I get an error

    Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'me'@'here' (usingpassword: NO) in /home/bla/bal/balla/opendb.php on line 3

    Unable to select database

    Where "balla" is the password protected directory. Trying to open the directory itself by typingsimply the URL like htt://mydomain.com/balla/ I will be prompted for providing a username and

    password, though the directory seems to work.

    Why is it I get then this error when trying to call the page that hosts the "includes" ?

    Thanks a lot

    By: Ivan Posted on 05-13-2010 5:12 AMI'm new to PHP, hope someone can give me a hand : )

    I can't connect to MySQL, IE prompted me "HTTP 500" can't display the web page.

    By following the instructions from a PHP-MySQL book, I did the followings:

    I installed Apache 2.2.15, PHP 5.3.2, MySQL 5.1.45.

    Created directory c:\Program Files\PHP\extensions, and have libmysql.dll and php_mysqli.dll savedinside.

  • 8/9/2019 Opening a Connection to MySQL Database From PHP is Easy. Just Use the Mysql_connect() Function Like This

    4/28

    In c:\windows\system32, libmysql.dll was included.

    In c:\Program Files\PHP\php.ini, two lines were included:

    extension_dir = c:\Program Files\PHP\extensions

    extension=php_mysqli.dll

    my connection.php file saved in c:\Program Files\Apache Software Foundation\Apache2.2\htdocs,and called from localhost/connection.php

    Any things else I still missed?

    thanks all.

    View All

  • 8/9/2019 Opening a Connection to MySQL Database From PHP is Easy. Just Use the Mysql_connect() Function Like This

    5/28

  • 8/9/2019 Opening a Connection to MySQL Database From PHP is Easy. Just Use the Mysql_connect() Function Like This

    6/28

  • 8/9/2019 Opening a Connection to MySQL Database From PHP is Easy. Just Use the Mysql_connect() Function Like This

    7/28

  • 8/9/2019 Opening a Connection to MySQL Database From PHP is Easy. Just Use the Mysql_connect() Function Like This

    8/28

  • 8/9/2019 Opening a Connection to MySQL Database From PHP is Easy. Just Use the Mysql_connect() Function Like This

    9/28

  • 8/9/2019 Opening a Connection to MySQL Database From PHP is Easy. Just Use the Mysql_connect() Function Like This

    10/28

  • 8/9/2019 Opening a Connection to MySQL Database From PHP is Easy. Just Use the Mysql_connect() Function Like This

    11/28

  • 8/9/2019 Opening a Connection to MySQL Database From PHP is Easy. Just Use the Mysql_connect() Function Like This

    12/28

  • 8/9/2019 Opening a Connection to MySQL Database From PHP is Easy. Just Use the Mysql_connect() Function Like This

    13/28

  • 8/9/2019 Opening a Connection to MySQL Database From PHP is Easy. Just Use the Mysql_connect() Function Like This

    14/28

  • 8/9/2019 Opening a Connection to MySQL Database From PHP is Easy. Just Use the Mysql_connect() Function Like This

    15/28

  • 8/9/2019 Opening a Connection to MySQL Database From PHP is Easy. Just Use the Mysql_connect() Function Like This

    16/28

  • 8/9/2019 Opening a Connection to MySQL Database From PHP is Easy. Just Use the Mysql_connect() Function Like This

    17/28

  • 8/9/2019 Opening a Connection to MySQL Database From PHP is Easy. Just Use the Mysql_connect() Function Like This

    18/28

  • 8/9/2019 Opening a Connection to MySQL Database From PHP is Easy. Just Use the Mysql_connect() Function Like This

    19/28

  • 8/9/2019 Opening a Connection to MySQL Database From PHP is Easy. Just Use the Mysql_connect() Function Like This

    20/28

  • 8/9/2019 Opening a Connection to MySQL Database From PHP is Easy. Just Use the Mysql_connect() Function Like This

    21/28

  • 8/9/2019 Opening a Connection to MySQL Database From PHP is Easy. Just Use the Mysql_connect() Function Like This

    22/28

  • 8/9/2019 Opening a Connection to MySQL Database From PHP is Easy. Just Use the Mysql_connect() Function Like This

    23/28

  • 8/9/2019 Opening a Connection to MySQL Database From PHP is Easy. Just Use the Mysql_connect() Function Like This

    24/28

  • 8/9/2019 Opening a Connection to MySQL Database From PHP is Easy. Just Use the Mysql_connect() Function Like This

    25/28

  • 8/9/2019 Opening a Connection to MySQL Database From PHP is Easy. Just Use the Mysql_connect() Function Like This

    26/28

  • 8/9/2019 Opening a Connection to MySQL Database From PHP is Easy. Just Use the Mysql_connect() Function Like This

    27/28

  • 8/9/2019 Opening a Connection to MySQL Database From PHP is Easy. Just Use the Mysql_connect() Function Like This

    28/28