OOPHP 从 MySQL 选择
我想知道如何执行以下操作我想创建一个公共函数,允许我从 MYSQL 中进行选择
这是我到目前为止的代码,但它会引发 if 错误。
public function select($table,$options,$where,$orderby)
{
$sql = mysql_query("SELECT ".
if($options)
{
$options
}
." FROM ".
$table
if($where)
{
." WHERE ".$where.
}
if ($orderby)
{
." ORDER BY ".$orderby.
}
."") or mysql_error() ;
$row = mysql_fetch_assoc($sql);
$rows[] = $row;
print json_encode($rows);
}
解析错误:语法错误,/home/realcas/public_html/eshop/ecms/system/classes/database.php 第 23 行出现意外的 T_IF
I am wondering how to do the following I want to create a public function that allows me to do selects from MYSQL
Here is the code I have so far but it brings up a if error.
public function select($table,$options,$where,$orderby)
{
$sql = mysql_query("SELECT ".
if($options)
{
$options
}
." FROM ".
$table
if($where)
{
." WHERE ".$where.
}
if ($orderby)
{
." ORDER BY ".$orderby.
}
."") or mysql_error() ;
$row = mysql_fetch_assoc($sql);
$rows[] = $row;
print json_encode($rows);
}
Parse error: syntax error, unexpected T_IF in /home/realcas/public_html/eshop/ecms/system/classes/database.php on line 23
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试
Try
函数调用中不能有 if 语句。在外部构建 SQL,然后将其直接传递给 mysql_query。示例:
我还假设您在 mysql_error() 之前缺少
exit
。就像现在一样,您不会得到任何输出。将其更改为:第三,您将只能获取一行,因为您只调用 mysql_fetch_assoc 一次。只要有结果,您就应该继续迭代它:
You cannot have if-statements inside a function call. Build your SQL outside and then pass it directly to mysql_query. Example:
I also assume that you're missing an
exit
before mysql_error(). As it is now, you wont get any output. Change it to:Third, you will only be able to fetch a single row since you only invoke
mysql_fetch_assoc
once. You should continue iterating over it as long as there are results:增强方式:
enhanced way: