Some basics of PHP and MySQL
Well, I’ve maked a list of queries to facilitate the MySQL management via PHP.
<?php
# Some basics of PHP & MySQL/*Creating a Database */
$conn = mysql_connect(‘mysql_host’,'mysql_username’,'mysql_pwd’); //Connection to the Database
if(!$con) { //We check if the Database connection was succesful
die (‘Error connecting to the Database.’);
} else {
$res = mysql_create_db(‘DB_NAME’,$conn); //Create the Database, change ‘DB_NAME’ to the Database name you wish.
if(!$res) { //We check if the Database creation was succesful
echo ‘Error creating Database.’;
} else {
return true;
}
}/*Deleting a Database */
$conn = mysql_connect(‘mysql_host’,'mysql_username’,'mysql_pwd’); //Connection to the Database
if(!$con) { //We check if the Database connection was succesful
die (‘Error connecting to the Database.’);
} else {
$res = mysql_drop_db(‘DB_NAME’,$conn); //Delete the Database, change ‘DB_NAME’ to the Database name you wish delete.
if(!$res) { //We check if the Database creation was succesful
echo ‘Error deleting Database.’;
} else {
return true;
}
}/*Creating a table into a Database */
$conn = mysql_connect(‘mysql_host’,'mysql_username’,'mysql_pwd’); //Connection to the Database
mysql_select_db(“DB_NAME”,$conn); //Selecting the Database (change ‘DB_NAME’ to the name of your database
if(!$con) { //We check if the Database connection was succesful
die (‘Error connecting to the Database.’);
} else {
$res = mysql_query(“CREATE TABLE Table_Name(Table,Fields)”,$conn); //Create a table into a Database, separate fields by comma.
if(!$res) { //We check if the table creation was succesful
echo ‘Error creating table.’;
} else {
return true;
}
}/*Deleting a table of a Database */
$conn = mysql_connect(‘mysql_host’,'mysql_username’,'mysql_pwd’); //Connection to the Database
mysql_select_db(“DB_NAME”,$conn); //Selecting the Database (change ‘DB_NAME’ to the name of your database
if(!$con) { //We check if the Database connection was succesful
die (‘Error connecting to the Database.’);
} else {
$res = mysql_query(“DROP TABLE Table_Name”,$conn); //Deletes a table of a Database
if(!$res) { //We check if the table delete was succesful
echo ‘Error creating table.’;
} else {
return true;
}
}/*Inserting, Deleting and Modifying fields of a table */
$conn = mysql_connect(‘mysql_host’,'mysql_username’,'mysql_pwd’); //Connection to the Database
mysql_select_db(“DB_NAME”,$conn); //Selecting the Database (change ‘DB_NAME’ to the name of your database
if(!$con) { //We check if the Database connection was succesful
die (‘Error connecting to the Database.’);
} else {
$res=mysql_query(“ALTER TABLE Table_Name ADD field type”,$conn); //Adds a field, where ‘field’ is the name of the field that you whish add and type is the datatype, ex.: int
$res=mysql_query(“ALTER TABLE Table_Name DROP field”,$conn); //Delete a field
$res=mysql_query(“ALTER TABLE Table_Name MODIFY field newtype”,$link);
}
?>
If you don’t understand something, you can make a post comment with your doubt, and I’ll try to solve it, in the measure that I can.
Leave a Comment