-2

How to search from two table and query execute and bind to form json array to pass

cont_search.php

        <?php

    include "connection.php";
    $keyword=$_GET["context"];
    //Checking if any error occured while connecting
    if (mysqli_connect_errno()) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
        die();
    }

    $query =$conn->prepare("(SELECT isbn, title, authors, accession, publisher, pubyear, pages, keywords, subheadings, booknote, rak, hr, vr,            status, 'book' as type FROM books WHERE title LIKE '%".$keyword."%' OR authors LIKE '%".$keyword."%' OR publisher LIKE                      '%".$keyword."%' OR pubyear LIKE '%".$keyword."%' OR keywords LIKE '%".$keyword."%' OR subheadings LIKE '%".$keyword."%' OR               booknote LIKE '%".$keyword."%')  
           UNION ALL
           (SELECT t_acc, t_title, t_author, t_university, t_puby, t_keywords, t_rak, t_hr, t_vr, t_status, 'thesis' as type FROM thesis WHERE t_title LIKE '%".$keyword."%' OR t_author LIKE '%".$keyword."%' OR t_university LIKE '%".$keyword."%' OR t_puby LIKE '%".$keyword."%' OR t_keywords LIKE '%".$keyword."%')");

 echo "Error: " . $query . "<br>" . $conn->error;

    $query->execute();

    $query->bind_result($isbn, $title, $authors, $accession, $publisher, 
  $pubyear, $pages, $key, $sub, $bkn, $rak, $hr, $vr, $status, $tacc, 
                        $ttile, $tauthor, $tuni, $tpuby, $tkey, $trak, $thr, 
   $tvr, $tstatus);


    $books = array(); 
    $data =array();
    //traversing through all the result 
        while($query->fetch()){
        $temp = array();
        $temp['isbn'] = $isbn; 
        $temp['title'] = $title; 
        $temp['authors'] = $authors; 
        $temp['accession'] = $accession; 
        $temp['publisher'] = $publisher; 
        $temp['pubyear'] = $pubyear; 
        $temp['pages'] = $pages; 
        $temp['keywords'] = $key; 
        $temp['subheadings'] = $sub; 
        $temp['booknote'] = $bkn; 
        $temp['rak'] = $rak; 
        $temp['hr'] = $hr; 
        $temp['vr'] = $vr;
        $temp['status'] = $status;
        $temp['t_acc'] = $tacc;
        $temp['t_title'] = $ttile;
        $temp['t_author'] = $tauthor;
        $temp['t_university'] = $tuni;
        $temp['t_puby'] = $tpuby;
        $temp['t_keywords'] = $tkey;
        $temp['t_rak'] = $trak;
        $temp['t_hr'] = $thr;
        $temp['t_vr'] = $tvr;
        $temp['t_status'] = $tstatus;


        array_push($data, $temp);

        }
        $books['ss'] = true; //ss=send status
        $books['search'] = $data;
    //displaying the result in json format 
        header('Access-Control-Allow-Origin: *');
        header('Content-type:application/json;charset=utf-8');
        echo json_encode($books);

    ?>

http://localhost/cont_search.php?context=english

I execute/run above query and error occur

Error: The used SELECT statements have a different number of columns

Fatal error: Uncaught Error: Call to a member function execute() on boolean in C:\cont_search.php

  • 1
    what of this "Error: The used SELECT statements have a different number of columns" in unclear? – Jeff Jun 27 '18 at 20:38
  • 1
    I suggest you read up on how to use [UNION](https://dev.mysql.com/doc/refman/8.0/en/union.html) properly. – Patrick Q Jun 27 '18 at 20:40
  • I use this php for one table and it's work now i get data from two table so i don't know how to execute and bind it... i also not know about error @Jeff – Asad Mallick Jun 27 '18 at 20:40
  • Possible duplicate of [The used SELECT statements have a different number of columns (REDUX!!)](https://stackoverflow.com/questions/3655708/the-used-select-statements-have-a-different-number-of-columns-redux) – Patrick Q Jun 27 '18 at 20:43
  • So, you're trying to prepare a query, and then you see that it produces an error, and then you try to execute it anyway? You need to check the return value from `$conn->prepare`: if it returns false, then print the error message and don't proceed with attempting to execute it. (Note that this is just good coding practice. It only improves your error reporting and eliminates the "Call to a member function on boolean" error message. Others have commented on how to resolve the SQL error itself.) – Greg Schmidt Jun 27 '18 at 20:46

1 Answers1

0

Your first error there is in the sql query.

Each SELECT statement within UNION must have the same number of columns

You have more columns in the first select than in the second one

sanNeck
  • 148
  • 7
  • then how i retrieve data from two table.@sanNeck – Asad Mallick Jun 27 '18 at 20:46
  • and both table have no same column which is use as primary key to make join @sanNeck – Asad Mallick Jun 27 '18 at 20:48
  • 1
    Is `UNION` really what you want? Or are you looking for a `JOIN`? There's no information in your question that would tell us this. You can join on other columns than primary keys, if that's what's required, but we have no way to know right now. – Greg Schmidt Jun 27 '18 at 20:49