Solved

PHP SEARCH PROBLEM, Please Help

Posted on 2011-03-24
6
255 Views
Last Modified: 2012-05-11
Hi Experts,

I have this search function, It should allow the user to search by customer_name and contact_mobile.

It goes to my script but does not return any results. the while loop fails expecting a boolean value.

any Ideas??

code is attached. Modifications are welcomed..
<?php 
 if(isset($_POST['submit'])){ 
	  if(isset($_GET['go'])){ 
	  if(preg_match("/^[  a-zA-Z]+/", $_POST['searchterm'])){ 
	  $searchterm=$_POST['searchterm']; 
	  //connect  to the database 
	include 'myphp.php';
	  //-query  the database table 
	  $sql="SELECT contact_mobile, customer_name FROM customer WHERE customer_name LIKE '%" . $searchterm .  "%' OR mobile_number LIKE '%" . $searchterm ."%'"; 
	  //-run  the query against the mysql query function 
	  $result=mysql_query($sql); 
	   var_dump ($sql);
	  //-create  while loop and loop through result set 
	  while($row=mysql_fetch_array($result)){ 
	          $customer_name  =$row['customer_name']; 
	          $mobile_number =$row['contact_mobile']; 
	           var_dump ($result);
	 
	  //-display the result of the array 
	  echo "<ul>\n"; 
	  echo "<li>" . "<a  href=\"mobilenum.php?val=$customer_name\">"   .$customer_name . " " . $mobile_number .  "</a></li>\n"; 
	  echo "</ul>"; 
	  } 
	  } 
	  else{ 
	  echo  "<p>Please enter a search query</p>"; 
	  } 
	  } 
	  } 
	?>

Open in new window

0
Comment
Question by:NeoAshura
  • 3
  • 2
6 Comments
 
LVL 4

Assisted Solution

by:qasim_md
qasim_md earned 100 total points
ID: 35205872
Convert your mobile_number column to varchar::: code below :::

$sql="SELECT contact_mobile, customer_name FROM customer WHERE customer_name LIKE '%" . $searchterm .  "%' OR convert(varchar(20),mobile_number) LIKE '%" . $searchterm ."%'";

did that help ??? let me know ...
0
 
LVL 6

Author Comment

by:NeoAshura
ID: 35206327
it already is varchar11 set in the database.
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35206931
add this to the top:
error_reporting(E_ALL);
you may see why the query fails.
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 400 total points
ID: 35206937
And do it like this:

$result=mysql_query($sql) or die(mysql_error());
0
 
LVL 6

Author Comment

by:NeoAshura
ID: 35207087
i did that but removed it when i posted code the error i get is..

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in I:\EasyPHP-5.3.5.0\www\search.php on line 15

0
 
LVL 6

Author Comment

by:NeoAshura
ID: 35207114
$result=mysql_query($sql) or die(mysql_error()))

solved it, I wasnt calling correct table name. thanks .
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem with Simple PHP/mySQL Query 3 51
PHP Undefined Index in HTML Form? 2 31
Mysql sync between 3-4 mysql db 4 23
Calculating number of days between two dates 7 31
Creating and Managing Databases with phpMyAdmin in cPanel.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

932 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

8 Experts available now in Live!

Get 1:1 Help Now