Click here to Skip to main content
15,906,081 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone!, I need your help. I got an error in code, my compiler said "mysq_query() experts parameter 1 to be string, resource given test.php line 21" and "mysql_fetch_array() expects parameter 1 to be resource, null given in test.php on line 72" and I can't figure out how to solve this errors and I don't know how I will add pagination on my codes and I would like to know how I can show any message when there is no result from the filters. . Please see and check my codes below. Thank you in advance for your help. :)

Here is my codes:
<?php

if(isset($_POST['search']))
{
    $valueToSearch = $_POST['valueToSearch'];
    // search in all table columns
    // using concat mysql function
    $query = "SELECT * FROM `dbcharity2016` WHERE CONCAT(`genNo`, `reliefNo`, `disas_date`, `area`, `city`, `type`, `surv_date`, `surv_date`, `surv_volunteers`, `vol_incharge`, `relief_date`, `volunteers`, `venue`, `T_Beneficiary`, `unit`, `individual`, `house`, `sacksNo`, `DRG`, `cost_set`, `cost_relief`, `DAI`, `CAG`, `TCA`) LIKE '%".$valueToSearch."%'";
    $search_result = filterTable($query);
    
}
 else {
    $query = "SELECT * FROM `dbcharity2016`";
    $search_result = filterTable($query);
}

// function to connect and execute the query
function filterTable($query)
{
    $con = mysql_connect("localhost","test_webdev","e7#_AZ*9we_^","test_WebBase");
    $filter_Result = mysql_query($con, $query);
    return $filter_Result;
}

?>
<!DOCTYPE html>
<html>
    <head>
        <title>test</title>
        <style>
            table,tr,th,td
            {
                border: 1px solid black;
            }
        </style>
    </head>
    <body>
        
        <form action="test.php" method="post">
            <input type="text" name="valueToSearch" placeholder="Value To Search"><br><br>
            <input type="submit" name="search" value="Filter"><br><br>
            
            <table>
                <tr>
                    <th>General No.</th>
                    <th>Relief No.</th>
                    <th>Disaster Date</th>
                    <th>Area</th>
                    <th>City</th>
                    <th>Relief type</th>
                    <th>Survey Date</th>
                    <th>Survey Volunteers</th>
                    <th>Volunteer-in-Charge</th>
                    <th>Relief Date</th>
                    <th>Volunteers</th>
                    <th>Venue</th>
                    <th>Beneficiary</th>
                    <th>Unit</th>
                    <th>Individual</th>
                    <th>House</th>
                    <th>No. of Sacks</th>
                    <th>Details of Relief Goods</th>
                    <th>Cost per set</th>
                    <th>Cost of Relief Goods
                    <th>Death and Injuries</th>
                    <th>Cash Assistance</th>
                    <th>Total Cash Assistance</th>

                </tr>

      <!-- populate table from mysql database -->
                <?php while($row = mysql_fetch_array($search_result)):?>
                <tr>
                    <td><?php echo $row['genNo'];?></td>
                    <td><?php echo $row['reliefNo'];?></td>
                    <td><?php echo $row['disas_date'];?></td>
                    <td><?php echo $row['area'];?></td>
                    <td><?php echo $row['city'];?></td>
                    <td><?php echo $row['type'];?></td>
                    <td><?php echo $row['surv_date'];?></td>
                    <td><?php echo $row['surv_volunteers'];?></td>
                    <td><?php echo $row['vol_incharge'];?></td>
                    <td><?php echo $row['relief_date'];?></td>
                    <td><?php echo $row['volunteers'];?></td>
                    <td><?php echo $row['venue'];?></td>
                    <td><?php echo $row['T_Beneficiary'];?></td>
                    <td><?php echo $row['unit'];?></td>
                    <td><?php echo $row['individual'];?></td>
                    <td><?php echo $row['house'];?></td>
                    <td><?php echo $row ['sacksNo'];?></td>
                    <td><?php echo $row ['DRG'];?></td>
                    <td><?php echo $row ['cost_set'];?></td>
                    <td><?php echo $row ['cost_relief'];?></td>
                    <td><?php echo $row ['DAI'];?></td>
                    <td><?php echo $row ['CAG'];?></td>
                    <td><?php echo $row ['TCA'];?></td>
                </tr>
                <?php endwhile;?>
            </table>
        </form>
        
    </body>
</html>


What I have tried:

I tried to change $connection to $con and mysqli_query to mysql_query, unfortunately the error is still there and increased more. I don't know how I fix this mess and how I add pagination too in every page when table hit 10 rows automatic the pagination will appeared. and I know too how to show message "No Result" when there is no result in filter.

[edit by Nelek] Additional information copied from non answer below
And I'm using php version 4.0.1 and Mysql version 5.5.54
Posted
Updated 25-Apr-17 15:09pm
v2

Read the documentation: PHP: mysql_query - Manual[^]
The first parameter is the query string and the second parameter is the link identifier. So you have to use
PHP
$filter_Result = mysql_query($query, $con);

For mysql_fetch_array(), you are passing $search_result. But that variable has not been declared and initialised when using it there. Make that variable global on top of your code:
PHP
global $search_result;

You should also check if that variable is set before using it and report an error if not or it is false (e.g. when there was a database error).

To get the number of matching rows use PHP: mysql_num_rows - Manual[^]. When this is zero no matching rows has been found. The number of rows can be also used for pagination.
 
Share this answer
 
Okay Jochen Arndt. Thank you!. :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900