Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用mysqli_*connection时aura/sqlquery中的变量绑定_Php_Mysql_Mysqli_Pdo_Auraphp - Fatal编程技术网

Php 使用mysqli_*connection时aura/sqlquery中的变量绑定

Php 使用mysqli_*connection时aura/sqlquery中的变量绑定,php,mysql,mysqli,pdo,auraphp,Php,Mysql,Mysqli,Pdo,Auraphp,我有一个使用mysqli_*()函数的遗留应用程序(实际上,它使用mysql_*()functions.Gah!)。我将其用作SQL查询生成器。例如: $queryFactory = new Aura\SqlQuery\QueryFactory('mysql'); $select = $queryFactory->newSelect(); $select->from('sometable AS t') ->where('t.field1 = 0') ->w

我有一个使用
mysqli_*()
函数的遗留应用程序(实际上,它使用
mysql_*()
functions.Gah!)。我将其用作SQL查询生成器。例如:

$queryFactory = new Aura\SqlQuery\QueryFactory('mysql');
$select = $queryFactory->newSelect();
$select->from('sometable AS t')
    ->where('t.field1 = 0')
    ->where("t.field2 <> ''");
现在我想在
where()
中进行一些变量绑定:

当我对字符串进行强制转换时,转义/绑定似乎从未发生过。似乎只有在使用PDO和prepared语句时才会进行绑定


使用
mysqli
连接时如何在
aura/sqlquery
中获取变量绑定?

您可以使用
$select->getBindValues()
获取绑定值

我要说的是,使用Aura.Sql而不是pdo,因为它在某些其他情况下(如
in()
query)会帮助您

以自述为例

// a PDO connection
$pdo = new PDO(...);

// prepare the statment
$sth = $pdo->prepare($select->getStatement());

// bind the values and execute
$sth->execute($select->getBindValues());
让我知道,如果你需要更多的澄清相同


谢谢。

如果您的PHP版本>=5.6,这里有一个函数,您可以使用它从aura/sqlquery对mysqli运行查询

function mysqli_query_params($mysqli, $query, $params, $types = NULL)
{
    $statement = $mysqli->prepare($select);
    $types = $types ?: str_repeat('s', count($params));
    $statement->bind_param($types, ...$params);
    $statement->execute();
    return $statement;
}
像这样使用

mysqli_query_params($mysqli, $select->getStatement(), $select->getBindValues())

我想查询生成器是一种需求,而您不想要一个能够处理原始SQL的解决方案?@YourCommonSense我更喜欢使用查询生成器来处理复杂的连接。例如,如果一个用户模型有多个需要多个联接的方法,那么可以很容易地在单个辅助方法中执行这些常用联接,然后传递
$select
对象。管理原始SQL文本对我来说很麻烦。是的,我明白了。但仍然如此。我不清楚你的要求。你只需要获得一个原始SQL,用于mysql函数,这是唯一的选择?是的,除非我转换成mysqli(我可能无论如何都应该这样做),并按照你的建议使用
$mysqli->prepare()
(前提是PHP版本足够高,这确实超出了我的直接控制范围)。我想这是您可能需要的答案,因为您希望使用mysqli连接,而不是pdo。感谢您的快速回复。我知道我可以将这些绑定值用于PDO。问题是如何生成可用于mysql或mysqli的原始SQL。
function mysqli_query_params($mysqli, $query, $params, $types = NULL)
{
    $statement = $mysqli->prepare($select);
    $types = $types ?: str_repeat('s', count($params));
    $statement->bind_param($types, ...$params);
    $statement->execute();
    return $statement;
}
mysqli_query_params($mysqli, $select->getStatement(), $select->getBindValues())