Hello again.

I'm looking for a way of displaying the results of a search facility on a site im creating.

The site is based on an animal sanctury and the database holds two key tables for searching 'blog' and 'animals' respectivly.

Now, how would i go about displaying the results for example in my site search i typed in 'moggy' how can i display the results of moggy from the animal database (age,colour,breed etc) and moggy from the blog (title,content) and echo it out accordingly

The search part itsself is no real problem its the fetching and displaying the results that i can't fiqure out.
Any help would be great, thanks


8 Years
Discussion Span
Last Post by emhmk1

usually after you make the connection, call mysql_query() to set your query then call
mysql_fetch_array() . Then you will have an array to read out.

The ultimate authority is here:

Thanks for the reply, i'm aware of that section of code, what i was meaning is.
i have 2 tables and 6 rows to search
1 = animals - name,colour,type,breed
2 = Blog - title,artical

How do i echo out the results in a fetch_array when only 1 row matches? or indeed if 5 rows match?

is it simply

while ($row = mysql_fetch_array($result)){
echo $row['name']\r\n;
echo $row['colour']\r\n;
echo $row['type']\r\n;
echo $row['breed']\r\n;
echo $row['title']\r\n;
echo $row['artical']\r\n

i dont see how that will work?!



why not? there will be an array of associative arrays.
Do a var_dump($row) to see what you have.
I am assuming that your query didn't fail and return a null, of coarse.
BTW is "article" misspelled or is that the British spelling?
I know that colour is different than our spelling of color.
I pointed that out just in case you have a problem displaying that variable.


Thanks for the reply,

Firstly, yes it was a typo! Thanks for that!
Secondly, We may have crossed wires after what i am after so i will try and explain it slightly better, sorry to seem dim about this but my head is cracking up.

When i get the results from the search i want to be able to distinguish the two tables in the results so for example if i type in a name, say 'harry' then on my db there i a table called animals which lists all the animals names and stuff about that particular animal. Then in blogs there may be an article on 'harry' so that stuff will be returned too.

in my head i have it as this

while ($row = mysql_fetch_array($sql)){
echo "<h1>Blog results</h1>";
echo $row['title'];
echo $row['article'];

echo "<h1>Animal Results </h1>";
echo $row['name'];
echo $row['colour'];
echo $row['type'] 

i know the code above will output the <h1> for every returned result but i want to identify where each result comes from!

The above code is the only way can kind of explain what i'm after.
Would it be better doing a mulitple query and doing a mysql_num_rows?

Thanks for your input, it is greatly appreciated.



I'm not clear how you are linking those tables.
You could do it with a foreign key . I don't know how the blog table is indexed so that it can be related to the animal table, so I really don't know what the answer to the question would be.
Google mysql joins and foreign key.

As for the repetitive heading try this;

echo "<div style ='width:100%'><h1>Blog results</h1><div style='float:left;margin-left:5%'><h1>Animal Results </h1></div></div>";
while ($row = mysql_fetch_array($sql)){

echo "<div style ='width:100%'> $row['title'].&nbsp.$row['article'].
<div style='float:left;margin-left:5%'> $row['name']. &nbsp
.$row['colour'].&nbsp.$row['type'] </div></div>"


The above is not how i would do this as a production site, but it should give you one set of headings with the data rows beneath.
The better way is to create the HTML document and do in-line php echo.

Edited by JRM: n/a


Thanks for your assistance JRM, i decided to do two queries and do the inline echo on the html page for the results.


This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.