chrisC Posted February 18, 2009 Author Share Posted February 18, 2009 this looks to advanced now lol ... im doing it for a piece of work, and i have no idea what this is now Quote Link to comment https://forums.phpfreaks.com/topic/145756-php-search-for-friends-problem/page/2/#findComment-765355 Share on other sites More sharing options...
chrisC Posted February 18, 2009 Author Share Posted February 18, 2009 is there no easier way around this? e.g. a new script completly Quote Link to comment https://forums.phpfreaks.com/topic/145756-php-search-for-friends-problem/page/2/#findComment-765357 Share on other sites More sharing options...
shadiadiph Posted February 18, 2009 Share Posted February 18, 2009 mm no not really the second file i asked you to amke i just edited it you have to save it as connection.php your table is called user correct Quote Link to comment https://forums.phpfreaks.com/topic/145756-php-search-for-friends-problem/page/2/#findComment-765361 Share on other sites More sharing options...
chrisC Posted February 18, 2009 Author Share Posted February 18, 2009 im lost now, so from above im really not sure what i need to create! Â Â Quote Link to comment https://forums.phpfreaks.com/topic/145756-php-search-for-friends-problem/page/2/#findComment-765364 Share on other sites More sharing options...
shadiadiph Posted February 18, 2009 Share Posted February 18, 2009 are your database username and passwprd both "ccheckley" just asking because i could fill out the two files for you   Quote Link to comment https://forums.phpfreaks.com/topic/145756-php-search-for-friends-problem/page/2/#findComment-765368 Share on other sites More sharing options...
chrisC Posted February 18, 2009 Author Share Posted February 18, 2009 password 4aPRaJew ccheckley username  much appreciated  Quote Link to comment https://forums.phpfreaks.com/topic/145756-php-search-for-friends-problem/page/2/#findComment-765376 Share on other sites More sharing options...
shadiadiph Posted February 18, 2009 Share Posted February 18, 2009 whats the actual database name Quote Link to comment https://forums.phpfreaks.com/topic/145756-php-search-for-friends-problem/page/2/#findComment-765390 Share on other sites More sharing options...
chrisC Posted February 18, 2009 Author Share Posted February 18, 2009 ccheckley database name  table name users   Quote Link to comment https://forums.phpfreaks.com/topic/145756-php-search-for-friends-problem/page/2/#findComment-765392 Share on other sites More sharing options...
shadiadiph Posted February 18, 2009 Share Posted February 18, 2009 do you have an email address at the website your website is on i will set it to send any error messages to your email Quote Link to comment https://forums.phpfreaks.com/topic/145756-php-search-for-friends-problem/page/2/#findComment-765395 Share on other sites More sharing options...
chrisC Posted February 18, 2009 Author Share Posted February 18, 2009 no i dont need anything like that its basically needed to be kept as simple as possible its a piece of work at uni lol using a database from uni! Quote Link to comment https://forums.phpfreaks.com/topic/145756-php-search-for-friends-problem/page/2/#findComment-765398 Share on other sites More sharing options...
chrisC Posted February 18, 2009 Author Share Posted February 18, 2009 that isnt a problem right??? Quote Link to comment https://forums.phpfreaks.com/topic/145756-php-search-for-friends-problem/page/2/#findComment-765410 Share on other sites More sharing options...
premiso Posted February 18, 2009 Share Posted February 18, 2009 Well this thread has really gone to crap. Â Alright I think what you are looking for is the "like" keyword in MySQL Â <?php session_start(); $b = $_SESSION["gatekeeper"]; $a = $_POST["name"]; $conn = mysql_connect("localhost", "ccheckley", "4aPRaJew"); mysql_select_db("ccheckley"); $result = mysql_query("SELECT * FROM users WHERE name LIKE '%{$a}%'"); if (mysql_num_rows($result) > 1) { while ($row = mysql_fetch_assoc($result)) { Â echo " name: {$row['name']} <br/>"; Â echo " username: {$row['username']} <br/>"; Â echo " birthday: {$row['birthday']} <br/>"; Â echo " interests: {$row['interests']} <br/>"; Â echo " <a href='addfriend.php?username={$row['username']}'>Make this user your friend</a>"; } }else { echo "Unable to locate any username's whose name is like {$a}"; } // not needed mysql_close($conn); ?> Â Give that code a try and see what comes of it. As a side note for you to look at later on I would look into utilizing mysql_real_escape_string for preventing SQL Injections. Â See if this gives you the desired results. Quote Link to comment https://forums.phpfreaks.com/topic/145756-php-search-for-friends-problem/page/2/#findComment-765417 Share on other sites More sharing options...
chrisC Posted February 18, 2009 Author Share Posted February 18, 2009 Wow, ok right when I did a search on my website it came up with all the people signed up, only problem is It always does that? ... is there no way of doing this so that when a keyword is typed e.g. search for all friends on the database whos interest are football  thanks Quote Link to comment https://forums.phpfreaks.com/topic/145756-php-search-for-friends-problem/page/2/#findComment-765424 Share on other sites More sharing options...
premiso Posted February 18, 2009 Share Posted February 18, 2009 Wow, ok right when I did a search on my website it came up with all the people signed up, only problem is It always does that? ... is there no way of doing this so that when a keyword is typed e.g. search for all friends on the database whos interest are football  thanks  There is, you just have to setup your code that way.  The following is untested, but "should" work pending any errors on my part: <?php session_start(); $b = $_SESSION["gatekeeper"]; $find['name'] = isset($_POST['name'])?$_POST['name']:null; $find['interest'] = isset($_POST['interest'])?$_POST['interest']:null; $search = array(); foreach ($find as $key => $val) { if (!is_null($val)) { $search[] = "`{$key}` LIKE '%{$val}%'"; } } if (count($search) > 0) $search = implode(" OR ", $search); else $search = ""; // displays everything, change this if you do not want that functionality. $conn = mysql_connect("localhost", "ccheckley", "4aPRaJew"); mysql_select_db("ccheckley"); $result = mysql_query("SELECT * FROM users WHERE {$search}"); if (mysql_num_rows($result) > 1) { while ($row = mysql_fetch_assoc($result)) {  echo " name: {$row['name']} <br/>";  echo " username: {$row['username']} <br/>";  echo " birthday: {$row['birthday']} <br/>";  echo " interests: {$row['interests']} <br/>";  echo " <a href='addfriend.php?username={$row['username']}'>Make this user your friend</a>"; } }else { echo "Unable to locate any username's whose name is like {$a}"; } ?>  This uses isset implode array and foreach to determine and create the search string. Quote Link to comment https://forums.phpfreaks.com/topic/145756-php-search-for-friends-problem/page/2/#findComment-765428 Share on other sites More sharing options...
chrisC Posted February 18, 2009 Author Share Posted February 18, 2009 it works much better, although You can type a load of rubbish and it finds everyone on the datbase and shows it Quote Link to comment https://forums.phpfreaks.com/topic/145756-php-search-for-friends-problem/page/2/#findComment-765429 Share on other sites More sharing options...
chrisC Posted February 18, 2009 Author Share Posted February 18, 2009 ignoring the post above  after inserting that code i got 1 error  if (mysql_num_rows($result) > 1) {  Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/CheckleyChristopher/public_html/Topherbox/search.php on line 26 Unable to locate any username's whose name is like Quote Link to comment https://forums.phpfreaks.com/topic/145756-php-search-for-friends-problem/page/2/#findComment-765432 Share on other sites More sharing options...
shadiadiph Posted February 18, 2009 Share Posted February 18, 2009 ok looks like your problem is solved  i did this so you can loose all that connection stuff on your page  up to you what you do with it  save as config.php  <?php //local server error_reporting(7); session_start(); @header("Cache-control: private"); $host="localhost"; $username="ccheckley"; $password="4aPRaJew"; $db_name="ccheckley"; mysql_connect("$host", "$username", "$password")or die("cannot connect to server"); mysql_select_db("$db_name")or die("cannot select db"); ?>  and link by using  <?php ini_set ("display_errors", "1"); error_reporting(E_ALL); session_start(); include("config.php");  in the head you can delete all the database connection stuff then like premiso said what he has done looks fine for football i guess you could try  $result = mysql_query("SELECT * FROM users WHERE name LIKE '%{$a}%' OR interest LIKE '%{$a}%' ");   Quote Link to comment https://forums.phpfreaks.com/topic/145756-php-search-for-friends-problem/page/2/#findComment-765437 Share on other sites More sharing options...
premiso Posted February 18, 2009 Share Posted February 18, 2009 Change this portion, it means there was an error in the SQL statement: Â $result = mysql_query("SELECT * FROM users WHERE {$search}") or DIE(mysql_error() . "<br /> Using search string {$search}"); Â And see what that tells ya. As for it returning rubbish, that is on you to filter out user input and verifying certain input etc. I have provided a rough example, not knowing how your code looks. Â Also change this since your DB column name should be "interests" Â $find['interests'] = isset($_POST['interests'])?$_POST['interests']:null; Quote Link to comment https://forums.phpfreaks.com/topic/145756-php-search-for-friends-problem/page/2/#findComment-765438 Share on other sites More sharing options...
chrisC Posted February 18, 2009 Author Share Posted February 18, 2009 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 '' at line 1 Using search string Quote Link to comment https://forums.phpfreaks.com/topic/145756-php-search-for-friends-problem/page/2/#findComment-765440 Share on other sites More sharing options...
shadiadiph Posted February 18, 2009 Share Posted February 18, 2009 <a href="addfriend.php?username='.$username.'">Make this user your friend</a>'; Quote Link to comment https://forums.phpfreaks.com/topic/145756-php-search-for-friends-problem/page/2/#findComment-765442 Share on other sites More sharing options...
premiso Posted February 18, 2009 Share Posted February 18, 2009 Simple fix.  <?php session_start(); $b = $_SESSION["gatekeeper"]; $find['name'] = isset($_POST['name'])?$_POST['name']:null; $find['interests'] = isset($_POST['interests'])?$_POST['interests']:null; $search = array(); foreach ($find as $key => $val) {  if (!is_null($val)) {    $search[] = "`{$key}` LIKE '%{$val}%'";  } } if (count($search) > 0)  $search = " WHERE " . implode(" OR ", $search); // modified here else  $search = ""; // displays everything, change this if you do not want that functionality. $conn = mysql_connect("localhost", "ccheckley", "4aPRaJew"); mysql_select_db("ccheckley"); $result = mysql_query("SELECT * FROM users {$search}"); // and here if (mysql_num_rows($result) > 1) {  while ($row = mysql_fetch_assoc($result)) {    echo " name: {$row['name']} <br/>";    echo " username: {$row['username']} <br/>";    echo " birthday: {$row['birthday']} <br/>";    echo " interests: {$row['interests']} <br/>";    echo " <a href='addfriend.php?username={$row['username']}'>Make this user your friend</a>";  } }else {  echo "The search parameters you provided returned no results."; } ?>  Basically you left all search parameters empty. The above should return all rows in the table if no search parameters are provided. Quote Link to comment https://forums.phpfreaks.com/topic/145756-php-search-for-friends-problem/page/2/#findComment-765443 Share on other sites More sharing options...
chrisC Posted February 18, 2009 Author Share Posted February 18, 2009 yep! ... i just used the above code and all when typing anything into the search bar i recieve all teh details of everyone on the datbase Quote Link to comment https://forums.phpfreaks.com/topic/145756-php-search-for-friends-problem/page/2/#findComment-765445 Share on other sites More sharing options...
premiso Posted February 18, 2009 Share Posted February 18, 2009 yep! ... i just used the above code and all when typing anything into the search bar i recieve all teh details of everyone on the datbase  Now on your form, if you have an input field for "interests" and "name" filling those out and posting should automatically populate the search field and return those limited results. Quote Link to comment https://forums.phpfreaks.com/topic/145756-php-search-for-friends-problem/page/2/#findComment-765447 Share on other sites More sharing options...
chrisC Posted February 18, 2009 Author Share Posted February 18, 2009 ok well my form is liek this though   <form action="search.php" method="post"> <br /> Enter Search:<br /> <input name="friendsearch" type=""text" size="40"/> <br /> <input type="submit" name="submit" value="search"/> </form>   is this a problem? Quote Link to comment https://forums.phpfreaks.com/topic/145756-php-search-for-friends-problem/page/2/#findComment-765450 Share on other sites More sharing options...
chrisC Posted February 18, 2009 Author Share Posted February 18, 2009 do i need to change my form then?? Quote Link to comment https://forums.phpfreaks.com/topic/145756-php-search-for-friends-problem/page/2/#findComment-765453 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.