Solved

query for two fields from one table in mysql

Posted on 2011-03-14
9
232 Views
Last Modified: 2012-06-27
hi,
iam trying to search for a word from two fields in one table . for example :

topic_title

topic_txt

i want search for "camel" in both fields .How can i do that?.

i tryed this one but it didn't work...
SELECT topic_title & ' ' & topic_txt as fulltxt FROM topic where fulltxt LIKE '$keyword'

Open in new window

0
Comment
Question by:eCamel
9 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35130105
in mysql, to concat, use concat:

SELECT concat(topic_title , ' ' , topic_txt ) as fulltxt FROM topic where fulltxt LIKE '$keyword'



0
 
LVL 2

Expert Comment

by:sihar86
ID: 35130141
use wildcard character

SELECT topic_title, topic_txt FROM topic where topic_title LIKE  '%$keyword%'  or  topic_txt  LIKE  '%$keyword%'

Open in new window

0
 

Author Comment

by:eCamel
ID: 35130152
thanx for your quick reply

i tryed your code but it gave me this in php

Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in C:\AppServ\www\eharaj\inc\search.php on line 26

Warning: Division by zero in C:\AppServ\www\eharaj\inc\class.pager.php on line 28

Open in new window

0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35130164
indeed, you cannot reuse the alias in the WHERE clause
SELECT concat(topic_title , ' ' , topic_txt ) as fulltxt 
  FROM topic 
 where concat(topic_title , ' ' , topic_txt ) LIKE '$keyword'

Open in new window

0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 500 total points
ID: 35130184
You need to post the PHP code just before line 26 of C:\AppServ\www\eharaj\inc\search.php

It SHOULD look something like this

$rs = mysql_query("SELECT topic_title, topic_txt FROM topic where topic_title LIKE  '%$keyword%'  or  topic_txt  LIKE  '%$keyword%' ");

....

if ( mysql_num_rows( $rs ) ...... etc )

The $rs should be what came back from the query running successfully. YOu can add an extra check like so

if ( ! $rs )
     echo "Query failed - " . mysql_error();
else {
     if ( mysql_num_rows( $rs ) ...... etc )
      ..... more code
}
0
 

Author Comment

by:eCamel
ID: 35130213
i tryed the second answer and also gave me the same error
0
 

Author Comment

by:eCamel
ID: 35130257
ok i think i need to post my whole code here it is >> can you figure out what is wrong??

$num1 = mysql_query("SELECT topic_title, topic_txt FROM topic where topic_title LIKE  '%$keyword%'  or  topic_txt  LIKE  '%$keyword%'");
$max=mysql_numrows($num1);
$pager = new pager( 
    $max ,                     /*see above*/
    $number ,                         /*how many records to display at one time*/
    @$_GET['_p']  /*this is the current page no carried via _GET*/
    ) ;
$pager->set_range(10);
//end pager//
$result = mysql_query("SELECT topic_title, topic_txt FROM topic where topic_title LIKE  '%$keyword%'  or  topic_txt  LIKE  '%$keyword%' ORDER BY mthbt,lastupdate DESC LIMIT ".$pager->get_limit());
$num=mysql_numrows($result);
while($r=mysql_fetch_array($result))
{	
$tid=$r["id"];
$themsg .=get_topic($tid);
}

Open in new window

0
 
LVL 40

Expert Comment

by:Sharath
ID: 35131705
try this.
$num1 = mysql_query("SELECT * FROM (SELECT concat(topic_title, ' ',topic_txt) as fulltext FROM topic) as t1 where fulltext LIKE  '%$keyword%'");

Open in new window

0
 

Author Closing Comment

by:eCamel
ID: 35131761
that was the right answer thanks ....

thank you all.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now