I want to pull the data in the database by connecting to SQL Server. I can connect to SQL Server but I can’t print the data. I get blank screen output. What is the problem?
JavaScript
x
<?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);
?>
Advertisement
Answer
You have a syntax error in the generated T-SQL Statment – missing spaces before FROM
and WHERE
. The generated statement is SELECT form_adres, form_sehir, form_adsoyadFROM databasename.omg_user.ie_formWHERE form_no='15275'
. Fix the statement and always check the result from mssql_query()
execution:
JavaScript
<?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;
}
?>