php select 函数生成奇怪的 where 子句
您好,我尝试创建一个函数来生成选择函数。
但是下面的代码
public function select($psTableName, $paFields ="",$paWhere=array())
{
//initial return value
$lbReturn = false;
try
{
$lsQuery = "SELECT * FROM `";
$lsQuery .= $psTableName;
$lsQuery .= "` ";
if (!empty($paWhere)){
$lsQuery .= "WHERE ";
print_r($paWhere);
foreach($paWhere as $lsKey => $lsValue)
{
echo $lsKey."<br>";
$paWhere[] = $lsKey." = '".mysql_real_escape_string($lsValue)."'";
}
$lsQuery .= implode(" AND ", $paWhere);
//$lsQuery = substr($lsQuery,0,(strlen($lsQuery)-5));
}
//echo $lsQuery;
//execute $lsQuery
$this->msLastQuery = $lsQuery;
if(!$this->execute())
{
throw new DBException("Select failed, unable to execute query: ".$lsQuery);
}
else
{
$lbReturn = true;
}
}
catch(DBException $errorMsg)
{
ErrorHandler::handleException($errorMsg);
}
return $lbReturn;
}
生成了这个sql语句:
SELECT * FROM `persons` WHERE [email protected] AND 2d1cf648ca2f0b2499e62ad7386eccc2 AND 1 AND per_email = '[email protected]' AND per_password = '2d1cf648ca2f0b2499e62ad7386eccc2' AND per_active = '1'
我不知道为什么它首先只显示where子句后面的值,然后返回并显示键=>; 价值观。
有任何想法吗?
Hello I try to create a function to generate select functions.
But the following code
public function select($psTableName, $paFields ="",$paWhere=array())
{
//initial return value
$lbReturn = false;
try
{
$lsQuery = "SELECT * FROM `";
$lsQuery .= $psTableName;
$lsQuery .= "` ";
if (!empty($paWhere)){
$lsQuery .= "WHERE ";
print_r($paWhere);
foreach($paWhere as $lsKey => $lsValue)
{
echo $lsKey."<br>";
$paWhere[] = $lsKey." = '".mysql_real_escape_string($lsValue)."'";
}
$lsQuery .= implode(" AND ", $paWhere);
//$lsQuery = substr($lsQuery,0,(strlen($lsQuery)-5));
}
//echo $lsQuery;
//execute $lsQuery
$this->msLastQuery = $lsQuery;
if(!$this->execute())
{
throw new DBException("Select failed, unable to execute query: ".$lsQuery);
}
else
{
$lbReturn = true;
}
}
catch(DBException $errorMsg)
{
ErrorHandler::handleException($errorMsg);
}
return $lbReturn;
}
generates this sql statement:
SELECT * FROM `persons` WHERE [email protected] AND 2d1cf648ca2f0b2499e62ad7386eccc2 AND 1 AND per_email = '[email protected]' AND per_password = '2d1cf648ca2f0b2499e62ad7386eccc2' AND per_active = '1'
I don't know why it first shows only the values after the where clause and then goes back and shows the key => values.
Any idea's?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在循环中重用
$paWhere
,以便附加到当前值。 您需要使用一个新的数组:You reuse
$paWhere
in your loop so you append to the current values. You need to use a fresh array: