Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to execute query in php & mysql
    primarykey
    data
    text
    <p>Given below is my code for a query. I am using PDO.</p> <p>The search.php page:</p> <pre><code>&lt;?php //Retrieving input values if(!empty($_POST)) { //Extracting input field values $book_id = $_POST['search_book_id']; $title = $_POST['search_title']; $author_name = $_POST['search_author_name']; //Calling function to execute the query $results_search_book = search_book($book_id, $title, $author_name); //Echoing out the query results if(count($results_search_book) &gt; 0) { echo "&lt;tbody&gt;"; foreach ($results_search_book as $row) { echo "$rowStart"; echo "$row[TITLE]"; echo "$rowEnd"; } echo "&lt;/tbody&gt;"; } else { echo "&lt;tfoot&gt;"; echo "&lt;td colspan='4'&gt;The query returned an empty set!&lt;/td&gt;"; echo "&lt;/tfoot&gt;"; } } else { echo "&lt;tfoot&gt;"; echo "&lt;td colspan='4'&gt;No content to display, you haven't run a query yet!&lt;br&gt;Error custom message!&lt;/td&gt;"; echo "&lt;/tfoot&gt;"; } ?&gt; </code></pre> <p>Here is the functions.php page which connects to the db and the query. I am trying to call the function <code>search_book($book_id, $title, $author_name);</code> and returning the result set to print them.</p> <p>Here is the functions.php page: </p> <pre><code>function search_book($x, $y, $z) { $conn = connect_db(); $book_id = $x; $title = '%' . $y . '%'; $author_name = '%' . $z . '%'; echo $book_id; echo $title; echo $author_name; try { //Preapring the query $results = $conn-&gt;prepare("SELECT TITLE, A.BRANCH_NAME, A.BRANCH_ID, NO_OF_COPIES, COUNT(BOOK_LOANS.BOOK_ID) AS NUM_OUT, NO_OF_COPIES - COUNT(BOOK_LOANS.BOOK_ID) AS NUM_AVAIL, AUTHOR_NAME FROM (SELECT TITLE, BOOK.BOOK_ID, BOOK_COPIES.BRANCH_ID, BRANCH_NAME, NO_OF_COPIES, BOOK_AUTHORS.AUTHOR_NAME FROM BOOK,BOOK_COPIES,LIBRARY_BRANCH,BOOK_AUTHORS WHERE BOOK_COPIES.BOOK_ID=BOOK.BOOK_ID AND BOOK_AUTHORS.BOOK_ID = BOOK.BOOK_ID AND (BOOK.TITLE LIKE :title OR BOOK.BOOK_ID = :book_id OR BOOK_AUTHORS.AUTHOR_NAME LIKE :author_name) AND BOOK_COPIES.BRANCH_ID=LIBRARY_BRANCH.BRANCH_ID) AS A LEFT OUTER JOIN BOOK_LOANS ON A.BOOK_ID=BOOK_LOANS.BOOK_ID AND A.BRANCH_ID=BOOK_LOANS.BRANCH_ID GROUP BY A.BOOK_ID, A.BRANCH_ID"); //Executing the query $results-&gt;bindParam(':book_id', $book_id, PDO::PARAM_STR); $results-&gt;bindParam(':title', $title, PDO::PARAM_STR); $results-&gt;bindParam(':author_name', $author_name, PDO::PARAM_STR); $results-&gt;execute(); $data = $results-&gt;fetchAll(); return $data; } catch(PDOException $e) { echo 'ERROR: Book Search query failed!'; } } </code></pre> <p>I am getting all the rows in return whereas I just need the qualified ones.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload