user1623347
user1623347

Reputation: 15

PHP MSSQL mssql_fetch_array - can't get results to echo

I've successfully created a connection to an MSSQL database, executed a query and returned the results using mssql_num_rows. I'm able to get $numRows to echo and show "Rows Returned" on the page.

The columns in the table have these names: employeeName, prideSelect, prideComment, flagpoleSelect, flagpoleComment. All are nvarchar(50) or nvarchar(max)

What I'm not able to do is to echo any of the data in the table. I'm new to php/sql and I've tried searching for a solution with no luck. I'm not sure if it has to do with the database columns being set as nvarchar or not. Or if it is an error in my code. I've tried mssql_fetch_array and mssql_fetch_row to display the reults, but can't get either to work. Below is the code that I'm using:

<?php

//connects to Heros database

$conn = mssql_connect($host, $uid, $pwd) or die('Could not connect')
        or die("Couldn't connect to SQL Server on $host");

$selected = mssql_select_db($db, $conn)
        or die("Couldn't open database $db");

echo "MSSQL Connection successful";

//declare the SQL statement that will query the database
$query = mssql_query('SELECT * FROM [Heros].[dbo].[tblHeros]');

//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>";

//display the results 
while ($row = mssql_fetch_array($result)) {
    echo "<li>" . $row["employeeName"] . $row["prideSelect"] . $row["prideComment"] . "</li>";
}

//close the connection
mssql_close($conn);
?>

Upvotes: 1

Views: 8121

Answers (2)

Jesenko Sokoljak
Jesenko Sokoljak

Reputation: 19

You wrote: $query = mssql_query('SELECT * FROM [Heros].[dbo].[tblHeros]');

instead of: $query = 'SELECT * FROM [Heros].[dbo].[tblHeros]';

Upvotes: 0

Madbreaks
Madbreaks

Reputation: 19539

You want mssql_fetch_assoc

http://php.net/manual/en/function.mssql-fetch-assoc.php

Cheers

Upvotes: 1

Related Questions