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 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题