www.Tutorialsforu.info

Free Tutorials Cave

  • Increase font size
  • Default font size
  • Decrease font size
Your Ad Here



MySql General

Create a MySQL Database With PHP

E-mail Print
Current revision posted to MySQL Tutorials by admin on 12/18/2008 8:19:26 PM

To create a database use the mysql_query() function to execute an SQL query like this

<?php
include 'config.php';
include 'opendb.php';

$query  = "CREATE DATABASE phpcake";
$result = mysql_query($query);


include 'closedb.php';
?>

Please note that the query should not end with a semicolon.

PHP also provide a function to create MySQL database, mysql_create_db(). This function is deprecated though. It is better to use mysql_query() to execute an SQL CREATE DATABASE statement instead like the above example.

If you want to create MySQL database using PHP mysql_create_db() function you can do it like this :

<?php
include 'config.php';
include 'opendb.php';

mysql_create_db('phpcake');

include 'closedb.php';
?>

If you want to create tables in the database you just created don't forget to call mysql_select_db() to access the new database.

Note: some webhosts require you to create a MySQL database and user through your website control panel (such as CPanel). If you get an error when trying to create database this might be the case.

Creating the Tables

To create tables in the new database you need to do the same thing as creating the database. First create the SQL query to create the tables then execute the query using mysql_query() function.

Example : contact.php
Source code : contact.phps

<?php
include 'config.php';
include 'opendb.php';

$query  = 'CREATE DATABASE phpcake';
$result = mysql_query($query);

mysql_select_db('phpcake') or die('Cannot select database');

$query = 'CREATE TABLE contact( '.
         'cid INT NOT NULL AUTO_INCREMENT, '.
         'cname VARCHAR(20) NOT NULL, '.
         'cemail VARCHAR(50) NOT NULL, '.
         'csubject VARCHAR(30) NOT NULL, '.
         'cmessage TEXT NOT NULL, '.
         'PRIMARY KEY(cid))';

$result = mysql_query($query);

include 'closedb.php';
?>

Of course when you need to create lots of tables it's a good idea to read the query from a file then save in $query variable instead of writing the query in your script.

<?php
include 'config.php';
include 'opendb.php';

$queryFile = 'myquery.txt';

$fp    = fopen($queryFile, 'r');
$query = fread($fp, filesize($queryFile));
fclose($fp);
$result = mysql_query($query);

include 'closedb.php';
?>

 

Deleting a Database

As with creating a database, it is also preferable to use mysql_query() and to execute the SQL DROP DATABASE statement instead of using mysql_drop_db()

<?php
include 'config.php';
include 'opendb.php';

// ... do something here

$query  = 'DROP DATABASE phpcake';
$result = mysql_query($query);

// ... probably do something here too

include 'closedb.php';
?>

After the database and the tables are ready it's time to put something into the database.


Read Full Article
 

Connect to MySQL Database

E-mail Print
Current revision posted to MySQL Tutorials by admin on 12/18/2008 8:17:51 PM

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

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die                      ('Error connecting to mysql');

$dbname = 'petstore';
mysql_select_db($dbname);
?>

$dbhost is the name of MySQL server. When your webserver is on the same machine with the MySQL 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 : MySQL Tutorial

Don't forget to select a database using mysql_select_db() after connecting to mysql. If no database selected 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. For example if the MySQL server name is db.php-mysql-tutorial.com and the port number is 3306 (the default port number for MySQL) then you you can modify the above code to :

 

<?php
$dbhost = 'db.php-mysql-tutorial.com:3306';
$dbuser = 'root';
$dbpass = 'password';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die                      ('Error connecting to mysql');

$dbname = 'petstore';
mysql_select_db($dbname);
?>

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

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

Source code : config.phps , opendb.phps

<?php
// This is an example of config.php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$dbname = 'phpcake';
?>

 

<?php
// This is an example opendb.php
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die                      ('Error connecting to mysql');
mysql_select_db($dbname);
?>

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

<?php
include 'config.php';
include 'opendb.php';

// ... do something like insert or select, etc

?>


Closing the Connection

The 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 function call in a file named closedb.php.

Source code : closedb.phps

<?php
// an example of closedb.php
// it does nothing but closing
// a mysql database connection

mysql_close($conn);
?>

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

<?php
include 'config.php';
include 'opendb.php';

// ... do something like insert or select, etc

include 'closedb.php';
?>


Read Full Article
 

Getting Started with MySQL

E-mail Print
Current revision posted to MySQL Tutorials by admin on 12/18/2008 8:16:22 PM

MySQL Tutorial - Getting Started

By : PHP-MySQL-Admin
Nov 11, 2005

 

This mysql tutorial covers the basic stuff that you can do with MySQL. But before we go any further make sure you already have MySQL installed, if you don't have it installed check out this page : Installing PHP and MySQL (plus Apache).

This part of tutorial covers the following :


Read Full Article
 

How would you compress your MySQL Backup

E-mail Print

How would you compress your MySQL Backup


Artice taken from <a href="http://www.mysqlperformanceblog.com/2008/06/05/how-would-you-compress-your-mysql-backup/"> Here </a>.

Backing up MySQL Database most people compress them - which can make a good sense in terms of backup and recovery speed as well as space needed or be a serious bottleneck depending on circumstances and approach used. First I should mention this question mainly arises for medium and large size databases - for databases below 100GB in size compression performance is usually not the problem (though backup impact on server performance may well be).


Read more...
 

MySQL Configuration Files

E-mail Print

MySQL Configuration Files

 

              Configuring a MySQL server is often just a matter of editing the configuration file to make any changes you need and then restarting the server. While that sounds rather simple, adjusting the server's configuration is something you're not likely to do on a daily basis. More likely, you've installed MySQL, configured it minimally or with the defaults, and then let it run. Most users never go back and adjust the server configuration until a problem arises. As a result, it's easy to forget how to configure MySQL.
 

Read more...
 


Page 4 of 5

Subscribe By Email

Enter your email address:

Delivered by FeedBurner

Translate

Donate

Development & maintainance needs time & money.
With your donation you can help us to keep this project alive
Donate:
  Monthly Monthly
Currency
Amount