Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7962945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:29:14+00:00 2026-06-04T05:29:14+00:00

I am writing a simple search engine for my database using mysqli and prepared

  • 0

I am writing a simple search engine for my database using mysqli and prepared statements, I’m currently using call_user_func_array to dynamically create my query. My question is: is there a faster or better way to do this using mysqli?
here’s my code:

<?php 
session_start();
require('../../../scripts/bkfunctions.php');
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');

//////////////////
$resultParameters = array();
$paramsArr =array();
$results = array();




////DB OPEN//////////////////////////////////////////
$conn = dbConnect("ITbunker");
$paramsArr = explode(",", $_GET['paramsQuery']);
$qStr="";
$bindParams = array('s');

    ////////////////////////////////////
    foreach ($paramsArr as $n=>$k) {

            if ($n==0) {
            $qStr = "SELECT * FROM trabajosEnSubasta WHERE MATCH(titulo, descripcion, habilidades) AGAINST (?)";
            }
            else {
            $qStr .= " UNION SELECT * FROM trabajosEnSubasta WHERE MATCH(titulo, descripcion, habilidades) AGAINST (?)";
            $bindParams[0] .= 's';
            }
            array_push($bindParams, $k);
    }
    ///////////////////////////////////////

    ///Preparar query y ejecutar
    $stmt = $conn->prepare($qStr);
    call_user_func_array(array($stmt, 'bind_param'), makeValuesReferenced($bindParams));
    $stmt->execute();

/////////////////////////////////////////////////////////////////////////////////////////*

$meta = $stmt->result_metadata();  

    while ($campo = $meta->fetch_field() ) {
    $resultParameters[] = &$row[$campo->name];
    }
    call_user_func_array(array($stmt, 'bind_result'), $resultParameters);

    $i=0;
    while ( $stmt->fetch() ) {  
        $i++;
        foreach( $row as $key => $val ) {
        $results["resultado$i"][$key] = $val;
        }  
    }


echo json_encode($results);
///////////////////////////////////////////////////

$conn->close();
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T05:29:15+00:00Added an answer on June 4, 2026 at 5:29 am

    You have a lot of things to address in your question – and call_user_func_array isn’t one of them.

    Look at your query

    This is the sort of query you are building:

    SELECT
        * 
    FROM 
        trabajosEnSubasta 
    WHERE 
        MATCH(titulo, descripcion, habilidades) AGAINST (?)
    UNION 
        SELECT
            * 
        FROM 
            trabajosEnSubasta 
        WHERE 
            MATCH(titulo, descripcion, habilidades) AGAINST (?)
    UNION 
        SELECT
            ...
    

    And.. it begs the question why you’d do that. This is functionally identical, and likely much faster to execute:

    SELECT
        * 
    FROM 
        trabajosEnSubasta 
    WHERE 
        MATCH(titulo, descripcion, habilidades) AGAINST (?)
        OR
        MATCH(titulo, descripcion, habilidades) AGAINST (?)
        OR
        ...
    

    Look at your logic

    $paramsArr = explode(",", $_GET['paramsQuery']);
    

    Therefore requesting a url like /foo?paramsQuery=a,b,c,d,e,f,g,h,i,j will currently trigger a 10 table union query, if you update your query syntax, that’ll be a 10 full-text-index query. Is that really what you want? It might be – but for example if someone searches for a known habilidad it’d be a lot more efficient to do:

    SELECT
        trabajosEnSubasta.*
    FROM
        trabajosEnSubasta
    LEFT JOIN
        habilidades_map ON habilidades_map.trabajo_id = trabajosEnSubasta.id
    WHERE
        habilidades_map.habilidad = "known habilidad"
    

    A full-text index is relatively expensive to query, and shouldn’t really be your only means of searching your data.

    Profile your code

    I have to assume you’re asking about call_user_func_array because your code is slow to execute. Don’t guess where problems are in code look and find out.

    • Use explain to see what your queries are doing
    • Use xdebug profiling to see where your code is spending it’s time
    • Webgrind is an easy to install means of seeing xdebug profile output.

    Dont waste your time optimizing 0.01%

    call_user_func_array does have a cost to use, and that’s why code like this exists. BUT the difference is really, really insignificant (especially in light of the super-heavy UNION queries in the question) unless you’re calling it hundreds+ times. Focus on the things that have the biggest performance impact on your application, not the things that make absolutely no measurable difference one way or the other.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a simple search query for my Entity Framework application. I need to
I am writing a simple C++ database, using Visual Studio 2008 express edition, like
I am writing a simple DOS utility that uses the findstr utility to search
I'm writing simple GUI using wxPyhon and faced some problems. My application does simple
I writing a simple method which should delete an entity from the database if
I'm writing a page that does very simple search queries, resulting in something like:
I am writing a simple search algorithm for wikipedia. I am having trouble when
I'm writing a simple search form for a certain model. Let's call the model
I'm trying to write a simple search server, using Django with AJAX. The server
I'm building a simple recipe search engine with Ruby and Sinatra for an iPhone

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.