how to establish connection to database using PHP MySql

Krosterolent

New Member
Is the following code correct to create a database in php?\[code\]<?php $con=mysql_connect('localhost','admin','admin'); if(!$con) { die("could not connect:' .mysql_error()); } $sql = "CREATE DATABASE db1"; mysql_select_db("db1", $con); $sql = "CREATE TABLE year ( ayear varchar(10), fyear varchar(10) )"; if(isset($_POST['id'])) { $ayear = $_POST['ayear']; $fyear = $_POST['fyear']; if($ayear != "" && $fyear != "") { $query = "INSERT INTO year VALUES ('$ayear', '$fyear')"; $result = mysql_query($query) or die(mysql_error()); } else echo "one of the field is empty"; } if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } mysql_close($con); ?> \[/code\]After executing the code, if i check in MySql for the created database, the database was not created. What is the problem with the code? how can i improvise it? Can't i use create database command inside the
 
Back
Top