SQL 服务器获取数据 php

SQL Server fetch data php

我想通过连接到 SQL 服务器来提取数据库中的数据。我可以连接到 SQL 服务器,但无法打印数据。我得到空白屏幕输出。有什么问题?

<?php
$myServer = "...";
$myUser = "...";
$myPass = "....";
$myDB = "...";

//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
  or die("Couldn't connect to SQL Server on $myServer");

//select a database to work with
$selected = mssql_select_db($myDB, $dbhandle)
  or die("Couldn't open database $myDB");

//declare the SQL statement that will query the database
$query = "SELECT form_adres, form_sehir, form_adsoyad";
$query .= "FROM databasename.omg_user.ie_form";
$query .= "WHERE form_no='15275'";

//execute the SQL query and return records
$result = mssql_query($query);

$numRows = mssql_num_rows($result);
echo "<h1>" . $numRows . " Row" . ($numRows == 1 ? "" : "s") . " Returned </h1>";

while($row = mssql_fetch_array($result))
{
  echo "<li>" . $row["form_adres"] . $row["form_sehir"] . $row["form_adsoyad"] . "</li>";
}
//close the connection
mssql_close($dbhandle);
?>

您在生成的 T-SQL 语句中存在语法错误 - FROMWHERE 之前缺少空格。生成的语句为SELECT form_adres, form_sehir, form_adsoyadFROM databasename.omg_user.ie_formWHERE form_no='15275'。修复语句并始终检查 mssql_query() 执行的结果:

<?php
...

// declare the SQL statement that will query the database
$query = " SELECT form_adres, form_sehir, form_adsoyad";
$query .= " FROM databasename.omg_user.ie_form";
$query .= " WHERE form_no='15275'";

// execute the SQL query and return records
$result = mssql_query($query, $dbhandle);
if ($result === false) {
    echo "Error (mssql_query): ".mssql_get_last_message();
    exit;
}   

...
?>

你可以试试这个代码..

//declare the SQL statement that will query the database
$query = "SELECT form_adres, form_sehir, form_adsoyad";
$query .= "FROM databasename.omg_user.ie_form";
$query .= "WHERE form_no='15275'";
//execute the SQL query and return records
$result = mysql_query($dbhandle, $query);