Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Warning: sqlsrv_query() expects parameter 1 to be resource, object given in C:\xampp\htdocs\phpconn\index.php on line 8

Warning: sqlsrv_fetch_array() expects parameter 1 to be resource, bool given in C:\xampp\htdocs\phpconn\index.php on line 10

What I have tried:

<?php
 
 include 'conn.php';

$query = " SELECT * FROM Item ";


$res = sqlsrv_query($conn, $query);

while ($row = sqlsrv_fetch_array($res, SQLSRV_FETCH_ASSOC))
{

	echo $row['Description'];
}



?>
Posted
Updated 12-Oct-21 23:22pm
v4
Comments
Chris Copeland 13-Oct-21 5:43am    
It's hard to diagnose this without knowing what's in the conn.php file, but it sounds like the $conn variable isn't being initialised as a database connection. Check that the $conn variable is actually being assigned from sqlsrv_connect(), and possibly provide the contents here (with any credentials removed)
A Ali_786 13-Oct-21 5:56am    
$conn = new PDO($dsn, $user, $pass);
Chris Copeland 13-Oct-21 6:24am    
That's your problem, you're trying to mix and match the PDO class (which I would say is probably a better choice to use) with the sqlsrv_ methods. In order to establish a connection for sqlsrv_query() you have to use sqlsrv_connect()

If you want to use PDO instead then that's fine, you'll have to use the PDO-specific methods such as PDO::query

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900