Re: Search function not working...

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 2/1/08, Jason Pruim <japruim@xxxxxxxxxx> wrote:
>
> So I said in another thread that I would be asking another question
> about functions... So here it goes, I am attempting to write a
> function to search the database, which used to work just fine when I
> wrote it without using a function (Would that be considered static?)
> Now that I am attempting to rewrite my stuff so I can reuse the code,
> now it's not working... Here is what I used to do and it worked just
> fine:
>
>
> $qstring = "SELECT * FROM ".$table." WHERE FName like '%$search%' or
> LName like '%$search%' or Add1 like '%$search%' or Add2 like '%$search
> %' or City like '%$search%' or State like '%$search%' or Zip like '%
> $search%' or XCode like '%$search%'";
> if ($_SESSION['search'] != NULL){
>        echo "The search string is: <strong>$search</strong>.<BR>";
>        $qrow[]= mysql_query($qstring) or die(mysql_error());
>        $qresult = $qrow[0];
>        $num_rows = mysql_num_rows($qresult);
>        //display search form
>                echo "
>                        <form action='search.php' method='GET'>
>                        <label>Search:
>                        <input type='text' name='search' id='search' />
>                        </label>
>                        <input type='submit' value='Go!' />
>                </form>";
>
>
>
> echo <<<HTML
>        <a href='index.php'>Return to database</A>
>        <P>Total Records found: {$num_rows}</P>
>        <A href='excelexport.php'>Export selection to excel</A>
>        <form method='GET' action='edit.php'>
>        <table border='1'>
>                <tr>
>                <th><a href='?order=a'>First Name</A></th>
>                <th><A href='?order=b'>Last Name</A></th>
>                <th><A href='?order=c'>Address Line 1</A></th>
>                <TH><A href='?order=d'>Address Line 2</A></th>
>                <TH><A href='?order=e'>City</A></th>
>                <th><A href='?order=f'>State</A></th>
>                <th><A href='?order=g'>Zip</A></th>
>                <TH><A href='?order=h'>Code</A></th>
>                <th><A href='?order=i'>ID #</A></th>
>                <TH>Edit</th>
>                <th>Delete</th>
>        </tr>
>
> HTML;
>        echo "Just testing: ".$_SESSION['search'];
> while($qrow = mysql_fetch_assoc($qresult)) {
>        //Display the search results using heredoc syntax
> echo <<<HTML
>
>        <tr>
>
>                <td>{$qrow['FName']}</td>
>                <td>{$qrow['LName']}</td>
>                <td>{$qrow['Add1']}</td>
>                <td>{$qrow['Add2']}</td>
>                <td>{$qrow['City']}</td>
>                <td>{$qrow['State']}</td>
>                <td>{$qrow['Zip']}</td>
>                <td>{$qrow['XCode']}</td>
>                <td>{$qrow['Record']}</td>
>                <td><a href='edit.php
> ?Record={$qrow['Record']}'>Edit</a></td>
>                <td><a href='delete.php
> ?Record={$qrow['Record']}'>Delete</a></td>
>        </tr>
>
>        </form>
> HTML;
>
> Now, here is what I have as a function and is not working:
>
> <?PHP
> $FName ="";
> $LName ="";
> $Add1 = "";
> $Add2 = "";
> //              $_SESSION['search'] = $_GET['search'];
>


function search($searchvar, $table, $num_rows, $FName, $LName, $Add1, $Add2)
{
       $qstring = "SELECT * FROM ".$table." WHERE FName like '%$searchvar%'
                        or LName like '%$searchvar%' or Add1 like
'%$searchvar%' or Add2 like
                        '%$searchvar%' or City like '%$searchvar%' or State
like '%$searchvar%'
                        or Zip like '%$searchvar%' or XCode like
'%$searchvar%'";

        $qrow[]= mysql_query($qstring) or die(mysql_error());
        $qresult = $qrow[0];
        $num_rows = mysql_num_rows($qresult);
        while($qrow = mysql_fetch_assoc($qresult)) {
               $FName = $qrow['FName'];
               $LName = $qrow['LName'];
               $Add1 = $qrow['Add1'];
               $Add2 = $qrow['Add2'];

        }

// what do you want to return...?
                return;

}

?>

$returnedSomething = search($searchvar, $table, $num_rows, $FName, $LName,
$Add1, $Add2);

Jason,

1. You're missing a closing brace at the end of the while loop.
2. You're not returning anything from the function.

The input parameters are pretty clear but what were you expecting to
accomplish within the function?

All the data returned from the query only have scope within the function, so
unless you return something, like an array or a string, use references, or
write to a global variable, you're not doing anything with the data from the
db.

David

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux