Jump to content

Mysterious numbers appear when returning the result.


Niixie

Recommended Posts

Hello PHP people.

 

I have a problem with my php function, which i hope is a small problem.

When i get the result of my function, the numbers "1" and "2" appear, the number "2" above the area, and the number "1" under the area.

Picture proof.

11gkcio.png

 

What i don't get, is where the numbers are coming from, because when i check the database, there is only one row with the numbers "1" and "2", but the numbers appear no matter which row i return.

 

Heres a piece of my code, I hope that you can see the problem somewhere?

 

$q = "SELECT * FROM itrades WHERE active = 1 ORDER BY RAND() LIMIT $sort";
            $result = mysql_query($q) or die(mysql_error());
            
            if(!$result) {
                $message = 'Invalid query: ' . mysql_error() . "\n";
                $message .= 'Whole query: ' . $q;
                die($message);
            }
            
            echo '<h3>Kørende I-Trades</h3>
                    <dl class="list1">';
            while($array = mysql_fetch_assoc($result)) {
                echo '
                    <dt>'.user::get_name(user::get_email($array['itraderid'])).'</dt>
                    <dd>
                        <p><i>'.$array['date'].'</i><br /><strong>'.$array['headline'].'</strong><br />'.$array['shortdis'].'</p>
                        <a href="#" class="link1">Se Trade</a>
                    </dd>';
            }
            echo '</dl>';

 

And, if you're wondering, then the numbers appear before and after the function result.

Picture proof.

juafcl.png

 

Thanks in advance!

Niixie

Link to comment
Share on other sites

The hole code of my function is this:

public function load_trades($sort) {
        Connection::Open();
        if($sort == "*") {
            $q = "select * from itrades where active=1";
            $result = mysql_query($q) or die(mysql_error());
            
            if(!$result) {
                $message = 'Invalid query: ' . mysql_error() . "\n";
                $message .= 'Whole query: ' . $q;
                die($message);
            }
            
            echo '<table class="area" border="0">
                        <thead>
                            <tr>
                                <th>Annonce</th>
                                <th>Dato</th>
                                <th colspan="2">Opretter</th>
                                <th>Læst</th>
                                <th>Besvaret</th>
                            </tr>
                        </thead>
                        <tbody>';
            while($array = mysql_fetch_assoc($result)) {
                echo '
                    <tr>
                        <td class="headline"><a href=trade.php?show='.$array['tradeid'].'>'.$array['headline'].'</a></td>
                        <td class="date">'.$array['date'].'</td>
                        <td class="picture"><a href="profile.php?show='.$array['itraderid'].'"><img src="images/profile/'.user::get_picture(user::get_email($array['itraderid'])).'" width="33" height="32"/></a></td>
                        <td class="name"><a href="profile.php?show='.$array['itraderid'].'">'.user::get_name(user::get_email($array['itraderid'])).'</a></td>
                        <td class="read">'.$array['read'].'</td>
                        <td class="counter">'.itrade::count_messages($array['tradeid']).'</td>
                    </tr>
                ';
            }
            echo '</tbody>
                    </table>';
        } else if($sort !== "*" && is_numeric($sort)) {
            $q = "SELECT * FROM itrades WHERE active = 1 ORDER BY RAND() LIMIT $sort";
            $result = mysql_query($q) or die(mysql_error());
            
            if(!$result) {
                $message = 'Invalid query: ' . mysql_error() . "\n";
                $message .= 'Whole query: ' . $q;
                die($message);
            }
            
            echo '<h3>Kørende I-Trades</h3>
                    <dl class="list1">';
            while($array = mysql_fetch_assoc($result)) {
                echo '
                    <dt>'.user::get_name(user::get_email($array['itraderid'])).'</dt>
                    <dd>
                        <p><i>'.$array['date'].'</i><br /><strong>'.$array['headline'].'</strong><br />'.$array['shortdis'].'</p>
                        <a href="trade.php?show='.$array['tradeid'].'" class="link1">Se Trade</a>
                    </dd>';
            }
            echo '</dl>';
        }
        mysql_free_result($result);
        return 1;
        Connection::Close();
    }

 

But it only counts from the else if part.

Link to comment
Share on other sites

I found out that the returning number "1" is caused by the function mysql::system that i made;

Old code ...

 

Its the "return 1;" part, i need to replace the "mysql_fetch_row" with something else, what could handle it?

 

EDIT: My fault, the function did not cause the number "1".

For some reason it does not return anything when i changed the system function code?

 

New code:

Connection::Open();
        $q = "select $select from system";
        $result = mysql_query($q) or die(mysql_error());
        
        if(!$result) {
            $message = 'Invalid query: ' . mysql_error() . "\n";
            $message .= 'Whole query: ' . $q;
            die($message);
        }
        
        while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
            echo $row[$select];
        }
        Connection::Close();

Link to comment
Share on other sites

organized your code abit

 

 

<?php
$q = "SELECT * FROM itrades WHERE active = 1 ORDER BY RAND() LIMIT $sort";
            $result = mysql_query($q) or die(mysql_error());
            
            if(!$result) {
                $message = 'Invalid query: ' . mysql_error() . "\n";
                $message .= 'Whole query: ' . $q;
                die($message);
            }
            
            echo <<<'EOT'<h3>Kørende I-Trades</h3>
                    <dl class="list1">
EOT;
            while($array = mysql_fetch_assoc($result)) {
            	    
            	  echo  <<<'EOT'
                '
                <dt>{user::get_name(user::get_email($array['itraderid']))}</dt>
                    <dd>
                    <p><i>{$array['date']</i><br /><strong>$array['headline']</strong><br />$array['shortdis']}</p>
                        <a href="#" class="link1">Se Trade</a>
                    </dd>
EOT;
        
          echo  <<<'EOT'</dl>
EOT;
            ?>

 

now your problem is

the last bit of line should use an if condition or ternary to establish if it is true or false.

 

 

 $row =(if some condition here) ? echo <<<'EOT' {$row[0]} 
EOT; : return 1; //if condition met return true , if not return false

Link to comment
Share on other sites

organized your code abit

 

 

<?php
$q = "SELECT * FROM itrades WHERE active = 1 ORDER BY RAND() LIMIT $sort";
            $result = mysql_query($q) or die(mysql_error());
            
            if(!$result) {
                $message = 'Invalid query: ' . mysql_error() . "\n";
                $message .= 'Whole query: ' . $q;
                die($message);
            }
            
            echo <<<'EOT'<h3>Kørende I-Trades</h3>
                    <dl class="list1">
EOT;
            while($array = mysql_fetch_assoc($result)) {
            	    
            	  echo  <<< 'EOT'
                '
                <dt>{user::get_name(user::get_email($array['itraderid']))}</dt>
                    <dd>
                    <p><i>{$array['date']</i><br /><strong>$array['headline']</strong><br />$array['shortdis']}</p>
                        <a href="#" class="link1">Se Trade</a>
                    </dd>
EOT;
        
          echo  <<<'EOT'</dl>
EOT;
            ?>

 

now your problem is

the last bit of line should use an if condition or ternary to establish if it is true or false.

 

 

 $row =(if some condition here) ? echo <<<'EOT' {$row[0]} 
EOT; : return 1; //if condition met return true , if not return false

 

I am in no doubt that your code may be faster, or better than mine but it does not really help when i have absolutely no idea of whats going on in the code?

Link to comment
Share on other sites

If you look at picture proof 1 in the very first post, then you can see that the numbers returns first 2 then 1.

And the last problem i wrote, about nothing returning, is because of the function system that does not return anything, or it doesn't return the correct thing.

Link to comment
Share on other sites

This is my function as it is now, when i changed it for about 15 minutes ago.

 

public function system($select) {
        Connection::Open();
        $q = "select $select from system";
        $result = mysql_query($q) or die(mysql_error());
        
        if(!$result) {
            $message = 'Invalid query: ' . mysql_error() . "\n";
            $message .= 'Whole query: ' . $q;
            die($message);
        }
        
        while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
            echo $row[$select];
        }
        Connection::Close();
    }

 

Still problematic.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.