I am unable to add a table to the database currently set in the connection parameter.
When i echo it on screen, the table to be created(query) displays correctly but it does not add the table to the database.
And when i add the same table(which is being displayed) manually in phpmyadmin, it creates a table successfully without any errors.
Page: mysql_1.php
Code: Select all
class database
{
//**Atributes for the class database
var $db_HOST = "localhost"; //server name
var $db_USER = "root"; //username
var $db_PASS = ""; //password
var $banco = "test2"; //database name
//var $CONST_ERRO = "Sorry could not connect to the database";
var $dbc;
function database()
{
//a constructor to a class connect_db()
$this->connect_db();
}
// Methods/Queries to be executed for a particular database
//function connect_db($server_name,$db_user_name,$db_pass,$db_name)
function connect_db()
{
//Method to connect to a database
$this->dbc = mysql_connect($this->db_HOST,$this->db_USER,$this->db_PASS);
$m = mysql_select_db($this->banco,$this->dbc);
if(!empty($m))
{
echo "<br>Connection to the Database $this->banco Successfull<br>";
}
if(empty($m))
echo "<br>Sorry could not connect to the Database";
return($dbc);
}
function create_db($db_name)
{
//creates a new database.
$sql="CREATE DATABASE ".$db_name;
echo $sql."<br>";
$tmp=$this->query_db($sql);
if(!empty($tmp))
{
echo "<br>Database <b>$db_name</b> Created Successfully.";
}
else
{
echo("<br>Database cannot be created.");
}
}
function add_table($tblname,$num_fields,$fields)
{
//adding a table into the database
$sql="CREATE TABLE ".$tblname."(<br> ";
if(!empty($num_fields))
{
$sql.=$fields."<br>)";
}
echo $sql."<br>";
$tmp=$this->query_db($sql);
if(isset($tmp))
{
echo "<br>Table <b>$tblname</b> Created Successfully.";
}
else
{
echo("<br>Table cannot be created.");
}
}
}Code: Select all
include("mysql_1.php");
$c = new database(); //defining the object for class database
$c->create_db(test2); //Creating a new database
//creating a table mytable within the newly created database
$c->add_table("mytable",6,"id smallint(5) NOT NULL auto_increment,LastName varchar(30) NOT NULL,FirstName varchar(30) NOT NULL,Address varchar(255) NOT NULL,Age int(3) NOT NULL,email varchar(255) NOT NULL,PRIMARY KEY(id)");Thanks and Regards,
Dream2rule