MS SQL Server Concepts and Programming Question: Download Questions PDF
PHP ODBC - How To Create a New Table?
![MS SQL Server Interview Answer MS SQL Server Interview Question](https://globalguideline.com/public/images/interview/13.png)
Answers:
Answer #1If you want to create a table in the database connected through a ODBC DSN, you can run the CREATE TABLE SQL statement using the odbc_exec() function, as shown in the following sample script:
<?php
$con = odbc_connect('ggl_SQL_SERVER','sa','GlobalGuideLine');
# 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 = odbc_exec($con, $sql);
if (!$res) {
print("Table creation failed with error: ");
print(odbc_error($con).": ".odbc_errormsg($con)." ");
} else {
print("Table ggl_links created. ");
}
odbc_close($con);
?>
<?php
$con = odbc_connect('ggl_SQL_SERVER','sa','GlobalGuideLine');
# 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 = odbc_exec($con, $sql);
if (!$res) {
print("Table creation failed with error: ");
print(odbc_error($con).": ".odbc_errormsg($con)." ");
} else {
print("Table ggl_links created. ");
}
odbc_close($con);
?>
Answer #2If 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:
S0001: [Microsoft][ODBC SQL Server Driver][SQL Server]
There is already an object named 'ggl_links' in the
database.
Table ggl_links created.
If you run it again, you will get:
Table creation failed with error:
S0001: [Microsoft][ODBC SQL Server Driver][SQL Server]
There is already an object named 'ggl_links' in the
database.
Download MS SQL Server Interview Questions And Answers
PDF
Previous Question | Next Question |
How To List All Columns in a Table using odbc_columns()? | PHP ODBC - How To Insert Data into an Existing Table? |