FORUM HOME | WHUK BLOG   
WEB HOSTING UK AFFORDABLE WEBSITE HOSTING SERVICES IN UNITED KINGDOM
PHP LINUX SHARED HOSTING WINDOWS ASP.NET HOSTING PACKAGES
ECOMMERCE HOSTING ASP MSSQL MS ACCESS ODBC FRONTPAGE HOSTING
CPANEL WHM FANTASTICO RESELLER DEDICATED SERVER WEB HOSTING
CHEAP PLESK CPANEL HTML MYSQL BEST UK VPS HOSTING COMPANY
CHEAP RELIABLE UK HOSTING PROVIDER SINCE 2001
MANAGED WEB HOSTING SERVICE
AFFORDABLE WEBSITE HOSTING SERVICES IN UNITED KINGDOM

Web Hosting UK Forums | Linux Windows Dedicated Server and cPanel VPS Hosting Forum » Web Hosting and Domains » FAQ's / Tutorials.

Reply
 
LinkBack Thread Tools Display Modes

  #1 (permalink)  
Old 14-10-07, 06:34 AM
Junior Member
 
Join Date: May 2007
Posts: 14
Default Using PHP to control Mysql Database

I'm going to tell everyone now how to control MySQL with PHP, I learned it a couple weeks ago and thought I should share what I know, so here goes.

Things you'll need:
A webserver that supports PHP
A simple text editor (notepad works fine, but I use DzSoft PHP Editor)

If you don't have a web server that supports PHP, you can download "Apache" which is a HTML webhost for your personal computer, "MySQL" which is obviously the MySQL Engine, and you'll need PHP 5.2.4. If you need help setting any of it up, drop me a line at xxx@hotmail.com, now on with the MySQL stuff.

Put any code into your text editor and just save it with the extension ".php", you may need to set to Save As Type "All Files (*.*)" with notepad and type in the entire name manually, like index.php and remember to put <?php at the beginning of every file, and ?> at the end.

The first thing you need to do is connect to the server:
PHP Code:
mysql_connect("localhost","admin","password") or die(mysql_error()); 
mysql_connect takes 3 parameters, first is the server, localhost means the machine the PHP will be running on, replace admin with your username and password with your password.

die(mysql_error()) only activates if the statement doesnt succeed, and stops the script and outputs the error that MySQL gave.

At this point you'll want to create, drop, or select a database (or all of the above).
To select a database that already exists, you would do something like this:
PHP Code:
mysql_select_db("myDatabase") or die(mysql_error()); 
Replacing myDatabase with the name of your existing database.

To create a database use the following.
PHP Code:
mysql_create_db("myDatabase") or die(mysql_error()); 
Of course, replacing myDatabase with the name of your desired database

To drop(delete) a database.
PHP Code:
mysql_drop_db("myDatabase") or die(mysql_error()); 
I'm going to tell everyone now how to control MySQL with PHP, I learned it a couple weeks ago and thought I should share what I know, so here goes.

Things you'll need:
A webserver that supports PHP
A simple text editor (notepad works fine, but I use DzSoft PHP Editor)

If you don't have a web server that supports PHP, you can download "Apache" which is a HTML webhost for your personal computer, "MySQL" which is obviously the MySQL Engine, and you'll need PHP 5.2.4. If you need help setting any of it up, drop me a line now on with the MySQL stuff.

Put any code into your text editor and just save it with the extension ".php", you may need to set to Save As Type "All Files (*.*)" with notepad and type in the entire name manually, like index.php and remember to put <?php at the beginning of every file, and ?> at the end.

The first thing you need to do is connect to the server:

PHP Code:
mysql_connect("localhost","admin","password") or die(mysql_error());


mysql_connect takes 3 parameters, first is the server, localhost means the machine the PHP will be running on, replace admin with your username and password with your password.

die(mysql_error()) only activates if the statement doesnt succeed, and stops the script and outputs the error that MySQL gave.

At this point you'll want to create, drop, or select a database (or all of the above).
To select a database that already exists, you would do something like this:
PHP Code:
mysql_select_db("myDatabase") or die(mysql_error());


Replacing myDatabase with the name of your existing database.

To create a database use the following.

PHP Code:
mysql_create_db("myDatabase") or die(mysql_error());


Of course, replacing myDatabase with the name of your desired database

To drop(delete) a database.

PHP Code:
mysql_drop_db("myDatabase") or die(mysql_error());


Replacing of course myDatabase with whatever database you want to eradicate, please note, this is irreversable.

Now, so far we've done nothing but connect to a database, which will naturally seem VERY boring, so now we can create tables! And read them too

Here's the code for a sample system to make a table:

PHP Code:
<?php 
mysql_connect
("localhost""admin""password") or die(mysql_error()); 
mysql_create_db("myDatabase") or die(mysql_error()); 

mysql_query("CREATE TABLE addressBook( 
id INT NOT NULL AUTO_INCREMENT,  
PRIMARY KEY(id), 
name VARCHAR(15),  
phoneNumber INT)"

or die(
mysql_error());   

echo 
"Data inserted into MySQL"

?>
Save this file as something like setupdb.php and test it out, after changing the login information at the top, and now I'll explain all the new code:
"mysql_query" sends data directly to MySQL, like typing it in from the MySQL command line, queries are like what comes next: id INT NOT NULL AUTO_INCREMENT; means we want a new field called "id" it should be an integer(a number), cannot be null, and auto increments, so it keeps track of itself.

"PRIMARY KEY(id)" sets id to be the primary key, which means by default, it will sort by that.

"name VARCHAR(15)" means to make a field called name, which accepts characters and up to 15 of them.

"phoneNumber VARCHAR(15)" says we want another string value to hold their phone number."



Now, we've created a table, but have nothing to show (except a table)

Now, to put some info into the table, put this into a PHP document
PHP Code:
<?php 
mysql_connect
("localhost","admin","password"
mysql_select_db("myDatabase"
mysql_query("INSERT INTO addressBook (name, phoneNumber) VALUES('John Smith','555-1234')") or die(mysql_error(); 
mysql_query("INSERT INTO addressBook (name, phoneNumber) VALUES('Joe Bloggs','555-4321')") or die(mysql_error(); 
mysql_query("INSERT INTO addressBook (name, phoneNumber) VALUES('Sally Smith','555-1235')") or die(mysql_error(); 
mysql_query("INSERT INTO addressBook (name, phoneNumber) VALUES('Beatrice Bloggs','555-5321')") or die(mysql_error(); 
?>
Don't forget to replace the login information! And only run this once, otherwise you'll have 2 of every entry (not that it would really harm anything)

I think INSERT INTO kind of explains itself, so I won't go into it today.

Now only one thing left in this tutorial, and that's reading the information.
PHP Code:
<?php 
mysql_connect
("localhost","admin","password"
mysql_select_db("myDatabase"
  
$result mysql_query("SELECT * FROM addressBook") or die(mysql_error()); 


while(
$row mysql_fetch_array($result)){ 
echo 
$row['name']. " - "$row['phoneNumber']; 
echo 
"<br />"

?>
The only new new MySQL statement I used here was mysql_fetch_array, which just fetches the first part of an array from the results returned by the SELECT * FROM addressBook, the rest of the new things are PHP and HTML, but this is a MySQL tutorial.
Reply With Quote

  #2 (permalink)  
Old 14-10-07, 10:06 AM
Senior Member
 
Join Date: Jan 2007
Location: Dorset
Posts: 1,119
Default

I think it should be made clear though to anyone using this example that "username","password" should be fetched and not simply typed on this page. If there were a page error then your user and password would be displayed for all to see.
Reply With Quote

Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump


All times are GMT. The time now is 12:27 PM.

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Copyright 2001-2010 Web Hosting UK. All rights reserved.
Web Hosting UK Forum





Site Map

Shared Cloud
Shared Cloud From £1

Affiliate Program
Earn up to £300 Per Sale

Dedicated Servers
Dedicated Server Hosting

Cloud Hosting
Cloud Server Hosting

Load Balanced Server
Load Balancing Server

VPS Hosting
Linux VPS Hosting

Windows VPS
Windows 2003 VPS

Zimbra Hosting
Zimbra Email Hosting

cPanel Hosting
Shared Linux Hosting

Windows Hosting
Shared Windows Hosting

Coldfusion Hosting
Windows Coldfusion Hosting

cPanel Reseller Hosting
Reseller Hosting

Windows Reseller
Windows Reseller Hosting

Email Web Hosting
Email Hosting

Semi-Dedicated Server
Semi-Dedicated Hosting

Remote Backup Plans
Offsite Backup Service


cpanel hosting
Knowledgebase Articles

Pre-Sales Question
Web Hosting FAQ's

Dedicated Hosting
Dedicated Server FAQ's

Virtual Private Servers
VPS Hosting

PHP MySQL Hosting
cPanel Hosting

Windows Hosting
ASP MSSQL Hosting

Domain Name
Domain registration FAQ's

CMS Hosting
CMS Hosting FAQ's

Payment Gateways
Payment FAQ's


Support Tutorials

cPanel Tutorials
cPanel Flash Tutorials

Wordpress Tutorials
Wordpress Flash Tutorials

Plesk Tutorials
Plesk Flash Tutorials

PhpMyadmin Tutorials
PhpMyadmin Flash Tutorials

Drupal Tutorials
Drupal Flash Tutorials

Mambo Tutorials
Mambo Flash Tutorials

Joomla Tutorials
Joomla Flash Tutorials

More Hosting Tutorials