Most annoyingly I cant get this to work, I made a sample table where there will be duplicate entries in some of the fields, and I need to display then all.

table looks a bit like this
id county city phonenumber

the id is unique as is the phonenumber, but there are records that would have the same county and city.. i need to search on county and display all the records for that county, but it only returns one record..

so I would want a result like the below, if my searh query was "cardiff"

1 cardiff llandaff 123
2 cardiff llandaff 456
3 cardiff pontypoo 856
4 cardiff pontypoo 966

hope you can help...

Recommended Answers

Try this

$result = mysql_query("SELECT * FROM area WHERE county = '".$county."'");

But the query looks allright, try to run your query from the phpmyadmin or the administrative tool you have, and see the results there first.

Jump to Post

echo the $result without mysql_query and see the query, and then run that query in phpmyadmin

Jump to Post

Might your other records may contain a trailing space?

Jump to Post

echo it without mysql_query

Jump to Post

Yes, in this case you are echoing the last row from the database because your script continues after the while loop.

As @pritaeas said, you have to echo inside your while

Jump to Post

All 17 Replies

my search string is this:

$result = mysql_query("SELECT * FROM area WHERE county = '$county'");

$county coming from a form...

Try this

$result = mysql_query("SELECT * FROM area WHERE county = '".$county."'");

But the query looks allright, try to run your query from the phpmyadmin or the administrative tool you have, and see the results there first.

still will only display one result :( ive been on this for over an hour.. thanks for the reply though

actually, it works in phpmyadmin!!

echo the $result without mysql_query and see the query, and then run that query in phpmyadmin

Might your other records may contain a trailing space?

No trailing spaces, if I echo the result I get "Resource id #4"

echo it without mysql_query

how can i echo a result if there is no query results?

$result = ("SELECT * FROM area WHERE county = '".$county."'");

echo $result;

SELECT * FROM area WHERE county = 'cardiff'

hi, did that, see above, no change :(

post all your code :))

<?php
echo "<h1>Results Summary</h1>";
//TABLE RESULTS
$county = $_POST['county'];
$con = mysql_connect("localhost","root","");
mysql_select_db('project', $con) or die('Cant Connect');
$result = mysql_query("SELECT * FROM area WHERE county = '".$county."'");
while($row = mysql_fetch_array($result))
{
$id = $row['id'];
$county = $row['county'];
$city = $row['city'];
$phone = $row['phone'];
}
echo $id;
echo $county;
echo $city;
echo $phone;
?>

The echo's should be inside the loop...

Yes, in this case you are echoing the last row from the database because your script continues after the while loop.

As @pritaeas said, you have to echo inside your while

How incredibly stupid of me..... thank you both very much, I feel a bit dull..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.