smti Posted July 26, 2009 Share Posted July 26, 2009 Hi Folks, I am having a problem with a search function I am writing. Here is the situation: I have text box named "search_for" where users can enter the term they want to find in a particular record. Users must then select from a drop down what field in the database they would like to check the term for. Users can select the following fields in the database to search: 1. ticketid 2. name 3. username 4. building Searching for a particular ticket ID appears to work fine, however, searching for anything else in any one of the other database fields produces an error. For example, if I search for "Columbia" and select building from the drop down I get the error: Error in SQL query: ERROR: column "Columbia" does not exist. Here is my search query code (and code to show result: include ("../../../../includes/connection.inc.php"); // execute query $sql = "SELECT ticketid, opendate, name, status FROM tickets where $in_what=$search_for order by ticketid asc"; $result = pg_query($dbh, $sql); if (!$result) { die("Error in SQL query: " . pg_last_error()); } // iterate over result set // print each row //while ($row = pg_fetch_array($result)) { $count=0; echo '<tbody>'; // keeps getting the next row until there are no more to get while ($row = pg_fetch_array($result)) { // Print out the contents of each row into a table $classStr = $count++ % 2 ? '' : "class='alt'"; echo "<tr $classStr> <td> $row[0]</td> <td> $row[1]</td> <td> $row[2]</td> <td> $row[3]</td>"; //Grab ID and put in variable. $id=$row['ticketid']; echo "<td><a href='viewticket.php?id=$id'><img src='images/icons/view.png' alt='View Record'></a></td> </tr>"; } echo '</tbody>'; // close connection pg_close($dbh); } Why can I locate records using the ticketid field, but not the others? Any help would be greatly appreciated! Thank You, smti Quote Link to comment https://forums.phpfreaks.com/topic/167515-searching-problem-with-php-postgres/ Share on other sites More sharing options...
btherl Posted July 27, 2009 Share Posted July 27, 2009 Can you add "echo $sql;" and show me the output please? I suspect you need single quotes around the $search_for, otherwise it'll be interpreted as a column name. You won't get the same problem for numbers because column names can't be numbers. Quote Link to comment https://forums.phpfreaks.com/topic/167515-searching-problem-with-php-postgres/#findComment-883858 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.