Displaying random records using ms sql server and php

To get random records in ms sql server using php is also very easy process, just you need to write this query in order to get the random records in ms sql server.

SELECT * FROM YOURTABLE ORDER BY newid()

Displaying random records using ms sql server and php

In php you cannot use select * statement for ms sql server, if you try to use it then it will give u error mention below

message: 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. (severity 16)

So to overcome this problem and to get random records too, use this query

$select_query="SELECT id,CAST(question AS TEXT) AS question,CAST(answer_one AS TEXT) AS answer_one,CAST(answer_two AS TEXT) AS answer_two,CAST(answer_three AS TEXT) AS answer_three,CAST(answer_two AS TEXT) AS answer_four,CAST(correct_answer AS TEXT) AS correct_answer FROM $tbl_name order by NEWID()";
$resultset=mssql_query($select_query);


Note:- For those columns you use varchar datatype it is better to convert then into TEXT in order to avoid the Unicode error.

So this is the way to Select and Display the Random Records in MS SQL SERVER using PHP

0 comments: