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.
Warning: sqlsrv_query() expects at least 2 parameters, 1 given in C:\Inetpub\wwwroot\msag\index.php on line 34
Your missing the connection resource in the query call. It's required as the first parameter. Since you likely set it up in that connect include, we don't know the name for it, but if I had to guess it'd be $db or $con which is pretty typical.
You may want to check as well for PDO drivers, it may be easier to use the PDO abstraction instead.
That indicates that your query has failed.
Use this to find out why: $resource=sqlsrv_query($conn, $query) or die(print_r(sqlsrvr_errors(), true));.
Interesting enough, the sqlsrv_query appears to accept either a string query or a bindable prepared type. Neat.
okay, when I added your code, I got this print out when I hit submit:
Array (  => Array (  => 42000 [SQLSTATE] => 42000  => 156 [code] => 156  => [Microsoft][SQL Server Native Client 10.0][SQL Server]Incorrect syntax near the keyword 'ORDER'. [message] => [Microsoft][SQL Server Native Client 10.0][SQL Server]Incorrect syntax near the keyword 'ORDER'. ) )
So I added '' around the Criteria variable:
$query = "SELECT * FROM MSAG WHERE $category LIKE %$criteria% ORDER BY StreetName ASC";
after that, if I just hit sumbit on the page, it will output ever record in the database, however, if I try and fill in the search critera I get this error:
Notice: Undefined variable: resource in C:Inetpubwwwrootmsagindex.php on line 44 Warning: sqlsrv_fetch_array() expects parameter 1 to be resource, null given in C:Inetpubwwwrootmsagindex.php on line 44
I also get the above error when The page first loads as well.
Is whatever $catagory a string datatype? If so, $criteria also has to be a string, so you need to block it into single quotations.
The notice is caused by being unable to construct the query since its blocked into an isset check on $_POST['search']. If you don't post the form, it will still attempt to execute the while, so when you first load the form it doesn't have a query to execute and triggers a notice that the variable $resource doesn't exist (and therefore cannot be fetched). Move the block with the fetch into the same block that queries.
I'd also pull this off: || isset($_POST['submit']). You shouldn't bother searching for a submit button; IE (at least olderish versions) refuse to accept that a form with just that button is valid if you hit the enter key instead of clicking. Therefore it will not consider it successful when passing to the server, so even if it is submitted it will not provide it to the server (in violation of W3 successful field standard). So you should simply check for the fields you are interested in.
I am sorry sir. I thank you for all your help so far, but I am officially lost now. I tried moving around the while loop with the fetch array and it still would not work right but I believe I probably did it wrong. Oh, and the 'Category' is a dropdown down menu and the criteria is the value to search the category with: