dqrfdl5708 2014-04-28 14:43
浏览 21
已采纳

返回一个变量 - PHP

I'm trying to display information from an array

Here is my php code:

<?

include('class.prmessage.php');
$mess = new OMessaging(true);
$array = $mess->GetAllMesseges(0,9);
?>

and this is the function it calls:

    function GetAllMesseges($order = 0, $receiver = '', $sender = '')
{
    switch( $order )
    {
        case 0:
            $order = 'readed ASC';
        case 1:
            $order = 'readed DESC';
    }
    $where = '';
    if(strlen($receiver) > 0 && strlen($sender) > 0)
        $where = ' AND ';

    $where = ((strlen($receiver) > 0)?'receiver=' . $receiver:'') . $where . ((strlen($sender) > 0)?'sender=' . $sender:'');

    $result = @mysql_query("SELECT * FROM ".$this->tblName." WHERE $where ORDER BY $order");

    if( !$result )
        return 1;
    echo $num = mysql_num_rows($result);
    $messege = '';
    for($i = 0 ; $i < $num ; $i++ )
    {
        $row = mysql_fetch_object($result);
        $messege[$i]['receiver'] = $row->receiver;
        $messege[$i]['sender'] = $row->sender;
        $messege[$i]['title'] = $row->title;
        $messege[$i]['body'] = $row->body;
        $messege[$i]['readed'] = $row->readed;  
        $messege[$i]['date'] = $row->date;
    }
    if( !is_array($messege) )
        return 2;
    return $messege;

i can access all the information i want to access, however when i declare the array:

$array = $mess->GetAllMessages(0,9)

it outputs the array length on screen.

How can i stop this?

  • 写回答

1条回答 默认 最新

  • doushen1026 2014-04-28 14:44
    关注

    Just change this line:

    echo $num = mysql_num_rows($result);
    

    To this:

    $num = mysql_num_rows($result);
    

    There are other problems you might want to fix in your code though. As Amal points out, the mysql package is officially deprecated. From the official documentation:

    Warning This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information.

    Also, your switch statement is broken. Without a break statement, the execution will 'fall through' to the next case label. For example:

    $order = 0;
    switch( $order )
    {
        case 0:
            echo 'readed ASC <br>';
        case 1:
            echo 'readed DESC <br>';
    }
    

    Will print both lines:

    readed ASC
    readed DESC
    

    So in your code, if $order is either 0 or 1, it will be assigned a value of 'readed DESC'. You can fix this with a simple break statement, like this:

    switch( $order )
    {
        case 0:
            $order = 'readed ASC';
            break;
        case 1:
            $order = 'readed DESC';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看