1

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);
?>
5
  • What does var_dump($result) produce? Commented Aug 24, 2012 at 18:59
  • You might want to put those <li> elements inside a <ul> or <ol> tag. Commented Aug 24, 2012 at 19:00
  • @user1623347 - are those your actual database login details? Commented Aug 24, 2012 at 19:03
  • @andrewsi I removed those lines. SMH Commented Aug 24, 2012 at 19:05
  • Are you getting empty <li> lines, or no <li> lines at all? Empty lines = using the wrong references in $row. No lines at all = query produced no results. Commented Aug 24, 2012 at 19:06

2 Answers 2

1

You want mssql_fetch_assoc

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

Cheers

Sign up to request clarification or add additional context in comments.

3 Comments

var_dump($result) shows this: resource(2) of type (mssql result)
No empty <li> lines are showing up. I'm going to try mssql_fetch_assoc
mssql_fetch_assoc worked if I write the query to only select one of the columns 'code' $query = mssql_query('SELECT prideSelect FROM [Heros].[dbo].[tblHeros]'); 'code' If I change it to 'code' $query = mssql_query('SELECT * FROM [Heros].[dbo].[tblHeros]'); 'code' or 'code' $query = mssql_query('SELECT prideComment FROM [Heros].[dbo].[tblHeros]'); 'code' then I get this message: MSSQL Connection successful MSSQL error: Unicode data in a Unicode-only collation or ntext data cannot be sent to clients using DB-Library (such as ISQL) or ODBC version 3.7 or earlier. fix: Cast ntext field to text?
0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.