PHP函数/过程动态绑定问号 [英] PHP function/procedure to bind question marks dynamically

查看:65
本文介绍了PHP函数/过程动态绑定问号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚接触PHP.我需要通过编写自定义函数来绑定PDO中的参数.

I'm fairly new to PHP. I'm in need to bind the parameters in PDO by writing a custom function.

说这些是我拥有的2条SQL.

Say these are the 2 sqls I have.

sample_sql_1="select f_name, age, address from table1 where l_name=? and dob >= ? and cty =?"

sample_sql_2="select * from table2 where cty=?"

我想编写一个接受问题&的sql查询的函数不管我传递多少参数,都可以将要绑定的参数绑定到问号.

I would like to write a function that accepts the sql query in question & bind the parameters to be bound to question marks irrespective of how many parameters I pass.

示例:我想打个电话

bind_params(sample_sql_1,array($name,$dob,$cty));
bind_params(sample_sql_2,array($cty));

这是我到目前为止编写的仅用于连接到数据库的功能

Here's the function I've writen so far to just connect to the DB

function pdo_db_query($query) {
    try {
    # MySQL with PDO_MYSQL
    $DBH = new dbconn(); // Create DB connection

    $DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $DBH->setAttribute( PDO::ATTR_EMULATE_PREPARES, false );
    $STH = $DBH->prepare($query);

    // Please help to create a dynamic function to bind
     bind_params(sample_sql_1,array($name,$dob,$cty));
     bind_params(sample_sql_2,array($cty));

    / Execute the query
    $STH->execute();

    # setting the fetch mode
    $STH->setFetchMode(PDO::FETCH_ASSOC);

    // Create temporary array variable
    $json_arr = array();

    while ($row = $STH->fetch()) {
        $json_arr[] = $row;
    }

    # Close the connection
    $DBH = null;

    // Return the result set as a json
    echo json_encode($json_arr);
    } catch (PDOException $e) {
    echo $e->getMessage();
    var_dump($e->getMessage());
    }
}

我需要编写函数"bind_params"的帮助.任何帮助都将使我受益匪浅.

I need help writing the function "bind_params". Any help would greatly benefit me please.

推荐答案

您不一定需要bind_params(),您只需将值作为数组提供给execute().

You don't necessarily need bind_params(), you can just provide the values as an array to execute().

请参见文档中的示例:

See this example from the documentation:

/* Execute a prepared statement by passing an array of insert values */
$calories = 150;
$colour = 'red';
$sth = $dbh->prepare('SELECT name, colour, calories
    FROM fruit
    WHERE calories < ? AND colour = ?');
$sth->execute(array($calories, $colour));

专门针对您的情况:

// add a parameter for the values
function pdo_db_query($query, $params = array()) {
    try {
    # MySQL with PDO_MYSQL
    $DBH = new dbconn(); // Create DB connection

    $DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $DBH->setAttribute( PDO::ATTR_EMULATE_PREPARES, false );
    $STH = $DBH->prepare($query);

    // Execute the query with the given params
    $STH->execute($params);

    # setting the fetch mode
    $STH->setFetchMode(PDO::FETCH_ASSOC);

    // Create temporary array variable
    $json_arr = array();

    while ($row = $STH->fetch()) {
        $json_arr[] = $row;
    }

    # Close the connection
    $DBH = null;

    // Return the result set as a json
    echo json_encode($json_arr);
    } catch (PDOException $e) {
    echo $e->getMessage();
    var_dump($e->getMessage());
    }
}

要将其与LIKE查询一起使用:

To use this with a LIKE query:

$query = "SELECT * FROM table WHERE field LIKE ?";
$params = array( '%' . $searchvalue . '%' );
$result = pdo_db_query( $query, $params );

这篇关于PHP函数/过程动态绑定问号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆