Jump to content

Recommended Posts

i have the following code, but i want to change it so it searches and lists anything that is LIKE the search for example:

if i searched for bat it would bring up all names with bat in it.

[code]<?php

if ($searched == 'yes')
      {
      $hostname="localhost";
      $username="twottk_Stefan";
      $password="364988979a";
      $dbname="twottk_twottk";
      $usertable="accounts";
     
      $link = mysql_connect($hostname, $username, $password);
     
           
      $query2 = "SELECT char_name, char_id FROM accounts WHERE char_id LIKE '$search' OR char_name LIKE '$search'";
      $result2= mysql_query ($query2);
      $num_rows=mysql_num_rows($result2);

      $row=mysql_fetch_array($result2);
      $search_char = $row['char_name'];
      $search_char_id = $row['char_id'];
     
      echo "$search_char ($search_char_id)";
     
     
      }

?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/35799-listing-search-results/
Share on other sites

Change your query to this:

[code]$query2 = "SELECT char_name, char_id FROM accounts WHERE char_id LIKE '%$search%' OR char_name LIKE '%$search%'";[/code]

Notice the percent (%) signs in there.  They're wild cards for multiple characters.

Regards
Huggie
OK, well then either the query, or the data is incorrect.

The query I've given you will work even if you only put a single character in.  What happens when you run the query in phpMyAdmin?  Does it return three rows?  If it does, then maybe you can post all of your query code here?

Regards
Huggie
[code]<?php

if ($searched == 'yes')
      {
      $hostname="localhost";
      $username="<username>";
      $password="<password>";
      $dbname="<db_name>";
      $usertable="accounts";
     
      $link = mysql_connect($hostname, $username, $password);
     
           
      $query2 = "SELECT char_name, char_id FROM accounts WHERE char_id LIKE '%$search%' OR char_name LIKE '%$search%'";
      $result2= mysql_query ($query2);
      $num_rows=mysql_num_rows($result2);     
      $row=mysql_fetch_array($result2);
     
      while($row = mysql_fetch_array($result2, MYSQL_ASSOC))
      {
     
      $search_char = $row['char_name'];
      $search_char_id = $row['char_id'];
     
     
      echo "$search_char ($search_char_id)<br>";
      }
     
     
     
      }

?>[/code]

that is all the code for the search and echo.
In that case you're data must be incorrect, the code should pull all members that have an 's' in their name so long as you put 's' into $search.

Also, as a side note, I'm assuming that $search is coming from a form or URL parameter of some kind, if it is then you should really be using the global name for it, something like $_GET['search'] or $_POST['search'] as opposed to relying on 'Register Globals' being switched on.

Check that your query returns the right result in phpMyAdmin before continuing.

Regards
Huggie
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.