Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   PHP (http://www.programmingforums.org/forum29.html)
-   -   Mysql search question? (http://www.programmingforums.org/showthread.php?t=3474)

dmorales Apr 20th, 2005 10:43 AM

Mysql search question?
 
How do I make my search box work?

I want them to search a specific field in a specific table and display matching results as a link, which you could click and then view informaiton pertaining to that subject.

The part I don't know how to do is the search and display as a link. Can anyone help me? A short example code would be great!

dmorales Apr 20th, 2005 10:49 AM

oh, and I know you have to use MATCH correct? but how do I display results? and how do I narrow the fields I"m searching whithin?

Pizentios Apr 20th, 2005 11:16 AM

what language are you trying to do this in? php?

dmorales Apr 21st, 2005 8:51 AM

Oops, sorry yes php.

Pizentios Apr 21st, 2005 9:06 AM

Here's a example of the php website:

[PHP]
<?php
// Connecting, selecting database
$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')
or die('Could not connect: ' . mysql_error());
echo 'Connected successfully';
mysql_select_db('my_database') or die('Could not select database');

// Performing SQL query
$query = 'SELECT * FROM my_table';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

// Printing results in HTML
echo "<table>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td>$col_value</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";

// Free resultset
mysql_free_result($result);

// Closing connection
mysql_close($link);
?>
[/PHP]

a list of all mysql related functions along with a few examples etc can be found here: http://ca3.php.net/manual/en/ref.mysql.php

hope that helps.

dmorales Apr 21st, 2005 1:36 PM

and to list them as links?

So it lists possible matches then you can select the one your are looking for and it will link you to more information about that item.

Pizentios Apr 21st, 2005 3:45 PM

to list em as links:

[PHP]
<?php
// Connecting, selecting database
$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')
or die('Could not connect: ' . mysql_error());
echo 'Connected successfully';
mysql_select_db('my_database') or die('Could not select database');

// Performing SQL query
$query = 'SELECT * FROM my_table';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

// Printing results in HTML
echo "<table>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td><a href=\"www.somedomain/somepage.php?action=viewresult\">$col_value</a></td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";

// Free resultset
mysql_free_result($result);

// Closing connection
mysql_close($link);
?>
[/PHP]

something like that. you just need to put the html link tags around what you want.

dmorales Apr 21st, 2005 11:20 PM

Cool! Thanks, I didin't know you could do that. :)

Pizentios Apr 22nd, 2005 8:44 AM

heh, you can do all sorts of things with php.

oh, and see this part:

[PHP]
echo "\t\t<td><a href=\"www.somedomain/somepage.php?action=viewresult\">$col_value</a></td>\n";
[/PHP]

see the action var after the page name and the ?

you can get the value of that var by doing this:
[PHP]
$mynewvar = $_GET['action'];
//or you could just call the var strait, like into a if statment:
if ($_GET['action'] == "something")
{
//do something.
}

//or you could pass it to a switch (my personaly fav way of doing stuff like that)
switch ($_GET['action'])
{
case "something1":
//do something1
break;
case "something2":
//do something2
break;
}
[/PHP]

not sure if you knew how to do that, just thought i'd post and try and help out :-)

anyways, if you got more questions, done hesitate to post em around here.


also, since this is a php question, i moved it to the php forum :-)

dmorales Apr 22nd, 2005 12:38 PM

Ok thanks! Maybe you can help me a little with this part. This is the submit script for this piece of the db. What I"m wanting to do is take the file path and the name of the files uploaded and add them to their respective fields in a table. But everything I"ve tried causes an error or just inserts array[name] every time. so here's the code I"m using and maybe you can tell me what's wrong with it.

:

<?php
        //check required fields
        if (($_POST['add_work_order_client'] == "") || ($_POST['add_work_order_work'] == "") || ($_POST['add_work_order_subject'] == "")) {
                echo
                "<body link='black' alink='red' vlink='red'>
                <font color=red><strong>Please enter all required information</strong></font><br /><br />
                <a href='/add_work_order.php'>back</a>
                </body>";
        } else {
        //connect to Mysql and select db
                $conn = mysql_connect('localhost', 'root', '')
                        or die(mysql_error());
                mysql_select_db('anchor_db', $conn) or die(mysql_error());
               
        //get master id
                $master_id = mysql_insert_id();
               
                        //add to work_orders table
                        $add_work = "insert into work_orders values ('', $master_id, now(), now(), '$_POST[add_work_order_work]',
                                '$_POST[add_work_order_subject]')";
                        @mysql_query($add_work) or die(mysql_error());
               
        //Upload files
                if ($_FILES['add_work_order_screen'] != "") {
                        @copy($_FILES[add_work_order_screen][tmp_name], "c:/Program Files/EasyPHP1-7/www/screenshot files/".$_FILES[add_work_order_screen][name]);
                echo "<p>Screenshot Successfully Uploaded</p>";
                } else {
                        echo "Couldn't Copy Screenshots File";
                }
               
                if ($_FILES['add_work_order'] != "") {
                        @copy($_FILES['add_work_order']['tmp_name'], "c:/Program Files/EasyPHP1-7/www/work order files/".$_FILES[add_work_order][name]);
                echo "<p>Work Order Successfully Uploaded</p>";
                } else {
                        echo "Couldn't Copy Work Order File";
                }
               
                //check relevance
                if (($_POST['add_work_order_text']) || ($_POST['add_work_order_screen']) ||  ($_POST['add_work_order'])) {
                        //add to wocontents table
                        $add_contents = "insert into wocontents values ('', $master_id, now(), now(), '$_POST[add_work_order_text]',
                                'c:/Program Files/EasyPHP1-7/www/screenshot files/'.$_FILES[add_work_order_screen][name], 'c:/Program Files/EasyPHP1-7/www/work order files/'.$_FILES[add_work_order][name])";
                        @mysql_query($add_contents) or die(mysql_error());
                }
               
                echo "<p>Your entry has been successfully added</p>";
                echo "<p><strong>Would you like to:</strong></p> <p><a href='http://localhost/add_work_order.php'>add another?</a></p>";
                echo "<p><a href='http://localhost/'>Go home?</a></p>";
                       
        }
?>



All times are GMT -5. The time now is 11:58 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC