Forum Moderators: coopster

Message Too Old, No Replies

searching mysql database

with php

         

jrock2005

12:48 am on Jun 30, 2005 (gmt 0)

10+ Year Member



Can someone help me create a search function in php.

Database name = files
table name = applications

In the table I have the following

ID
appname
descr

I want the people to beable to put an app name in and have the vaules show up in a table.

Or if someone could show me a link to a site that shows you how to do this that would be cool too. Thanks

perkiset

4:44 am on Jun 30, 2005 (gmt 0)

10+ Year Member



Are you saying you simply want a routine that will
output the descr column where appname = what the user input? if so...

<?php
mysql_connect("aHost", "aUser", "aPassword");
mysql_select_db("files");

// Assume we are running under a web server
// and appName is in the URL as a parm
$appName = $_GET['appname'];
$dataSet = mysql_query("select descr from applications where appname like '%$appName%'");
$row = mysql_fetch_row($dataSet);
while ($row) {
print $row[0] . '<br>' . chr(10);
$row = mysql_fetch_row($dataSet);
}
?>

jrock2005

5:14 am on Jun 30, 2005 (gmt 0)

10+ Year Member



Thanks for the reply. It is close to what I want, but. I want a text box and a search button next to it. I want it to query the appname not the description as of right now. For example one of my records is this:


Appname Description
Adobe 7.0 A program that lets you view pdf

If I were to type adobe in the seach box and I searched for it, it would bring this up. If you guys and gals need more details let me know.

perkiset

5:23 am on Jun 30, 2005 (gmt 0)

10+ Year Member



Ummm... unless I am completely misreading you, that SQL returns [descriptions] from the table where the appname column is "similar to *[$appName]*".

With regards to the text box/button biz, you then want to get the input variable from the POST php var, not the get ie.,

in the html:
<input type="text" name="appname" size="32">
<input type="submit" value="Perform Search">

... then in the PHP ...

$appName = $_POST['appname']

... or am I just missing you here?

mcibor

9:41 am on Jun 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



% in LIKE states for every length and character. Therefore I would suggest putting out first the results that start with search pattern and then the rest

This is just the query

$sql = "(SELECT appname, descr FROM applications WHERE appname LIKE '$appName%' ORDER BY appname) UNION (SELECT appname, descr FROM applications WHERE appname LIKE '%$appName%' ORDER BY appname)";

If you search $appName = "Eagle";
you will first get all apps starting with eagle:
Eagle 4.15...
then with word eagle inside
Network Eagle Monitor...

However this query will also find apps containing that search pattern:
Eaglemania
Weagles

etc
Hope this helps
Michal Cibor

perkiset

1:29 pm on Jun 30, 2005 (gmt 0)

10+ Year Member



Nice touch mcibor, may I add - if he really wants query strength, then he should put a fulltext index on the field and do a matching query rather than a straight "like" - the [a href="http://dev.mysql.com/doc/mysql/en/fulltext-search.html"]
MySQL Docs[/a] are pretty good and should get him there.

I use FullText a lot and it works really nicely.

perkiset

1:31 pm on Jun 30, 2005 (gmt 0)

10+ Year Member



Whoops - just checked the HTML codes doc, sorry for the bad formatting...

jrock2005

4:27 am on Jul 1, 2005 (gmt 0)

10+ Year Member



ok so for my app page here is my coding I have so far:


<html>
<head>
<title>Application Database</title>
<br>
<form>
<input type="text" name="appname" size="32">
<input type="submit" value="Perform Search">
</form>
<?
require("../test-php/util.php");
$sql = new MySQL_class;
$sql->Create("files");


echo("<h3>Selecting multiple rows</h3>\n");
echo("<p><ul><table border=1 cellpadding=4>\n");
echo("<tr><th>App Name</th><th>OS</th></tr>\n");
$sql->Query("Select appname, descr from applications order by appname");
for ($i = 0; $i < $sql->rows; $i++) {
$sql->Fetch($i);
$appname = $sql->data[0];
$descr = $sql->data[1];
echo("<tr><td>$appname</td><td>$descr</td></tr>\n");
}
echo("</table></ul>\n");

$sql = "(SELECT appname, descr FROM applications WHERE appname LIKE '$appName%' ORDER BY appname) UNION (SELECT appname, descr FROM applications WHERE appname LIKE '%$appName%' ORDER BY appname)";
?>

Now where do the results get displayed? Thanks

perkiset

5:43 am on Jul 1, 2005 (gmt 0)

10+ Year Member



I apologize jrock, but I am leaving on a trip for the next 2 weeks and can't continue this... but essentially, you need to do two different things here - if the page call method is GET, then you simply display HTML for user to enter his/her data into... if the method is POST then you get passed parameters that you can work with. Note that in your form tag you will have to specify method="POST" or it'll give you nothing...

my particular style would be this:

<!-- html tags, header et al -->
<!-- php/MySQL code that performs a search into an array IF the form method is a post -->

<!-- html + php block that displays the form - and if the array has elements (ie., it was filled up in the prior PHP block) then put display the elements here -->

<!-- closing html, footers etc -->

Hope this helps - I'll check in mid-July when I return to see if you are still stuck.
-EP

jrock2005

1:26 am on Jul 2, 2005 (gmt 0)

10+ Year Member



I am sorry but I do not understand what you are trying to say.

mcibor

6:59 pm on Jul 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is the corrected version of your code:

one file form.html:

<html>
<head></head>
<body>
<title>Application Database</title>
<br>
<form action="process.php" method="POST">
<input type="text" name="appname" size="32">
<input type="submit" value="Perform Search">
</form></body></html>

Form requires attribute action - pointing where to send data. Method is GET or POST. get you see in url, post you don't - therefore post is much better, more user friendly

in second file process.php (the file that form is sent to)

<?
require("../test-php/util.php");
$sql = new MySQL_class;
$sql->Create("files");

echo("<h3>Selecting multiple rows</h3>\n");
echo("<p><ul><table border=1 cellpadding=4>\n");
echo("<tr><th>App Name</th><th>OS</th></tr>\n");

$sqlquery = "(SELECT appname, descr FROM applications WHERE appname LIKE '$appName%' ORDER BY appname) UNION (SELECT appname, descr FROM applications WHERE appname LIKE '%$appName%' ORDER BY appname)";
$sql->Query($sqlquery);

for ($i = 0; $i < $sql->rows; $i++) {

$sql->Fetch($i);
$appname = $sql->data[0];
$descr = $sql->data[1];
echo("<tr><td>$appname</td><td>$descr</td></tr>\n");
}
echo("</table></ul>\n");
?>

And that's it.
Hope this helps you
Michal Cibor