MS SQL Server Concepts and Programming Question: Download MS SQL Server PDF

PHP MSSQL - How To Create a New Table?

Tweet Share WhatsApp

Answer:

If you want to create a table in the SQL Server database, you can run the CREATE TABLE SQL statement using the mssql_query() function, as shown in the following sample script:

<?php
$con = mssql_connect('LOCALHOST','sa','GlobalGuideLine');
mssql_select_db('GlobalGuideLineDatabase', $con);

# creating a new table
$sql = "CREATE TABLE ggl_links ("
. " id INT NOT NULL"
. ", url VARCHAR(80) NOT NULL"
. ", notes VARCHAR(1024)"
. ", counts INT"
. ", time DATETIME"
. ")";
$res = mssql_query($sql,$con);
if (!$res) {
print("Table creation failed with error: ");
print(" ".mssql_get_last_message()." ");
} else {
print("Table ggl_links created. ");
}

mssql_close($con);
?>

If you run this script for the first time and there is no existing table called ggl_links in the database, you will get:

Table ggl_links created.

If you run it again, you will get:

Table creation failed with error:
There is already an object named 'ggl_links' in the
database.

Download MS SQL Server PDF Read All 394 MS SQL Server Questions
Previous QuestionNext Question
How To List All Field Names in the Result Set using mssql_field_name()?PHP MSSQL - How To Drop an Existing Table?