Welcome Guest, Not a member yet? Register   Sign In
How to use Forge Class to create a database schema?
#9

(01-20-2017, 04:45 AM)InsiteFX Wrote: You would not be able to use the CodeIgniter database settings at the start.

Code to create the database:

PHP Code:
<?php
$serverName 
"localhost";
$userName   "username";
$password   "password";

// Create the Database Connection
$connId mysqli_connect($serverName$userName$password);

// Check the Database Connection
if ($connId === false)
{
 
   exit("ERROR: Could not connect. " mysqli_connect_error());


// Create the Database
// You have to supply the database name below!
$sql "CREATE DATABASE yourDatabaseName";

// Check that the Database was created
if (mysqli_query($connId$sql))
{
 
   echo "Database created successfully";
}
else
{
 
   echo "ERROR: Creating Database. " mysqli_error($connId);
}

// Close the Connection
mysqli_close($connId);

?>

You would then need a way to edit and update the CodeIgniter Database settings with the new information,
Once that is done then you would be able to use the CodeIgniter Database.

Here is pure code to create database table:

PHP Code:
<?php
$serverName 
"localhost";
$userName   "username";
$password   "password";

// Attempt MySQL server connection. Assuming you are running MySQL server
$connId mysqli_connect($serverName$userName$password);
 
// Check connection
if ($connId === false)
{
 
   exit("ERROR: Could not connect. " mysqli_connect_error());
}
 
// Attempt create table query execution
$sql "CREATE TABLE persons(person_id INT(4) NOT NULL PRIMARY KEY AUTO_INCREMENT, first_name CHAR(30) NOT NULL, last_name CHAR(30) NOT NULL, email_address VARCHAR(50))";

if (
mysqli_query($connId$sql))
{
 
   echo "Table persons created successfully";
}
else
{
 
   echo "ERROR: Could not able to execute $sql. " mysqli_error($connId);
}
 
// Close connection
mysqli_close($connId);
?>

Hope that gets you started.

Thanks for the code!

I thought that could be a way to do it in pure CodeIgniter, but seems your solution is the best performance way to do it.

I will keep my solution, because even today is a MySQL application, if in future it changes, using Forge Class will make it compatible without any change.
Reply


Messages In This Thread
RE: How to use Forge Class to create a database schema? - by Rodrigo Valentim - 01-24-2017, 04:16 AM



Theme © iAndrew 2016 - Forum software by © MyBB