Log in

View Full Version : sqlsrv_fetch_array() Error



willscarlet
11-19-2012, 03:08 PM
Hello everyone. I have a database (SQL server 2008 R2) that I finally got PHP5 installed on. I Can run basic simple querries against the DB without problem. I am now trying to allow a user to search the database by drop down menus and text fields to narrow a search down and am running into a few problems. Any advice would be greatly appreciated.

Error Message:
Warning: sqlsrv_query() expects at least 2 parameters, 1 given in C:\Inetpub\wwwroot\msag\index.php on line 34

Here is the index.php File:

<center><h1><u> MSAG Database </u></h1>
<br>
<br>

<?php include 'includes/menu.php'; ?>

<form method="post" action="index.php">
<input type="hidden" name="submitted" value="true" />

<label>Search Category:

<select name="category">
<option value="StreetName">Street Name</option>
</select>
</label>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<label>Search Criteria: <input type="text" name="criteria" /></label>

<input type="submit" name="search" value="Search"/>

</form>

</center>
<?php

include 'includes/db/connect.php';

if(isset($_POST['search']) || isset($_POST['submit']))
{
$category = $_POST['category'];
$criteria = $_POST['criteria'];

$query = "SELECT * FROM MSAG WHERE $category LIKE %$criteria% ORDER BY StreetName ASC";
$resource=sqlsrv_query($query);
}
?>
<form action="" method="post">
<table align="center" border="1" cellpadding="2" cellspacing="2" width="100%" >
<tr align = 'center'>
<td><b>Street Name</b></td>
</tr>
<?php
while($result=sqlsrv_fetch_array($resource))
{
echo "
<tr align = center>
<td>".$result['StreetName']."</td>
<td><a href=\"modify2.php?id=".$result['StreetID']."\"><input type=\"button\" name = \"details\" value = \"Details\" /></a></td>
</tr>
";
}
?>
</table>
</form>

Ultimately I would like to be able to have several different drop boxes and text fields to search by. I thank you all in advance for your time and help.

traq
11-19-2012, 09:00 PM
The error message tells you exactly what's wrong:
Warning: sqlsrv_query() expects at least 2 parameters, 1 given in ...
sqlsrv_query() (http://php.net/sqlsrv_query) expects the first argument to be a database connection (this is the arg you are missing), and the second argument to be the SQL query.