Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

search any text in data

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

search any text in data

Postby maideen » Tue Jun 18, 2013 6:57 am

Hi I have one problem. I need search any string data.
Below is my code. When I click, no result, I am using MSSQL and PDO

index.php

Code: Select all
<?php  include_once '../templete/header.php'; ?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Search</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form action="search.php" method="get" >
<table border="0" cellpadding="0" cellspacing="0">
<tr>
    <td><input type="text" name="query" id="text"  />&nbsp;</td>
    <td><input type="submit" name="submit" id="search" value="Search" /></td>
</form>
</body>
</html>


search.php

Code: Select all
<?php
 include_once '../templete/header.php';
 include_once '../inc/connection.inc.php';
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Search</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table>   
<tr>
  <td>Name</td>
  <td>Address</td>
  </tr>   
<?php
    $query = $_GET['query'];
   
    $stmt = $dbh->prepare("SELECT * FROM CompanyInfo WHERE (Name LIKE '%".$query."%') OR (Address LIKE '%".$query."%')");
    $stmt->setFetchMode(PDO::FETCH_ASSOC);
    //$stmt->execute();
    if(isset($stmt))
        {
            while($row = $stmt->fetch()); ?>
              <tr>
              <td><?php echo $row['Name'];?></td>
              <td><?php echo $row['Address'];?></td>
              </tr>
        <?php
        }
?>
</table>   
</body>
</html>


when I click button no result as well as no error

Pls help me

Maideen
maideen
New php-forum User
New php-forum User
 
Posts: 26
Joined: Mon Mar 07, 2011 11:38 pm

Re: search any text in data

Postby pbs » Tue Jun 18, 2013 7:58 pm

use "OR" statement in single bracket, check the below code

Code: Select all
$stmt = $dbh->prepare("SELECT * FROM CompanyInfo WHERE (Name LIKE '%".$query."%' OR Address LIKE '%".$query."%')");
pbs
New php-forum User
New php-forum User
 
Posts: 39
Joined: Sat May 11, 2013 1:40 am
Location: Nashik, India

Re: search any text in data

Postby maideen » Tue Jun 18, 2013 8:07 pm

hi

I have changed according to your suggestion, but still I could not get result. Nothing appeared in page

Pls

Maideen
maideen
New php-forum User
New php-forum User
 
Posts: 26
Joined: Mon Mar 07, 2011 11:38 pm

Re: search any text in data

Postby pbs » Tue Jun 18, 2013 8:31 pm

you have commented below statement, please check

Code: Select all
$stmt->execute();
pbs
New php-forum User
New php-forum User
 
Posts: 39
Joined: Sat May 11, 2013 1:40 am
Location: Nashik, India

Re: search any text in data

Postby maideen » Tue Jun 18, 2013 9:35 pm

hi

Thank you

I have checked too

Nothing happend

Maideen
maideen
New php-forum User
New php-forum User
 
Posts: 26
Joined: Mon Mar 07, 2011 11:38 pm

Re: search any text in data

Postby pbs » Tue Jun 18, 2013 9:45 pm

enable error reporting to all
also echo the query and execute it in phpmyadmin
pbs
New php-forum User
New php-forum User
 
Posts: 39
Joined: Sat May 11, 2013 1:40 am
Location: Nashik, India

Re: search any text in data

Postby maideen » Tue Jun 18, 2013 9:59 pm

Hi
Thank you all of you to give the suggestion.

The problem was in search.php
Here is the code which is working fine

Code: Select all
<?php
    $query = $_GET['query'];
    echo $query;
    $stmt = $dbh->query("SELECT * FROM CompanyInfo WHERE (Name LIKE '%".$query."%' OR Address LIKE '%".$query."%')");
    $stmt->setFetchMode(PDO::FETCH_ASSOC);
 
    if(isset($stmt))
        {
            while($row = $stmt->fetch()){ ?>
              <tr>
              <td><?php echo $row['Name'];?></td>
              <td><?php echo $row['Address'];?></td>
              </tr>
        <?php
       
        echo $row['Name']; 
     
        }
        }
?>


Thank you all of you once again

Maideen
maideen
New php-forum User
New php-forum User
 
Posts: 26
Joined: Mon Mar 07, 2011 11:38 pm


Return to PHP coding => General

Who is online

Users browsing this forum: Google Feedfetcher and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron