barryflood22 Posted April 11, 2007 Share Posted April 11, 2007 This code was provided to me by Anthylon for which I am very greatful, but i cant get it to work, can someone help? search.php ------------- <HTML> <HEAD></HEAD> <BODY> <form name="form1" method="post" action="searchresults.php"> <input name="txt_search" type="text" id="txt_search" /> <label> <input name="cmd_search" type="submit" id="cmd_search" value="Search"> </label> </form> </BODY> </HTML> searchresults.php ------------------ <HTML> <HEAD></HEAD> <BODY> <p><a href="../index.php">< back</a></p> <p> <? if($_POST['txt_search']) { $host = 'localhost'; $db='customer'; $uname='root'; $pass=''; $cnn = mysql_connect($host, $uname, $pass); if (!$cnn) { echo "Unable to connect to DB: " . mysql_error(); exit; } if (!mysql_select_db($db)) { echo "Unable to select $db: " . mysql_error(); exit; } $sql = "SELECT * FROM customer WHERE name= '" . $_POST['txt_search'] ."'"; $result = mysql_query($sql); if (!$result) { echo "Could not successfully run query ($sql) from DB: " . mysql_error(); exit; } if (mysql_num_rows($result) == 0) { echo "No records found"; exit; } echo 'Results of your search: <br> '; $i = 1; while ($row = mysql_fetch_assoc($result)) { echo "$i. ${row['name']}<br>"; $i++; } mysql_free_result($result); } ?> </p> </BODY> </HTML> when i run the script i get the following displayed: '; $i = 1; while ($row = mysql_fetch_assoc($result)) { echo "$i. ${row['name']} "; $i++; } mysql_free_result($result); } ?> Link to comment https://forums.phpfreaks.com/topic/46586-solved-sql-script-search/ Share on other sites More sharing options...
komquat Posted April 11, 2007 Share Posted April 11, 2007 Instead of echo 'Results of your search: <br> '; Try echo "Results of your search: <br> "; Link to comment https://forums.phpfreaks.com/topic/46586-solved-sql-script-search/#findComment-226812 Share on other sites More sharing options...
boo_lolly Posted April 11, 2007 Share Posted April 11, 2007 this is what you want: <?php //searchresults.php if($_POST['txt_search']) { $host = 'localhost'; $db = 'customer'; $uname = 'root'; $pass = 'xxx'; $cnn = mysql_connect($host, $uname, $pass); if (!$cnn) { echo "Unable to connect to DB: " . mysql_error(); exit; } if (!mysql_select_db($db)) { echo "Unable to select $db: " . mysql_error(); exit; } $sql = "SELECT * FROM customer WHERE name LIKE '%" . $_POST['txt_search'] ."%'"; $result = mysql_query($sql); if (!$result) { echo "Could not successfully run query ($sql) from DB: " . mysql_error(); exit; } if (mysql_num_rows($result) == 0) { echo "No records found"; exit; } echo 'Results of your search: <br> '; $i = 1; while ($row = mysql_fetch_array($result)) { echo $i . ${row['name'] ."<br>\n"; $i++; } mysql_free_result($result); } ?> Link to comment https://forums.phpfreaks.com/topic/46586-solved-sql-script-search/#findComment-226825 Share on other sites More sharing options...
barryflood22 Posted April 11, 2007 Author Share Posted April 11, 2007 im now getting a parse error wit the script u gave me Parse error: syntax error, unexpected '[' in C:\wamp\www\hoff_godd\Search_Records\searchresults.php on line 43 Link to comment https://forums.phpfreaks.com/topic/46586-solved-sql-script-search/#findComment-226853 Share on other sites More sharing options...
barryflood22 Posted April 11, 2007 Author Share Posted April 11, 2007 name in my database is a varchar(50) Link to comment https://forums.phpfreaks.com/topic/46586-solved-sql-script-search/#findComment-226855 Share on other sites More sharing options...
boo_lolly Posted April 11, 2007 Share Posted April 11, 2007 change this: echo 'Results of your search: <br> '; $i = 1; while ($row = mysql_fetch_array($result)) { echo $i . ${row['name'] ."<br>\n"; $i++; } to this: echo 'Results of your search: <br> '; $i = 1; while ($row = mysql_fetch_array($result)) { echo $i ." ". $row['name'] ."<br />\n"; $i++; } Link to comment https://forums.phpfreaks.com/topic/46586-solved-sql-script-search/#findComment-226858 Share on other sites More sharing options...
barryflood22 Posted April 11, 2007 Author Share Posted April 11, 2007 BUCKIN ROCK ON!!!! :-D Link to comment https://forums.phpfreaks.com/topic/46586-solved-sql-script-search/#findComment-226859 Share on other sites More sharing options...
barryflood22 Posted April 11, 2007 Author Share Posted April 11, 2007 that worked, but how would i get it to display other information belonging to that record? the extra fields are - address postcode TelephoneNumber EmailAddress Type Appointment id Link to comment https://forums.phpfreaks.com/topic/46586-solved-sql-script-search/#findComment-226864 Share on other sites More sharing options...
per1os Posted April 11, 2007 Share Posted April 11, 2007 echo 'Results of your search: <br> '; $i = 1; while ($row = mysql_fetch_array($result)) { echo $i ." ". $row['name'] ."<br />\n"; echo $i ." ". $row['address'] ."<br />\n"; $i++; } And just follow that. Link to comment https://forums.phpfreaks.com/topic/46586-solved-sql-script-search/#findComment-226874 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.