mattyd Posted October 22, 2010 Share Posted October 22, 2010 I am seeking to learn more about the noted subject, how to use PHP to allow a user to enter search terms and search a database. I have experimented with this with little results save for errors. Please see code listed below: search.php <? //// filename = search.php <form method="post" action="result.php3"> <select name="metode" size="1"> <option value="row_name1">metode1</option> <option value="row_name2">metode2</option> </select> <input type="text" name="search" size="25"> <input type="submit" value="Begin Searching!!"> </form> ?> results.php //// filename = result.php3 <? $hostname = "mysql7.000webhost.com"; // Usually localhost. $username = "a4542527_root"; // If you have no username, leave this space empty. $password = "*******"; // The same applies here. $usertable = "people"; // This is the table you made. $dbName = "a4542527_test1"; // This is the main database you connect to. MYSQL_CONNECT($hostname, $username, $password) OR DIE("Unable to connect to database"); @mysql_select_db( "$dbName") or die( "Unable to select database"); ?> <? //error message (not found message) $XX = "No Record Found"; $query = mysql_query("SELECT * FROM $usertable WHERE $metode LIKE '%$search%' LIMIT 0, 30 "); while ($row = mysql_fetch_array($query)) { $variable1=$row["row_name1"]; $variable2=$row["row_name2"]; $variable3=$row["row_name3"]; print ("this is for $variable1, and this print the variable2 end so on..."); } //below this is the function for no record!! if (!$variable1) { print ("$XX"); } //end ?> Upon viewing search.php I receive the error message: Parse error: syntax error, unexpected '<' in /home/a4542527/public_html/search.php on line 3 I believe I may be missing something and am a bit lost. Thank-you in in advance for any help or suggestions. ~Matty Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted October 22, 2010 Share Posted October 22, 2010 You're using short <? open tags. Change the tags from <? To <?php tags and see if that helps. EDIT: that isn't what the problem is, but it won't hurt to do it. The problem is you're trying to echo within php tags, without actually issuing an echo . . . Quote Link to comment Share on other sites More sharing options...
mattyd Posted October 22, 2010 Author Share Posted October 22, 2010 You're using short <? open tags. Change the tags from <? To <?php tags and see if that help. Thank-you for your reply. I made the change as you suggested but I am still getting the same error message as before. Matty Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted October 22, 2010 Share Posted October 22, 2010 I jumped the gun. See my edit above . . . Quote Link to comment Share on other sites More sharing options...
mattyd Posted October 22, 2010 Author Share Posted October 22, 2010 I jumped the gun. See my edit above . . . In the file "result.php"? I am sort of confused (and not sure how to fix this). Thanks. ~Matty Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted October 22, 2010 Share Posted October 22, 2010 In search.php, you open the php tags, then have an html block in there without using 'echo'. If you simply remove the <?php tags around that block of html, it should fix that error. I didn't look over all of the code, so fixing that one may reveal other errors that need attention. Quote Link to comment Share on other sites More sharing options...
mattyd Posted October 22, 2010 Author Share Posted October 22, 2010 In search.php, you open the php tags, then have an html block in there without using 'echo'. If you simply remove the <?php tags around that block of html, it should fix that error. I didn't look over all of the code, so fixing that one may reveal other errors that need attention. Thank-you for all of your help. Matty Quote Link to comment Share on other sites More sharing options...
mattyd Posted October 22, 2010 Author Share Posted October 22, 2010 That error has been resolved. Now I am able to enter search data and perform a search. When attempting to do this, though, I receive the error message: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/a4542527/public_html/result.php on line 15 Note: I am using this code from a tutorial and did not write it myself so I am a bit stuck with some of it. I am attempting to connect to a pre-existing DB and search for data that already resides there. Here is the actual page: http://bluelinedown.netau.net/search.php Thanks, ~Matty Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted October 22, 2010 Share Posted October 22, 2010 Frankly, that tutorial is already teaching you some bad habits. Look this over and see my comments within the code. Let me know if you have any questions. <?php $hostname = "mysql7.000webhost.com"; // Usually localhost. $username = "a4542527_root"; // If you have no username, leave this space empty. $password = "*******"; // The same applies here. $usertable = "people"; // This is the table you made. $dbName = "a4542527_test1"; // This is the main database you connect to. MYSQL_CONNECT($hostname, $username, $password) OR DIE("Unable to connect to database"); mysql_select_db( "$dbName") or die( "Unable to select database"); //error message (not found message) $XX = "No Record Found"; // assign a value for the 'empty result set' message $query = "SELECT * FROM $usertable WHERE $metode LIKE '%$search%' LIMIT 0, 30"; // Query string, separated from the query execution if( !$result = mysql_query($query) ) { // check query execution success/failure echo '<br>Query string: ' . $query . '<br>Resulted in an error: ' . mysql_error() . '<br>'; // if execution fails, echo debugging info incl. error and query string } else { // if query succeeds if( mysql_num_rows($result) > 0 ) { // if result set is not empty, echo the result set while ($row = mysql_fetch_array($query)) { $variable1=$row["row_name1"]; $variable2=$row["row_name2"]; $variable3=$row["row_name3"]; print ("this is for $variable1, and this print the variable2 end so on..."); } } else { // else, if the result set is empty, echo the message for an empty result set. echo $XX; } } //end ?> Quote Link to comment Share on other sites More sharing options...
mattyd Posted October 22, 2010 Author Share Posted October 22, 2010 Frankly, that tutorial is already teaching you some bad habits. Look this over and see my comments within the code. Let me know if you have any questions. <?php $hostname = "mysql7.000webhost.com"; // Usually localhost. $username = "a4542527_root"; // If you have no username, leave this space empty. $password = "*******"; // The same applies here. $usertable = "people"; // This is the table you made. $dbName = "a4542527_test1"; // This is the main database you connect to. MYSQL_CONNECT($hostname, $username, $password) OR DIE("Unable to connect to database"); mysql_select_db( "$dbName") or die( "Unable to select database"); //error message (not found message) $XX = "No Record Found"; // assign a value for the 'empty result set' message $query = "SELECT * FROM $usertable WHERE $metode LIKE '%$search%' LIMIT 0, 30"; // Query string, separated from the query execution if( !$result = mysql_query($query) ) { // check query execution success/failure echo '<br>Query string: ' . $query . '<br>Resulted in an error: ' . mysql_error() . '<br>'; // if execution fails, echo debugging info incl. error and query string } else { // if query succeeds if( mysql_num_rows($result) > 0 ) { // if result set is not empty, echo the result set while ($row = mysql_fetch_array($query)) { $variable1=$row["row_name1"]; $variable2=$row["row_name2"]; $variable3=$row["row_name3"]; print ("this is for $variable1, and this print the variable2 end so on..."); } } else { // else, if the result set is empty, echo the message for an empty result set. echo $XX; } } //end ?> Hi. I looked this over and replaced my file with this code. I received the following result when running a search query: Query string: SELECT * FROM people WHERE LIKE '%%' LIMIT 0, 30 Resulted in an error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIKE '%%' LIMIT 0, 30' at line 1 I am not really sure what that means or how to address the issue. Page: http://bluelinedown.netau.net/search.php Thanks. ~Matty Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted October 22, 2010 Share Posted October 22, 2010 That means the $search and $metode variables are empty or undefined. Is there anything in the tutorial that looks like this? If not, add this to the results.php file, right after //error message (not found message) if( isset($_POST['search']) ) { $search = mysql_real_escape_string($_POST['search']); } if( isset($_POST['metode']) ) { $metode = mysql_real_escape_string($_POST['metode']); } Quote Link to comment Share on other sites More sharing options...
mattyd Posted October 22, 2010 Author Share Posted October 22, 2010 That means the $search and $metode variables are empty or undefined. Is there anything in the tutorial that looks like this? If not, add this to the results.php file, right after //error message (not found message) if( isset($_POST['search']) ) { $search = mysql_real_escape_string($_POST['search']); } if( isset($_POST['metode']) ) { $metode = mysql_real_escape_string($_POST['metode']); } I did as you suggested and am getting a different error now: Query string: SELECT * FROM people WHERE row_name2 LIKE '%kate%' LIMIT 0, 30 Resulted in an error: Unknown column 'row_name2' in 'where clause' Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted October 22, 2010 Share Posted October 22, 2010 You're supposed to substitute your actual database column names into those . . . Quote Link to comment 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.