Friday, October 14, 2016
How To Create a mySQL Database Using PHP
How To Create a mySQL Database Using PHP
mySQL database is most popular database system used in internet. Today I am going to show you how to create a mySQL database using PHP script. To create a new database, well be using the following syntax here; CREATE DATABASE database_nameBut, we must use the mysql_query() function to execute the above command. This mysql_query() function is used to send a query to a MySQL connection. After we have base commands ready, we can arrange it to access and create your new MySQL database. Before we proceed further, you must keep in mind that you must have your web host supporting at least one MySQL database connection and you have full access to your web host control panel. However we are not going to enter the controlpanel zone, but the PHP script surely will have to. Now lets assume the following, $dbhost=localhost; // is your local host name.The following script will create a new database named, anup_database. $con variable is set to connect to the database, if it fails to it outputs with Could not connect mysql error. If it becomes successful to create new database, it outputs with Your new Database created. Else it will result Error creating database with mysql error listed. Here, w are successfully done to create a new database. Since weve created database we can create tables after then. Let me not end this tutorial right here, else you might be smothering about what to do next. Let me teach you how to create a table using PHP again. To Create a table, we are using the following syntax. CRATE TABLE table_nameIt will create the specified table in the specified mysql database name. While creating we added the CREATE DATABASE syntax in the mysql_query() function to execute it. In the same way well be adding CREATE TABLE syntax to the mysql_query(function). For an example, lets say that, we need to create a table named people, with three columns. And we need the column names: FirstName, LastName and Age. The following command creates table with $dbname variable (it varies with variable you provide before the command executes), here in our case, anup_database, as weve specified $dbname=anup_database; as our data base name. // Create tableNote:The above script is not complete and your browser will never accept ist as a PHP code until your code begins and ends with Varchar(15) data type allows you to enter only 15 characters (you can see that Ive added it after the column FirstName and LastName). And int (in the Age column) refers to an integer. Upto now what far weve reached is, with complex combinations of commands and functions, we will be able to create new database with new table and columns. You can easily combine the both scripts in to one file, like in the example below: Hope that was pretty helpful. |
Go to link Download
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment