Viewed   138 times

I'm creating a small mock-library adminstrator panel. I'm new to php so was hoping someone out there can help with my code.

I'm trying to filter results based on whatever dropdown option/paremter is selected. The dropdowns refer to three columns within my database table where the required information is due to be pulled from.

So for instance: - book_title selected - search book_title WHERE query? IS LIKE '%'; I know it's along these lines but would like some guidance.

 // Ignore warning and error messages
ini_set('display_errors', '1');

$value= $_POST["filter-query"];    

@mysql_select_db($duncan_library) or die( "Unable to select database");

if ($_POST['filter-query'] == 'catalog_number')
$query = "SELECT * FROM books WHERE catalog_number LIKE '%user-search'%;
elseif ($_POST['filter-query'] == 'book_title')
$query = "SELECT * FROM books WHERE book_title LIKE '%user-search'%;
elseif ($_POST['filter-query'] == 'book_author')
$query = "SELECT * FROM books WHERE book_author LIKE '%user-search'%;

$sql = mysql_query($query);
while ($row = mysql_fetch_array($sql)) {
        $catalog_number = $row["catalog_number"];
        $book_title = $row["book_title"];
        $book_author = $row["book_author"];

    echo $row["myrow"]."";

                <select name="filter-query"">
                    <option value="catalog_number">Catalog Number</option>
                    <option value="book_title">Book Title</option>
                    <option value="book_author">Book Author</option>


Current Error

Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:xampphtdocslibraryindex.php on line 20



Put your LIKE string inside quotes even the % sign and also you missed closing double quotes

    if ($_POST['filter-query'] == 'catalog_number')
    $query = "SELECT * FROM books WHERE catalog_number LIKE '%user-search%'";
    elseif ($_POST['filter-query'] == 'book_title')
    $query = "SELECT * FROM books WHERE book_title LIKE '%user-search%'";
    elseif ($_POST['filter-query'] == 'book_author')
    $query = "SELECT * FROM books WHERE book_author LIKE '%user-search%'";
Wednesday, November 23, 2022
$sql = new mysqli('','root','Qwert12345','plot_io_db');
//echo $sql->query('Select * From players');

It will work. Just remove port from localhost (

Tuesday, August 30, 2022

mysqli_connect("","username" ,"password","databasename");//Server name cannot be NULL

use loaclhost for server name(In Loacl)

    $con = mysqli_connect("localhost","username" ,"password","databasename");

    if (mysqli_connect_errno())
        echo "Failed to connect to MySQL: " . mysqli_connect_error();

Or can use MySQLi Procedural

    $servername = "localhost";
    $username = "username";
    $password = "password";

    // Create connection
    $con = mysqli_connect($servername, $username, $password);

    // Check connection
    if (!$con) {
        die("Connection failed: " . mysqli_connect_error());
    echo "Connected successfully";


$servername = "localhost";
$username = "root";
$password = "";
Monday, October 10, 2022

You will need to write a custom filter on server side to filter the results. What you type in the box is saved in 'term' and then 'q' is sent as a request parameter with the ajax call. So the ajax call to
should only return the filtered results and not all the results.

Select2 will make an AJAX call every time you type in the search box. You have to filter the results on server side and then return the results based on the text in search box.
I am using it in my JSP/Servlet application as below

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException  
    String searchTerm = request.getParameter("q");

    String json = getOptions(searchTerm);
    //getOptions method will return a string of  in JSON format

Your JavaScript code is correct.

I found Select2 is used here. If you open the link and the results obtained are different and based on the 'q' parameter.
While in you case, the results for calls to url '/concepts/names_for_search?q=d' and '/concepts/names_for_search?q=deri' are the same(i.e. not filtered)

Thursday, August 4, 2022

One method uses window functions and aggregation:

with cp as (
     select customerid, paymentmethod, count(*) as cnt,
            rank() over (partition by customerid order by count(*) desc) as seqnum
     from t
     group by customerid, paymentmethod
    cd as (
     select customerid, deliveryservice, count(*) as cnt
            rank() over (partition by customerid over by count(*) desc) as seqnum
     from t
     group by customerid, deliveryservice
select cp.customerid
from cp join
     on cp.customerid = cd.customerid
where (cp.seqnum = 1 and cp.PaymentMethod = 'CreditCard') and
      (cd.seqnum = 1 and cd.DeliveryService = '24hr');

Because you need the ranks along two different dimensions, I think you need two subqueries (or the equivalent).

Sunday, September 18, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :