Niixie Posted February 4, 2012 Share Posted February 4, 2012 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. 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. Thanks in advance! Niixie Quote Link to comment https://forums.phpfreaks.com/topic/256391-mysterious-numbers-appear-when-returning-the-result/ Share on other sites More sharing options...
darkfreaks Posted February 4, 2012 Share Posted February 4, 2012 can i see the full code? it seems your problem is before the username outputs. Quote Link to comment https://forums.phpfreaks.com/topic/256391-mysterious-numbers-appear-when-returning-the-result/#findComment-1314430 Share on other sites More sharing options...
Niixie Posted February 4, 2012 Author Share Posted February 4, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/256391-mysterious-numbers-appear-when-returning-the-result/#findComment-1314432 Share on other sites More sharing options...
premiso Posted February 4, 2012 Share Posted February 4, 2012 The probable cause is in the Connection::Open(); / Close, although the close is after the return. But since you are returning 1, that fits the output after. So yea, we will need to see more code to help. Quote Link to comment https://forums.phpfreaks.com/topic/256391-mysterious-numbers-appear-when-returning-the-result/#findComment-1314435 Share on other sites More sharing options...
Niixie Posted February 4, 2012 Author Share Posted February 4, 2012 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(); Quote Link to comment https://forums.phpfreaks.com/topic/256391-mysterious-numbers-appear-when-returning-the-result/#findComment-1314437 Share on other sites More sharing options...
darkfreaks Posted February 4, 2012 Share Posted February 4, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/256391-mysterious-numbers-appear-when-returning-the-result/#findComment-1314438 Share on other sites More sharing options...
Niixie Posted February 4, 2012 Author Share Posted February 4, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/256391-mysterious-numbers-appear-when-returning-the-result/#findComment-1314439 Share on other sites More sharing options...
darkfreaks Posted February 4, 2012 Share Posted February 4, 2012 edit: useless comment Quote Link to comment https://forums.phpfreaks.com/topic/256391-mysterious-numbers-appear-when-returning-the-result/#findComment-1314440 Share on other sites More sharing options...
Niixie Posted February 4, 2012 Author Share Posted February 4, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/256391-mysterious-numbers-appear-when-returning-the-result/#findComment-1314441 Share on other sites More sharing options...
darkfreaks Posted February 4, 2012 Share Posted February 4, 2012 have you tried using mysql_fetch_array instead of mysql_fetch_row Quote Link to comment https://forums.phpfreaks.com/topic/256391-mysterious-numbers-appear-when-returning-the-result/#findComment-1314443 Share on other sites More sharing options...
Niixie Posted February 4, 2012 Author Share Posted February 4, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/256391-mysterious-numbers-appear-when-returning-the-result/#findComment-1314444 Share on other sites More sharing options...
darkfreaks Posted February 4, 2012 Share Posted February 4, 2012 if you are returning ZERO or ONE why not use mysql_fetch_array($variable, MYSQL_NUM); Quote Link to comment https://forums.phpfreaks.com/topic/256391-mysterious-numbers-appear-when-returning-the-result/#findComment-1314445 Share on other sites More sharing options...
Niixie Posted February 4, 2012 Author Share Posted February 4, 2012 The system should return the value thats equal to the value from the database? Quote Link to comment https://forums.phpfreaks.com/topic/256391-mysterious-numbers-appear-when-returning-the-result/#findComment-1314450 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.