我从其他语言到PHP并尝试创建一个函数,该函数返回一个包含sql,params和param类型的sql查询对象。
代码:
function buildSQLWhereClause($conn, $place) {
$query = "SELECT p.*, s.* FROM product p INNER JOIN product_shop ps ON ps.p_id = p.p_id INNER JOIN shop s ON s.s_id = ps.s_id WHERE ";
$queryParams = [];
$queryParamTypes = "";
$i = 0;
$len = count($place);
foreach ($place as $key => $value) {
if ($i == $len - 1) {
$query .= "$key = ?";
$queryParams[] = $value;
$queryParamTypes .= "s";
} else {
$query .= "$key = ? AND ";
$queryParams[] = $value;
$queryParamTypes .= "s";
}
$i++;
}
return array(
"query" => $query,
"queryParams" => $queryParams,
"queryParamTypes" => $queryParamTypes
);
}
$queryObject = buildSQLWhereClause($conn, $place);
$query = queryObject['query'];
$queryParams = queryObject['queryParams'];
$queryParamTypes = queryObject['queryParamTypes'];
$ queryObject保存正确的信息。但是如何访问$queryObject
中的信息? $query, $queryParams, and $queryParamTypes
都保留值"q"
。
为什么这3个变量不能保持$queryObject
的预期值?
php错误日志:
[05-May-2016 19:14:25 Pacific/Auckland] PHP Notice: Use of undefined constant queryObject - assumed 'queryObject' in /Applications/MAMP/htdocs/the_vegan_repository/scripts/back_end/get_products.php on line 47
[05-May-2016 19:14:25 Pacific/Auckland] PHP Stack trace:
[05-May-2016 19:14:25 Pacific/Auckland] PHP 1. {main}() /Applications/MAMP/htdocs/the_vegan_repository/scripts/back_end/get_products.php:0
[05-May-2016 19:14:25 Pacific/Auckland] PHP 2. queryDbForProducts($conn = *uninitialized*, $place = *uninitialized*) /Applications/MAMP/htdocs/the_vegan_repository/scripts/back_end/get_products.php:105
[05-May-2016 19:14:25 Pacific/Auckland] PHP Warning: Illegal string offset 'query' in /Applications/MAMP/htdocs/the_vegan_repository/scripts/back_end/get_products.php on line 47
[05-May-2016 19:14:25 Pacific/Auckland] PHP Stack trace:
[05-May-2016 19:14:25 Pacific/Auckland] PHP 1. {main}() /Applications/MAMP/htdocs/the_vegan_repository/scripts/back_end/get_products.php:0
[05-May-2016 19:14:25 Pacific/Auckland] PHP 2. queryDbForProducts($conn = *uninitialized*, $place = *uninitialized*) /Applications/MAMP/htdocs/the_vegan_repository/scripts/back_end/get_products.php:105
[05-May-2016 19:14:25 Pacific/Auckland] PHP Notice: Use of undefined constant queryObject - assumed 'queryObject' in /Applications/MAMP/htdocs/the_vegan_repository/scripts/back_end/get_products.php on line 48
[05-May-2016 19:14:25 Pacific/Auckland] PHP Stack trace:
[05-May-2016 19:14:25 Pacific/Auckland] PHP 1. {main}() /Applications/MAMP/htdocs/the_vegan_repository/scripts/back_end/get_products.php:0
[05-May-2016 19:14:25 Pacific/Auckland] PHP 2. queryDbForProducts($conn = *uninitialized*, $place = *uninitialized*) /Applications/MAMP/htdocs/the_vegan_repository/scripts/back_end/get_products.php:105
[05-May-2016 19:14:25 Pacific/Auckland] PHP Warning: Illegal string offset 'queryParams' in /Applications/MAMP/htdocs/the_vegan_repository/scripts/back_end/get_products.php on line 48
[05-May-2016 19:14:25 Pacific/Auckland] PHP Stack trace:
[05-May-2016 19:14:25 Pacific/Auckland] PHP 1. {main}() /Applications/MAMP/htdocs/the_vegan_repository/scripts/back_end/get_products.php:0
[05-May-2016 19:14:25 Pacific/Auckland] PHP 2. queryDbForProducts($conn = *uninitialized*, $place = *uninitialized*) /Applications/MAMP/htdocs/the_vegan_repository/scripts/back_end/get_products.php:105
[05-May-2016 19:14:25 Pacific/Auckland] PHP Notice: Use of undefined constant queryObject - assumed 'queryObject' in /Applications/MAMP/htdocs/the_vegan_repository/scripts/back_end/get_products.php on line 49
[05-May-2016 19:14:25 Pacific/Auckland] PHP Stack trace:
[05-May-2016 19:14:25 Pacific/Auckland] PHP 1. {main}() /Applications/MAMP/htdocs/the_vegan_repository/scripts/back_end/get_products.php:0
[05-May-2016 19:14:25 Pacific/Auckland] PHP 2. queryDbForProducts($conn = *uninitialized*, $place = *uninitialized*) /Applications/MAMP/htdocs/the_vegan_repository/scripts/back_end/get_products.php:105
[05-May-2016 19:14:25 Pacific/Auckland] PHP Warning: Illegal string offset 'queryParamTypes' in /Applications/MAMP/htdocs/the_vegan_repository/scripts/back_end/get_products.php on line 49
[05-May-2016 19:14:25 Pacific/Auckland] PHP Stack trace:
[05-May-2016 19:14:25 Pacific/Auckland] PHP 1. {main}() /Applications/MAMP/htdocs/the_vegan_repository/scripts/back_end/get_products.php:0
[05-May-2016 19:14:25 Pacific/Auckland] PHP 2. queryDbForProducts($conn = *uninitialized*, $place = *uninitialized*) /Applications/MAMP/htdocs/the_vegan_repository/scripts/back_end/get_products.php:105
[05-May-2016 19:17:51 Pacific/Auckland] PHP Fatal error: Uncaught Error: Call to a member function bind_param() on boolean in /Applications/MAMP/htdocs/the_vegan_repository/scripts/back_end/get_products.php:55
Stack trace:
#0 /Applications/MAMP/htdocs/the_vegan_repository/scripts/back_end/get_products.php(105): queryDbForProducts(Object(mysqli), Array)
#1 {main}
thrown in /Applications/MAMP/htdocs/the_vegan_repository/scripts/back_end/get_products.php on line 55
答案 0 :(得分:1)
您忘记将$
放在queryObject
前面。这应该有效:
$query = $queryObject['query'];
$queryParams = $queryObject['queryParams'];
$queryParamTypes = $queryObject['queryParamTypes'];