OOPHP 从 MySQL 选择

发布于 2024-12-09 01:11:34 字数 677 浏览 1 评论 0原文

我想知道如何执行以下操作我想创建一个公共函数,允许我从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不顾 2024-12-16 01:11:34

尝试

$sql = mysql_query("SELECT ". $options ." FROM ". $table .
                   ($where ? "WHERE " . $where : "") .
                   ($orderby? "ORDER BY ".$orderby  : "")) or mysql_error() ; 

$row = mysql_fetch_assoc($sql); 
$rows[] = $row; 
print json_encode($rows); 

Try

$sql = mysql_query("SELECT ". $options ." FROM ". $table .
                   ($where ? "WHERE " . $where : "") .
                   ($orderby? "ORDER BY ".$orderby  : "")) or mysql_error() ; 

$row = mysql_fetch_assoc($sql); 
$rows[] = $row; 
print json_encode($rows); 
帥小哥 2024-12-16 01:11:34

函数调用中不能有 if 语句。在外部构建 SQL,然后将其直接传递给 mysql_query。示例:

$sql = "SELECT ";
if($options)
    $sql .= "FROM " . $table;
if($where)
    $sql .= " WHERE " . $where;
if($orderby)
    $sql .= " ORDER BY " . $orderby;

$query = mysql_query($sql);

我还假设您在 mysql_error() 之前缺少 exit。就像现在一样,您不会得到任何输出。将其更改为:

mysql_query($sql) or die(mysql_error());

第三,您将只能获取一行,因为您只调用 mysql_fetch_assoc 一次。只要有结果,您就应该继续迭代它:

$rows = array();
while($row = mysql_fetch_assoc($query))
    $rows[] = $row;

// $rows will now contain all rows returned from your select statement

You cannot have if-statements inside a function call. Build your SQL outside and then pass it directly to mysql_query. Example:

$sql = "SELECT ";
if($options)
    $sql .= "FROM " . $table;
if($where)
    $sql .= " WHERE " . $where;
if($orderby)
    $sql .= " ORDER BY " . $orderby;

$query = mysql_query($sql);

I also assume that you're missing an exit before mysql_error(). As it is now, you wont get any output. Change it to:

mysql_query($sql) or die(mysql_error());

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:

$rows = array();
while($row = mysql_fetch_assoc($query))
    $rows[] = $row;

// $rows will now contain all rows returned from your select statement
花海 2024-12-16 01:11:34

增强方式:

public function select($table,$options,$where,$orderby)
{
    $options = empty($options) ? "*"   : $options;
    $where  =  empty($where)   ? "1=1" : $where;
    $orderby = empty($orderby) ? ""    : $orderby;

    $qry = "SELECT $options FROM $table WHERE $where $orderby ";
    $result= mysql_query($qry) or die(mysql_error());

    while(($resultArray[] = mysql_fetch_assoc($result));
    return json_encode($resultArray);

}

enhanced way:

public function select($table,$options,$where,$orderby)
{
    $options = empty($options) ? "*"   : $options;
    $where  =  empty($where)   ? "1=1" : $where;
    $orderby = empty($orderby) ? ""    : $orderby;

    $qry = "SELECT $options FROM $table WHERE $where $orderby ";
    $result= mysql_query($qry) or die(mysql_error());

    while(($resultArray[] = mysql_fetch_assoc($result));
    return json_encode($resultArray);

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文