Solved

query for two fields from one table in mysql

Posted on 2011-03-14
9
238 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 143

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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
LVL 143

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
 
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 41

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
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…

734 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