Click to See Complete Forum and Search --> : Searching a Database


sac8513
10-14-2007, 11:38 PM
I have posted in the PHP forum about this but I do not know if this is where I should have done so.

Basically I have a database that holds just the Names of businesses. I want to create a search that when a particular name is searched or a couple letters are searched the appropriate content is displayed.

I am quite new but I am familiar with creating and adding content to database. I would appreciate anyone help.

Thanks

UI-ZEIKVK
10-15-2007, 06:35 AM
I have posted in the PHP forum about this but I do not know if this is where I should have done so.

Basically I have a database that holds just the Names of businesses. I want to create a search that when a particular name is searched or a couple letters are searched the appropriate content is displayed.

I am quite new but I am familiar with creating and adding content to database. I would appreciate anyone help.

Thanks


check that like statement

sac8513
10-15-2007, 11:07 PM
what does that mean?

UI-ZEIKVK
10-16-2007, 12:05 AM
what does that mean?


Assume you have a table as person, column as name and it contains 'Jason','sammoon' you can search it as this..

select * from person where name like '%mm%'; -> this will show 'sammoon'.

likewise...

sac8513
10-16-2007, 12:13 AM
I dont know if i mentioned but am still a bit new at this so we might have to take this back a few steps...

First how do i create a box and button to search in and where do i set up the code to make it run?

UI-ZEIKVK
10-16-2007, 12:28 AM
First create a html page contains a text box and submit button inside form tags.

search.html

<form action="msg.php" method="POST">
Name <input type="text" name="name">
<input type="submit" name="ok" value="submit">
<form>

msg.php

$name = $_POST['name'];

// db connection bit not included

$sql = "SELECT * FROM person where name like '%$name%' ORDER BY name ";
$res = mysql_query($sql);
$row = mysql_fetch_array($res);

foreach($row as $r)
{
echo "name = $r";
}

sac8513
10-16-2007, 01:43 AM
i am getting errors on line 10 and line 12

sac8513
10-16-2007, 01:49 AM
from the msg.php page

UI-ZEIKVK
10-16-2007, 01:49 AM
i am getting errors on line 10 and line 12

state that error. Copy & paste it!

sac8513
10-16-2007, 01:51 AM
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/buf/public_html/Database/Inspection/msg.php on line 10

Warning: Invalid argument supplied for foreach() in /home/buf/public_html/Database/Inspection/msg.php on line 12

sorry bout that

UI-ZEIKVK
10-16-2007, 02:06 AM
I've stated that db connection is not included....

Here is the full code....

<?php
$name = $_POST['name'];

$db_conn = mysql_connect("server_name", "user", "pass");
mysql_select_db('db_name');

$sql = "SELECT name FROM person where name like '%$name%' ORDER BY name";
$res = mysql_query($sql);
$row = mysql_fetch_array($res, MYSQL_ASSOC);

foreach($row as $r)
{
echo "name = $r";
}
?>

cheers

sac8513
10-16-2007, 02:12 AM
i know you mentioned that. In my code i included it though...

sac8513
10-16-2007, 02:13 AM
ill try this, maybe i made a typo

UI-ZEIKVK
10-16-2007, 02:16 AM
ill try this, maybe i made a typo


can you paste your code here...

sac8513
10-16-2007, 02:19 AM
So i am still receiving the same errors...It appears the issue is in these 2 lines



$row = mysql_fetch_array($res, MYSQL_ASSOC);

foreach($row as $r)

sac8513
10-16-2007, 02:21 AM
i changed the table names and such

<?


$name = $_POST['name'];

include "DBConnect.inc.php";

$sql = "SELECT * FROM restaurant where name like '%$name%' ORDER BY store_name ";
$res = mysql_query($sql);
$row = mysql_fetch_array($res, MYSQL_ASSOC);

foreach($row as $r)
{
echo "name = $r";
}
?>

UI-ZEIKVK
10-16-2007, 02:25 AM
Is your parameters for mysql_connect() and mysql_select_db() are fine..
I think the problem is the connection is not establishing..

sac8513
10-16-2007, 02:29 AM
ive been using them to update my other tables and ive had no connection problems..

UI-ZEIKVK
10-16-2007, 02:43 AM
ive been using them to update my other tables and ive had no connection problems..


have you use

$db_conn = mysql_connect("server_name", "user", "pass");
mysql_select_db('db_name');

for the connection.. If not check it out..

sac8513
10-16-2007, 02:49 AM
Yea I have it set up exactly like that