我本机装的wamp的开发环境,因为要开发新的站点,但数据是存在远程的MSSQL数据库里面的
我在网上找到两种连接MSSQL的方法,一种我用mssql_connect 总是连接不上,后来我用下面这种方法连接上的
2<?php
/** * php使用ODBC连接sql server数据库实例
*/
$server='ip地址或服务器名';
$username='数据库用户名';
$password='数据库密码';
$database='数据库名';
$connstr = "Driver={SQL Server};Server=$server;Database=$database";
if ( !odbc_connect($connstr,$username,$password,SQL_CUR_USE_ODBC)){
echo "Couldn't connect to SQL Server on $server";
}else{
echo "Connect successfully!<br>";
}
?>
现在是连接数据成功了
但后面的SQL查询语句怎么写
我在后面这样写它会报错
if ( !odbc_connect($connstr,$username,$password,SQL_CUR_USE_ODBC))
{ echo "Couldn't connect to SQL Server on $server";
}
else{
while($row=mssql_fetch_array(mssql_query('select * from t_item')))
{
echo $row[0];
}
} Warning: mssql_query() [function.mssql-query]: Unable to connect to server: (null) in C:\wamp\www\mssql.php on line 12Warning: mssql_query() [function.mssql-query]: A link to the server could not be established in C:\wamp\www\mssql.php on line 12Warning: mssql_fetch_array(): supplied argument is not a valid MS SQL-result resource in C:\wamp\www\mssql.php on line 12
是不是这种连接的查询语法不是这样写的
PHPMS SQL
我在网上找到两种连接MSSQL的方法,一种我用mssql_connect 总是连接不上,后来我用下面这种方法连接上的
2<?php
/** * php使用ODBC连接sql server数据库实例
*/
$server='ip地址或服务器名';
$username='数据库用户名';
$password='数据库密码';
$database='数据库名';
$connstr = "Driver={SQL Server};Server=$server;Database=$database";
if ( !odbc_connect($connstr,$username,$password,SQL_CUR_USE_ODBC)){
echo "Couldn't connect to SQL Server on $server";
}else{
echo "Connect successfully!<br>";
}
?>
现在是连接数据成功了
但后面的SQL查询语句怎么写
我在后面这样写它会报错
if ( !odbc_connect($connstr,$username,$password,SQL_CUR_USE_ODBC))
{ echo "Couldn't connect to SQL Server on $server";
}
else{
while($row=mssql_fetch_array(mssql_query('select * from t_item')))
{
echo $row[0];
}
} Warning: mssql_query() [function.mssql-query]: Unable to connect to server: (null) in C:\wamp\www\mssql.php on line 12Warning: mssql_query() [function.mssql-query]: A link to the server could not be established in C:\wamp\www\mssql.php on line 12Warning: mssql_fetch_array(): supplied argument is not a valid MS SQL-result resource in C:\wamp\www\mssql.php on line 12
是不是这种连接的查询语法不是这样写的
PHPMS SQL
你是使用 odbc 连接数据库的
那么就应该使用 odbc 函数组进行操作
那如果我想echo我要查的那个表后面该怎么写
$rs = odbc_exec($conn, 'select * from tbl_name');
$ar = odbc_fetch_array($rs);
print_r($ar);
我刚才在百度上搜了一下 我是这些写的 网页中能显示数据$conn=odbc_connect($connstr,$username,$password,SQL_CUR_USE_ODBC);
$sql="select * from t_item where fname like '%南昌%'";
$rs=odbc_exec($conn,$sql);
if (!$rs)
{exit("Error in SQL");}
echo "<table><tr>";
echo "<th>fnumber</th>";
echo "<th>fname</th></tr>";while (odbc_fetch_row($rs)){
$fname=odbc_result_all($rs,fnumber);
$fnumber=odbc_result($rs,fname);
echo "<tr><td >$fname</td>";
echo "<td align='center'>$fnumber</td></tr>";
}
odbc_close($conn);
echo "</table>";我想问一下 ,能把SQL查询改成调用SQL SQRVER中的存储过程吗,用什么方法调用存储过程,参数怎么给,
给我指明大概的方法就可以了
存储过程也属于 sql 指令
select fname='南昌' into #mssql exec mssql drop table #mssql 可以正常查询
在PHP里写成这样
$sql=" select fname='南昌' into #mssql exec mssql drop table #mssql";
$rs=odbc_exec($conn,$sql);
就会报错Warning: odbc_fetch_row() [function.odbc-fetch-row]: No tuples available at this result index in C:\wamp\www\mssql.php on line 20