Jump to content

Recommended Posts

  • Replies 53
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

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.

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

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.

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

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}%' ");

 

 

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;

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.

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.

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.