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?
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
orPDO_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';
}