Solved

query for two fields from one table in mysql

Posted on 2011-03-14
9
234 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

867 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

22 Experts available now in Live!

Get 1:1 Help Now