473,396 Members | 1,963 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

New to PHP - Search Question

Help..

Just learning php/mysql from a book and have put together a test page
as below.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<title>Sample Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"
/>
</head>

<body>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
<label>Please enter Town:
<input type="text" name="town" /></label>
<input type="submit" value="GO" />
</form>

<p>Search Results for: <?php echo $_GET['town']; ?></p>

<?php

$connection = mysql_connect('localhost', '????????', '????????');
$db = mysql_select_db("test", $connection);
$result = @mysql_query ('SELECT name FROM site01_details WHERE town
LIKE Bury');
while ($row = mysql_fetch_array($result))
{ echo '<p>' . $row['name'] . '</p>'; }

?>
</body>

</html>

This is published at www.freeweekends.co.uk

The database has fields for name (name of a business) and town (town in
which the business is in).
I firstly wanted to create a list for the businesses in the town 'Bury'
but this fails.

Secondly I wanted to pass the town search string entered in the form to
replace 'Bury'.

Sorry but I am totally new to this and just taking my first steps, so
the fault is probably going to be an obvious one.

P.s. the search is OK if I look for all names or towns so the database
connection and naming is correct.

Many thanks

Alec

Aug 30 '05 #1
5 1342
This seems that there is nothing in the Database ?!
try other mysql_query like this:

$sql = "Select name from site01_details where town like '%Bury%'";
$result = mysql_query($sql) or die(mysql_error(mysql_errno));

Hope this helps ..
Wolfgang ...

Alec wrote:
Help..

Just learning php/mysql from a book and have put together a test page
as below.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<title>Sample Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"
/>
</head>

<body>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
<label>Please enter Town:
<input type="text" name="town" /></label>
<input type="submit" value="GO" />
</form>

<p>Search Results for: <?php echo $_GET['town']; ?></p>

<?php

$connection = mysql_connect('localhost', '????????', '????????');
$db = mysql_select_db("test", $connection);
$result = @mysql_query ('SELECT name FROM site01_details WHERE town
LIKE Bury');
while ($row = mysql_fetch_array($result))
{ echo '<p>' . $row['name'] . '</p>'; }

?>
</body>

</html>

This is published at www.freeweekends.co.uk

The database has fields for name (name of a business) and town (town in
which the business is in).
I firstly wanted to create a list for the businesses in the town 'Bury'
but this fails.

Secondly I wanted to pass the town search string entered in the form to
replace 'Bury'.

Sorry but I am totally new to this and just taking my first steps, so
the fault is probably going to be an obvious one.

P.s. the search is OK if I look for all names or towns so the database
connection and naming is correct.

Many thanks

Alec

Aug 30 '05 #2
UPDATE

Stupid me.....

Problem solved with ('SELECT name FROM site01_details WHERE town
LIKE "Bury"');

But question still applies about using the entered form text in the
search string.

Many thanks.

Alec

Aug 30 '05 #3
Here,

First of all in ur PHP code, U have to confirm that u get $_GET['town']
!= "" ,if it is not NULL then u have make below changes in ur syntax
as:

$result = @mysql_query "SELECT name FROM site01_details WHERE town
LIKE %"'.$_GET['town'].' ");
RANI.

Aug 30 '05 #4
Rani

Will try that now. Thanks.

Wolfgang, sorry I meant under www.freeweekends.co.uk/test.php

Thanks both

Alec

Aug 30 '05 #5
JDS
On Tue, 30 Aug 2005 02:45:54 -0700, Alec wrote:
Problem solved with ('SELECT name FROM site01_details WHERE town
LIKE "Bury"');


Your SQL statement should:

* use single quotes around the value after LIKE
http://dev.mysql.com/doc/mysql/en/string-syntax.html

* There really is no reason to use LIKE if you do not use wildcards -- the
percent sign "%". Use equals "=" instead

Your SQL statement should look like:

$sql_statement = "SELECT name FROM site01_details WHERE town = 'Bury'";

You are best off putting the SQL statment in a variable and putting the
variable into the mysql_query() function:

$result = mysql_query($sql_statement);

(I don't know why some books don't do this straight away, or at all. It
doesn't make sense to me to teach something one way and then un-teach it
later.)

I have other comments on coding style and whatnot but this is enough for
now, I imagine.

COWS

--
JDS | je*****@example.invalid
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

Aug 31 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Rafael Nenninger | last post by:
This question has to do with MS file search but it is happening only with ..asp pages, so I though someone programming with .asp pages has experienced the same situation. I'm trying to find .asp...
19
by: RAJASEKHAR KONDABALA | last post by:
Hi, Does anybody know what the fastest way is to "search for a value in a singly-linked list from its tail" as oposed to its head? I am talking about a non-circular singly-linked list, i.e.,...
28
by: joshc | last post by:
If I have an array of data that I know to be sorted in increasing order, and the array is less than 50 elements, and I want to find the first element greater than a certain value, is a simple...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
0
by: | last post by:
I have a question about spawning and displaying subordinate list controls within a list control. I'm also interested in feedback about the design of my search application. Lots of code is at the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.