Mysql Part 1
Mysql Part 1
VYSHNAVIDEGREE
COLLEGE,YMG.
Unit-1
Installing and Configuring MySQL
InstalliDg MySQL on Windows
Ine MySQL installation process on Windows uses a standard Microsoft Windows Installer
(M^I) file to walk you through the installation and configuration of MySQL
Windows XP, Windows Server 2003, Windows Vista, or Windows on your
7 machine.
Go to the MySQL downloads page at
and select the Windows option from the http://dev.mysql.com/downloads/mvsql/
drop-down menu. Download theWindows MSI
5.5.html
Installer file for your systenm, either 32- or 64-bit. When this file has been
copied from the CD-ROM for this book), double-click it to downloaded (or
begin
The following'steps detail the installation of MySQL 5.5.21 on the installation process.
sequence follows the same steps regardless of your Windows Windows 7; the installation
1. Double-click the *.msi file to begin the environment.
installation sequence. You willsee the first screen of
the MySQL Setup Wizard, as shown in Figure 2.5. Click
Next to continue.
ne et te Seto
Cenc
Ingtek te ntconngn p
rem aures
RecoTnndd for rost eY
Concel
button to continue.
3. Confirm your choice in the next screen and click the Install
to the MySQL
4. When the installation is complete, you have the option of continuing
Instance Configuration Wizard.
Wizard, check the Launch the
To continue to the MySQL Instance Configuration the Finish button.
MySQL Instance Configuration Wizard check box and click
MySQL
Arehas
the Next
Wizard Welcome screen, click
Configuration
MySQL Instance configuration:
5. When you see the in the wizard. You will see two options for server click Next to
button to go to the next step Detailed Configuration
radio button, and then
Standard. Select the
Detailed and
Machine, or
continue. running: Developer Machine, Server
machine you are
YouSelect the type of Machine.
Dedicated MySQL Server continue.
making your selection, click Next to
After sQLSavsrstanco
Conguraaon Wizard
Conguato
MyssA Server nstance
CoaioUe the kMSOL Serer
i5 sere intane
dk nd GRU esge
riuence mono,
Flease sded s sever
tpe. TNg
Cnyer N h e
agciatDns m u t e r u n n n g on
Seta server Wetpprebon senok
Chhoose this pgton tor
Ca
base
mnemory space to install data
need to select the
6. In this step we
Page | 2
For BSc VÊh Sem
Programming with PHP and MySQL
VYSHNAVI DEGREE COLLEGE, YMG.
U.KUMARA SWAMY MCA.
to u:e te dett
Pltsse select he eNetor hetrnoDS drtatie f rou se rt wrt
settngs.
mo00 tstepce Settng
CSnc
7. After then we need to set a password for our data base it is called as Root password.
MysQL sever istance Contiguration Wizard
Cance
8. When we press the net button, all the configurations can be done. Those configurations can
be displayed as given bellow:
MysqL Sevar rstaco Contigurason Wizard
Processing conhgursten
Prpare cottgustian
B sturt senice
Ongs ppled.
Pres (Fnish] to cdose te Wzrd.
Programming with PHP and MySQL For BSc VIh Sem Page | 3
U.KUMARA SWAMY MCA. VYSHNAVI DEGREE COLLEGE, YMG.
On Linux/ UNIX and Mac OS X, incorrect permissions do not allow you to start the MySQL
daemon. If this is the case, be sure that you have changed owners and groups to match those
indicated in the installation instructions.
If you see the message ACcess denied when connecting to MySQL, be sure that you are
using the correct username and password.
. If you see the message Can't connect to server, make sure that the MySQL daemon is
running.
BasicSecurity Guidelines
Regardless of whether you are running MySQL on Windows, Linux/UNIX, or Mac OSX, and
no matter whether you administer your own server or use a system provided to you by your
Internet service provider, you must understand basic security guidelines.
Starting MySQL
Securing MySQL begins with the server startup procedure. If you are not the administrator
report
of the server, you cannot change this, but you can certainly check it out and
vulnerabilities to your Internet service provider.
If you see that MySQL is running as root on your system, immediately contact your Internet
the
service provider and complain. If you are the server administrator, you should start
the startup
MySQL process as a non-root user or specify the preferred username in
Commnand line:
# mysqld -user=no_root_user_name
command:
For example, if you want to run MySQL as user mysql, use this
# mysqld -user=mysql
through the mysqld_safe
However, the recommended method for starting MySQL is
installation:
startup script in the bin directory of your MySQL
# bin/nysqld_safe -user=mysql
Programming with PHP and MySQL For BSC VÊh Sem Page | 4
YMG.
VYSHNAVIDEGREE COLLEGE,
U.KUMARA SWAMY MCA.
password pair
looks at the host you are connecting from and the username and
. 1MySQL for your
you are using. If your host is allowed
to connect, your password is correct second
host, MySQL moves to the
usernamne, and the username matches
one assigned to the
step. attempting to use, MySQL verifies that
your user
2. For whichever SQL command you are that database, table, and field.
has permissions to perform that action for
Programming with PHP and MySQL For BSc VIh Sem Page | 5
U.KUMARA SWAMY MCA. VYSHNAVI DEGREE COLLEGE, YMG.
the latest and greatest; it is the version used in this chapter. Howeve, if you choose to install
(or already have installed either in a local or external development environment)
Apache 2.2.x or even Apache 2.0.x, all the PHP and MySQL code in this book will still work
as described. In fact, you will find a number of hosting providers still using Apache 2.0.X
issues
not even Apache 2.2.x, let alone the newest Apache 2.4.x branch. If you experience ny
example,
with installing Apache 2.4.x as described in thischapter, try an earlier version (for
Apache 2.2.x); the installation instructions are renmarkably similar.
for updates containing
The Apache Software Foundation uses minor and revision numbers
nor revision releases follow a set release
ecurity enhancements or bug fixes. Neither minor
to the code and
schedule. When enhancements or fixes are added
releases a new version.
thoroughly tested, the Apache Software Foundation
compression
Apache download site is packaged with different
The official Apache source code, compressed with
several versions of the the tar utility and then
can find packed with
distribution files are first
methods. The system. This
compress utility. installed on your
either the gzip tool or the gunzip utility FreeBSD and
the *. tar.gz version if you have the operating systems such as
Download open source included in
comes installed by default in
not present in your system. (It isn't
*.tar.Z file if gunzip is
utility
Linux. Download the commercial UNIX operating
systems.)
installation of many
the default
Page | 6
For BSc VIh Sem
Programming with PHP and MySQL
VYSHNAVI DEGREE COLLEGE, YMG.
U.KUMARA SWAMY MCA.
VERSION.tar.gz,
Tne ile you want to download will be named something similar to httpd-
where VERSION is the most recent release of Apache. For example,
Apache version 2.4.1 is distributed as a file named httpd-2.4.1.tar.gz. Keep
the downloaded
local/src/.
ile in a directory reserved for source files, such as /usr/src/ or /usr/
Uncompressing the Source Code
suffix), you can
lf you downloaded the tarball compressed with gzip (it will have a tar.gz
uncompress it using the gunzip utility (part of the gzip distribution).
You can uncompress and unpack the software by typing the following command:
#gunzip <httpd-2.4".tar.gz | tar xvf -
Uncompressing the tarball creates a structure of directories, with the top-level directory
named httpd-VERSION. Change your current directory to this top-level directory to prepare
for configuring the software.
Preparing to Build Apache
the configure script in
You can specify which features the resulting binary will have by usingwith
the top-level distribution directory. By default, Apache is compiled a set of standard
modules compiled statically and is installed in the /usr/local/apache2 directory. If you are
happy with these settings, you can issue the following command to configure Apache:
# fconfigure
Building and Installing Apache
The make utility reads the information stored in the makefiles and builds the server and
modules. Type make at the command line to build Apache. You will see several messages
indicating the progress of the compilation, and you will end up back at the prompt. After
compilation is finished, you can instal Apache by typing make install at the prompt. The
makefiles install files and directories and return you to the prompt:
Installing header files
Installing build system files
Installing man pages and online manual
prepared to use PHP, skip ahead to the "Apache Configuration File Structure" section to
learn more about the Apache configuration file and how to use it.
When you're ready to begin the installation, look for the link labeled Win32 Binary including
OpenSSL(MSI Installer). After you download the
installer, double-click the file to start the
installation process. You willget a welcome screen, as shown in Figure
copyright aw and
WARNING: Ths program Is protected by
Internabona treaties.
Next> Cancel
<Back
Page | 8
Programming with PHP and MySQL For BSc VÊth Sem
U.KUMARA SWAMY MCA. VYSHNAVIDEGREE COLLEGE, YMG.
Sottware -including making proprietary modifications except claim that you wrote it, but
De sure to read the license so that you fully understand the terms. After you accept the
license, the installer presents you with a brief introduction to Apache. Following that, it asks
you to provide basic information about your computer, as shown in Figure 3.2. This includes
the full network address for the server (for instance, mycomputer.mydomain.com) and the
administrator's emailaddress.
The server name is the name your clients will use to access your server, and the
administrator email address is added toerror messages so that visitors know how to contact
you when something goes wrong.
Apache HTTP Server 2.2 -Installation Wizard
Server Infomation
Please enter your server's infomation.
Also on this screen, you are prompted to select which installation shortcuts should
be installed- those for starting Apache as a service or those for starting Apache
Installing Apache as a service causes it to run every time Windows starts, andmanually.
control it through the standard Windows service administration tools. Installing you can
Apache
the current user requires you to start Apache manually and set the default port on for
Apache listens to requests to 8080 (instead of 80). Select the appropriate radio which
click Next to continue. button and
The next screen enables you to choose the type of installation:
installation means that Apache binaries and documentation aretypical or custom. A typical
installed, but headers and
libraries are not. This is the best option to choose unless you
modules. plan to compile your own
A custom installation enables you to choose whether to
install header files or documentation.
After selecting thetarget installation directory, which defaults to
c:\Program Files (x86)\ Apache Software Foundation\
with the installation process. If everything goes well, Apache 2.2, the program proceeds
it presents you with the final screen,
shown in Figure
ERVER
PRJJEGT
t
Apache HTTP Server 2.2 - Installation Wizard
Fnish Cancel
< Eac
Configuration File
Structure
httpd.conf. This
Apache
in text files. The main file is installation.
configuration information customize yOur
Apache
Apache keeps all its containers that enable you to performance, and
directives and as authorization,
file contains Apache, such settings refer. For
specific settings of to which those
Directives configure the context directory, or to a
Containers specify server as a whole, toa
network
parameters.
configuration can
refer to the
authorization
example,
single file.
Directives directive syntax:
apply for Apache
The following rules arguments follow the directive
name.
directive spaces. have no
The separated by directive; some
arguments are directive to
The directive arguments vary
from
type of ending
The number
and different line by
it on a
continue
arguments.
single line, but you can
directive occupies
a character (\). its own line.
A backslash must appear on at
line with a directive, and for directives
the previous precede the reference
The pound sign (#) should offers You'll soon learn about somne
a quick
>
http://httpd.apache.org/docs/2.4/mod/quickreference.
documentation
html. the online
The
Apache server knowledge using
supplement your
you should
directives, but
of the basic
documentation.
this model:
typically follows
documentation for directives description of the directive.
Compulsory
parameters
The Apache provides a brief directive options.
Description- This entry format of the brackets.
entry explains the in italics and
This appear
. Syntax italicS, parameters
and optional default value, it appears
here.
the directive can
appear.
appear in has a sections in
which host,
Default Ifthe directive containers or server config, virtual
the are
entry details possible values
C o n t e x t - This containers. The
Sem
MysQL For BSc VÊh
o g r a m m i n g with
PHP and
VYSHNAVIDEGREE COLLEGE, YMG.
U.KUMARA SWAMY MCA.
Troubleshooting
The following subsections describe several common problems that you might encounter the
first time you start Apache.
Already an Existing Web Server
Ifa server is already running on the machine and is listening to the same IP address
and port combination, Apache cannot start successfully. You will get an entry in the
error log file indicating that Apache cannot bind to the port:
[crit] (48)Address already in use: make_sock: could not bind... [alert) no listening sockets
available, shutting down
To solve this problem, you need to stop the running server or change the Apache
configuration to listen on a different port.
No Permission to Bind to Port
You will get an error if you do not have administrator permissions and you try to
bind to a privileged port (between 0 and 1024):
[crit] (13)Permission denied: make_sock: could not bind to address 10.0.0.2:80 [alert] no
listening sockets available, shutting down
To solve this problem, you must either log on as the administrator before starting Apache or
change the port number; 8080 is acommonly used nonprivileged port.
Access Denied
You might not be able to start Apache if you do not have permission to read the
configuration files or to write to the log files. You willget an error similar to the following:
(13) Pernuission denied: httpd: could not open error log file
This problem can arise if the user who built and installed Apache is different from
the user trying to run it.
Installing PHP on Windows
Installing PHP on Windows requires nothing more than downloading the distribution file.
To download the PHP distribution files, go to the homne of PHP on Windows,
http://windows.php.net/, and follow the link to the Downloads page. Grab the latest
version of the thread-safe ZIP package-for this example, we are using 54.0. Our
distribution will be named something similar to php- VERSION.zip, where VERSION is the
most recent release number.
After the file downloads to your system, double-click it to launch our unzipping software.
The distribution is packed up with relative pathnames already in place, so extract the files to
a new directory called C:\ php\, where it will place all the files and subdirectories under that
new directory.
Programming with PHP and MySQL For BSc VIth Sem Page | 11
U.KUMARA SWAMY MCA. VYSHNAVI DEGREE COLLEGE, YMG.
Next, go to the C:\php\ directory and copy the php.ini-development file to php.ini. Now, to
get a basic version of PHP working with Apache, we need to make a few minor
modifications to the Apache configuration file.
Integrating PHP with Apache on Windows
To ensure that PHP and Apache get along with one another, we need to add a few items to
the httpd.conf configuration file. First, find a section that looks like this:
# Example:
# LoadModule foo_module modules/mod_foo.so
Page | 12
For BSc VIh Sem
Programming with PHP and MysQL
U.KUMARA SWAMY MCA. VYSHNAVI DEGREE COLLEGE, YMC.
The echo and print) Statements :- These two statements are used to print the
information on the browser.
Example :- echo "example";
print("example");
Combining HTML and PHP:
Listing 4.2 A PHP Script Incorporated into HTML
1: <\DOCTYPE html>
2: <html>
3: <head>
4: <title>A PHP script including HTML</title>
5: </head>
6: <body>
7: <h1><?php echo "hello world";
8: </body> ?></h1>
9: </html>
Page | 14 P
Programming with PHP and MySQL For BSc V[h Sem
VYSHNAVI DEGREE COLLEGE,
YMG.
U.KUMARASWAMY MCA.
abundant extension library.
hs will helps in creating a support community,
Speed: It is relative fast since it uses much system resource. with C, it's very
O
for those who are familiar
U Easy to use: It uses C like syntax, so
website scripts.
easy for them to pick up and it is very easy to create
so when bugs are found, it can
OStable: Since it is maintained by many developers,
be quickly fixed.
find functional modules you need such
UPowerful library support: we can easily
as PDE, Graph etc. connect to database easily using
DBuilt-in database connection modules: You can
we will use database
PHP, since many websites are data/content driven, so
apps.
frequently, this will largely reduce the development time of web Mac, it's easy for
Windows, Linux and
OCan be run on many platforms, including
users to find hosting service providers.
Disadvantages of PHP
can see the source code, if there are
OSecurity :Since it is open sourced, so all people explore the weakness of PHP.
bugs in the source code, it can be used by people to
since it is not very modular.
ONot suitable for large applications: Hard to maintain programmers and lead to
OWeak type: Implicit conversion may surprise unwary compare equal because
unexpected bugs. For example, the strings "1000" and "le3"
they are implicitly cast to floating point numbers.
Building blocks of PHP
Explain about Variables?
Definition
Avariable is a special container that we can define, which then "holds" a value,
such as a number, string,object, array, or a Boolean.
PHP Variables
A
variable can have a short name (likex and y) or a more descriptive name (age,
carname, total volume).
Rules for PHP variables:
> Avariable starts with the $ sign, followed by the name of the variable
> A variable name must start with a letter or the underscore character
> A variable name cannot start with a number
Avariable name can only contain alpha-numeric characters and underscores (A-z, 0-9,
and_)
> Variable names are case-sensitive ($age and $AGE are two different variables)
Examples
<?php
$txt = "W3Schools.com";
echo "I love $txt!";
<?php
Bx =5;
By = 4;
echo $x +Sy:
myTest): $x</p>";
echo "<p>Variable x outside function is:
within
?>
function has a LOCAL SCOPE and can only be accessed
a
A variable declared within
that function:
Example
<?php
function myTest() {
$x =5; // local scope
function is:$x</p>";
echo "<p>Variable x inside
myTest();
//using x outside the
function will generate an error
function is: $x</p>";
echo "<p>Variable x outside
?> variables
Super global Variables PHP has several predefined
your own creation, all
addition to global variables of present, and their values are available to
In always
These variables are actually an array of other
variables:
called superglobals. following superglobals is method.
our scripts. Each of thevariables provided toa script through the POST method.
GET
$_GET contains any variables provided to a script through the
$_POST contains any through a cookie.
variables provided to a script
$_COOKIE contains any provided to script through file uploads.
a
$_FILES contains any
variables script locations.
information such as headers, file paths, and
.$_SERVER COntains
the server
variables provided to a script as part of
.$_ENV containsany
POST, or
environment.
provided to a script via GET,
.$_REQUEST containsany variables
COOKIE
input mechanisms.
Page 16
Static
Normally, when a function is completed/executed, all of its variables are
sometimes we want a local variable NOT to be deleted. We need deleted. However,
To do this, use the static keyword whern you first declare it for a further job.
Example the variable:
<?php
function myTest() {
static $x=0;
echo $x;
$xt+;
myTest):
myTest):
myTest);
?>
Data Types
Different types of data take up different amounts of menmory and may be treated differently
when they are manipulated by a script.
PHP is loosely typed, meaning that it automatically determines the data type at the time data
is assigned to each variable.
Variables can be used flexibly - in one instance, a variable can hold a string and then later in
the script it can hold an integer or some other data type.
PHP supports the following data types:
Type Example Description
Boolean true One of the special values true or false
Integer 5 A whole number
Float or double 3.234 Afloatingpoint number
String "hello" A collection of characters
Examples
(string)
<?php
$x= "Hello world!";
$y = Hello world!';
echo $x;
echo "<br>";
echo $y;
?>
(null) <?php
$x = "Hello world!";
$x = null;
var_dump($x);
?>
integer
<?php
Bx = 5985;
ar _dump($x);
oat
?php
K=10.365;
r_dump($x);
ay
ohp Page | 18 P.
MySQL For BSc VÊH
Sem
ramming with PHP
and
YMG
VYSHNAVIDEGREE COLLEGE,
U.KUMARA SWAMY MCA.
Scars =
array("Volvo" "BMW",Toyo
ta");
var_dump(Scars);
2>
String operators
o Array operators
Arithmetic operatorS perform commonarithmetical
with numeric values to
The PHP arithmetic operators are used multiplication etc.
subtraction,
operations, such as addition,
Arithmetic operators: Sample Result
Example
Name
Operator 13
10+3
+
Addition
7
10-3
Subtraction 3.3333333333333
10/3
Division 30
10*3
Multiplication
1
10%3
Modulus
a
use Operators
SX. "
$X =
$X %= 5
test "
$X .= return
operands
and
their
using fails.
tests the test
if
orfalse
c o m p a r a t i v e
Comparison operators
perform s u c c e s s f u l
is
the test
operators
C o m p a r i s o n
value
true if
Boolean
the
YMG.
VYSHNAVIDEGREE COLLEGE,
U.KUMARA SWAMY MCA.
type. false
$X > 4
Left is greater
Greater than
than right.
$x >= 4 true
Greater than or Left is greater
than or equal
equal to
to right. false
$x < 4
Left is less
Less than
than right. true
$X <= 4
Less than or Left is less than
integer variable,
Increment/Decrement operators integer constant 1 from an
add or subtract the
These operators allow us to
variable itself.
assigning the result to the decrenment or pre
increment/ decrement.
post increment /
These are applied as
below table.
These are shown in
Description
Operator
Name
returns $x
Increments $x by one, then
Pre-increment
++$X increments $x by one
Returns $x, then
Post-increment
$X++ returns $x
Decrenents $x by one, then
Pre-decrement
$X-
Page | 21
Sem
MySOL For BSc VÊh
VYSHNAVI DEGREE COLLEGE
U.KUMARA SWAMY MCA.
Logical operators
Logicaloperators test combinations of Boolean values.
and And Left and right are true. true and false false
Not The single operand is ! true false
not true.
String operators
PHP has two operators that are specially designed for strings.
Name Example Result
Operator
Concatenation of $txt1
Concatenation $txti. $txt2
and $txt2
Array operators
The PHP array operators are used to compare arrays.
Operator Precedence
The PHP engine usually reads your expression from left to right. However, you can override
operator precedence by putting parentheses around your expressions.
Progranming with PHP and MySQL For BSc VIh Sem Page | 22 P.
VYSHNAVI DEGREE COLLEGE,
YMG.
U.KUMARA SWAMY MCA.
Constarnts
changed during the
Aconstant is an identifier (name) for a simple value. The value cannot be
script.
constant name).
A valid constant name starts with a letter or underscore (no $ sign before the
Note: Unlike variables, constants are automatically global across the entire script.
Create a PHP Constant
To create a constant, use the define) function.
Syntax
define(ame, value, case -insensitive)
Parameters:
o name: Specifies the name of the constant
o value: Specifies the value of the constant
o case-insensitive: Specifies whether the constant name should be case-insensitive.
Default is false
The example below creates a constant with a case-sensitive name:
Example
<?php
define("GREETING", "Welcome to W3Schools.com!");
echo GREETING;
2>
TOgramming with PHP and MySQL For BSc VIh Sem Page | 23
U.KUMARA SWAMY MCA. VYSHNAVI DEGREE COLLEGE.
1. The if Statement :
Syntax
True
if (expression) If(cond
ition)
Statement - x:
else
?>
Clause with the if Statement:
3. Using the elseif
Syntax
if (expression)
Page | 2F
Sem
PHP and MySQL For BSc V[h
Programming with
VYSHNAVI DEGREE COLLEGE, YMG.
U.KUMARA SWAMY MCA.
Block 2 statements
Block 3 statements
else
Ext