Jump to content


Select From Database


6 replies to this topic

#1 JoeVincetti

    Young Padawan

  • Members
  • Pip
  • 82 posts

Posted 06 October 2005 - 09:17 PM

Is there a way to get information from a table in a database where value_a = "" OR value_b = "". I know how to select from a database where value_a = "" AND value_b = "".

I am trying to search from a table WHERE user LIKE '%$search%'. I would like to add to my search by searching also for the users email. (WHERE email '%$search%')
mysql_connect($server,$dbuser,$dbpassword);
@mysql_select_db($database) or die( "Unable to select database");
$result = mysql_query("SELECT * FROM users WHERE user LIKE '%$search%'") or die("Could not find any items with that name."); 
$num=mysql_numrows($result);

Edited by JoeVincetti, 06 October 2005 - 09:23 PM.


#2 MillerTime

    Young Padawan

  • Members
  • Pip
  • 69 posts

Posted 06 October 2005 - 10:07 PM

$result = mysql_query("SELECT * FROM users WHERE user LIKE '%$search%' OR WHERE email LIKE '%$search%'") or die("Could not find any items with that name.");

like that?

Edited by MillerTime, 06 October 2005 - 10:07 PM.


#3 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 07 October 2005 - 12:00 AM

The simple way to find this out would have been to try it your self, or going and checking the mysql manual (a link to which is provided in a sticky in this forum).

And miller's code won't work, it'd be more like:
mysql_query("SELECT * FROM users WHERE email LIKE '%$search%' OR foo = 'bar'");


#4 MillerTime

    Young Padawan

  • Members
  • Pip
  • 69 posts

Posted 07 October 2005 - 12:58 AM

oh yeah..i had "AND" thee first and i forgot to take out the "WHERE"

#5 JoeVincetti

    Young Padawan

  • Members
  • Pip
  • 82 posts

Posted 07 October 2005 - 04:19 PM

I tried to follow what you told me:

mysql_connect($server,$dbuser,$dbpassword);
@mysql_select_db($database) or die( "Unable to select database");
$result = mysql_query("SELECT * FROM users WHERE user LIKE '%$search%' OR WHERE first LIKE '%$search%' OR WHERE last LIKE '%$search%' OR WHERE email LIKE '%$search%'") or die("Could not find any items with that name."); 
$num=mysql_numrows($result);

When I ran the script i got the or die() error. Does anyone see the problem?

I am sure that $search exists and that what I am searching for is in the table "users".

#6 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 07 October 2005 - 08:15 PM

rc69, on Oct 6 2005, 10:00 PM, said:

And miller's code won't work, it'd be more like:
mysql_query("SELECT * FROM users WHERE email LIKE '%$search%' OR foo = 'bar'");
3 things:
1. Learn to pay more attention to what's actually wrote down, it may help you with life.

2. If you're going to add "or die()" after a mysql_query(), make it something usefull. Go look at 90% of the scripts out there that have something like that, they'll never have "die('random comment')", it'll most likely be "or die(mysql_error())". It'd help you and everybody else if you did that from now on.

3. Pay more attention to what has been wrote down, and look more closely at the code I gave. You have to many "WHERE"'s in your code. You can also check the mysql manual for more info on the SELECT statement and other misc stuff.
$result = mysql_query("SELECT * FROM users WHERE user LIKE '%$search%' OR first LIKE '%$search%' OR last LIKE '%$search%' OR email LIKE '%$search%'") or die(mysql_error()); 
$num=mysql_numrows($result);
if(!$num){
  die("Could not find any items with that name.");
}
--------
Sorry if i sound a bit aggitated, it's been a long day...

#7 JoeVincetti

    Young Padawan

  • Members
  • Pip
  • 82 posts

Posted 07 October 2005 - 09:49 PM

Thank you so much. The link you gave me will help me solve a lot of problems on my own. Sorry about that. It has been a long day.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users