| Home > Computer & I'net > CREATE DATABASE... |
|
|
|||||||||||||||
|
|
|||||||||||||||
|
||||||||||||||||
CREATE DATABASE | |
The HOST value is usually set as localhost. You may want to check your host server's FAQ/HELP area or email them to be sure. The USERNAME and PASSWORD are relatively easy considering they are the same ones you use to log onto your hosting account normally. Now you should be ready to create your first database with the help of PHP. This example shows three parts to do this. <?php // set your infomation. $dbhost='localhost'; $dbusername='david'; $dbuserpass='mypassword'; $dbname='test'; // connect to the mysql database server. $link_id = mysql_connect ($dbhost, $dbusername, $dbuserpass); echo "success in database connection."; // create the database. $dbname=$dbusername."_".$dbname; if (!mysql_query("CREATE DATABASE $dbname")) die(sql_error()); echo "success in database creation."; ?> The first section declares your basic variables. These let you easily use them throughout the rest of the coding. The second section connects to the database server. We are using a variable to activate a MySQL command. The variable will hold a positive or negative value depending on the successful connection or not. mysql_connect is the database command to connect to the database server using the initial settings. The third section is doing a few things. First it is altering the value of the database name. On some systems, the databases are arranged in a "UserName_DataBaseName" setup. Thus to be compatable, the script is creating this effect for us. The example will produce a database file called "david_test". The next line is using an IF statement. If the statement holds a true value (being a negative result from the query), it will stop the current script (die) and print out the specific SQL error. mysql_query is the command being used to activate the specified information which happens to be to CREATE a DATABASE called test. As long as this line did not "die", the last echo command will print out a successful creation. If that is all successful, this is what you will have so far visually... david_test
----------------- Article by David Stanley. Visit his site http://www.htmlite.com. Reprinted with permission. | |
| Articles |
•Auto & Trucks•Business•Computer & I'net·General·Apache·CSS·Database·Hardware·HTML·Javascript&DHTML·LinuxMySQL·Operating System·Perl / CGI·PHP·Programming·Publishing·Search Engines·Software Problems·SSI·Tips & Tricks·Utilities·Web Design•Family•Food & Drink•Gardening•Health•Other•Pets•Psychology•Spiritual•Travel•Women |
| Calculators |
|