0% found this document useful (0 votes)
4 views1 page

Connecting PHP MySQL

Php book

Uploaded by

Imodu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
4 views1 page

Connecting PHP MySQL

Php book

Uploaded by

Imodu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 1

from PHP & MySQL in easy steps

from PHP & MySQL in easy steps


Connecting PHP & MySQL …cont’d

Connection to a MySQL database can be attempted in PHP


with a standard piece of script that describes four connection
parameters of Host, Username, Password, and Database
l 3 Now, begin a second script that incorporates the
connection script, stating its path in the parent directory
<?php
# Incorporate the MySQL connection script.
name. Upon failure, the script provides a descriptive message,
require ( ‘../connect_db.php’ ) ;
whereas on success it specifies the character set to be used

l
when sending data to and from the database server:
4 Next, complete this script by adding instructions to
$dbc = mysqli_connect ( ‘host’, ‘user’, ‘password’, ‘database’ ) display connection information if the attempt has
OR die ( mysqli_connect_error() ) ;
succeeded
mysqli_set_charset( $dbc, ‘charset’ ) ; # Display MySQL version and host.
if( mysqli_ping( $dbc ) )
You need not understand in detail how the script works at this { echo ‘MySQL Server ‘ . mysqli_get_server_info( $dbc ).
stage, but recognize that it contains sensitive information. For ’connected on ‘ . mysqli_get_host_info( $dbc ) ; }
this reason it should not be placed in the web server’s /htdocs
directory like all other PHP scripts, where its contents may be
accessible, but placed instead safely in /htdocs parent directory
– for example, in the “C:/Abyss Web Server” directory rather
l 5 Save the script in your web server’s /htdocs directory as
require.php, then open it in your browser (via HTTP) to
see the connection details on successful connection
than in the “C:/Abyss Web Server/htdocs” directory.
Any PHP script can incorporate another PHP script by
using a “require” statement to specify the other script’s path.
This feature can be used to good effect to incorporate the
connection script without revealing its sensitive information.

l 1 Launch a plain text editor and create a connection script


describing the parameters from the previous pages
<?php

# Connect on ‘localhost’ for user ‘mike’


l 6 Temporarily edit connect_db.php by changing the
database name to one that does not exist, then save the
file and reload the page in your browser to see the error
# with password ‘easysteps’ to database ‘site_db’.
$dbc = mysqli_connect You do not need to
( ‘localhost’ , ‘mike’ , ‘easysteps’ , ‘site_db’ )
understand how these
OR die
( mysqli_connect_error() ) ;
scripts work just now –
they merely ensure you
# Set encoding to match PHP script encoding. can connect to MySQL
mysqli_set_charset( $dbc , ‘utf8’ ) ; with PHP. But you can
usefully refer back to

l 2 Save the file in the parent directory of your web server’s


/htdocs directory folder as connect_db.php l 7 Finally, correct the connect_db.php script by changing
the database name back to site_db, then save the file and
them later to see how
your knowledge of PHP
has progressed.
reload the page to see the connection succeed once more

Congratulations, you have now successfully configured the Abyss


Web Server, PHP engine, and MySQL Server for development.

You might also like