PHP + SQL - Using SQL Server 'Create To' scripts to create tables through PHP
Get the solution ↓↓↓Solution:
YourCREATE
statement is correct, just remove the keywordGO
from your script. TheGO
keyword is not part of T-SQL, it is SQL Server utilitiy statement.
Because I don't know how your class is written, I've made a simple script for test using PHP Driver for SQL Server (SQLSRV_FETCH_ASSOC
constant is specific for this driver). It works correct withoutGO
statement and gives error with this statement (PHP 7.1 and SQL Server Express 2012).
<?php
$server = 'server\instance,port';
$cinfo = array(
"Database"=>'database'
);
$conn = sqlsrv_connect($server, $cinfo);
if( $conn === false )
{
echo "Error (sqlsrv_connect): ".print_r(sqlsrv_errors(), true);
exit;
}
$sql =
"CREATE TABLE [dbo].[departments](
[department_pk_fk] [varchar](25) NOT NULL,
[des] [varchar](100) NULL,
CONSTRAINT [PK_departamentos] PRIMARY KEY CLUSTERED
([department_pk_fk] ASC) WITH (
PAD_INDEX = OFF,
STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON
) ON [PRIMARY]
) ON [PRIMARY]";
$stmt = sqlsrv_query($conn, $sql);
if( $stmt === false ) {
echo "Error (sqlsrv_query): ".print_r(sqlsrv_errors(), true);
exit;
}
sqlsrv_free_stmt($stmt);
sqlsrv_close($conn);
echo 'OK'."</br>";
?>
If you don't know how the SQL statement is verified, try to remove schema name (dbo
), brackets for object names ([]
), because there can be some additional syntax corrections in class method.
Other things, that can cause problems are permissions.
You can always usesqlsrv_errors()
to get additional information about specific errors.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: call to undefined function mysqli_connect()
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.