unistake Posted September 10, 2009 Share Posted September 10, 2009 Hi all, I have the following code that I am trying to get to echo one full row from a mysql database. For some reason the code works, however it shows the same result twice. I only want it to be displayed once! Do you know where I have gone wrong? Thanks <?php $dbhost = "localhost"; $dbuser = "user"; $dbpassword = "passsword"; $db = "db"; $con = mysql_connect($dbhost, $dbuser, $dbpassword) or die ("cant connect with $dbuser"); mysql_select_db("$db"); $sql = "SELECT bookmaker FROM bets WHERE loginName = 'unistake'"; $result = mysql_query($sql) or die ("cant do query!"); while($row = mysql_fetch_array($result)) { $used = $row['bookmaker']; $sql2 = "SELECT * FROM bookmakers WHERE bookmaker !='$used' ORDER BY id DESC LIMIT 1" or die ("cant find query2"); $result2 = mysql_query($sql2) or die ("cant do query2"); while($row2 = mysql_fetch_array($result2)) { extract($row2); echo "id, $bookmaker, $totalbonus, $freebet, sr= $sr, pf= $pf, $minodds, $details, <a href=$link>this is the link</a>"; } } ?> Link to comment https://forums.phpfreaks.com/topic/173787-solved-php-is-echoing-the-same-result-twice-from-mysql/ Share on other sites More sharing options...
Bricktop Posted September 10, 2009 Share Posted September 10, 2009 Hi unistake, You have a while loop nested inside another while loop so in effect the nested while loop twice. Remove the second while loop and try again, like: <?php $dbhost = "localhost"; $dbuser = "user"; $dbpassword = "passsword"; $db = "db"; $con = mysql_connect($dbhost, $dbuser, $dbpassword) or die ("cant connect with $dbuser"); mysql_select_db("$db"); $sql = "SELECT bookmaker FROM bets WHERE loginName = 'unistake'"; $result = mysql_query($sql) or die ("cant do query!"); while($row = mysql_fetch_array($result)) { $used = $row['bookmaker']; $sql2 = "SELECT * FROM bookmakers WHERE bookmaker !='$used' ORDER BY id DESC LIMIT 1" or die ("cant find query2"); $result2 = mysql_query($sql2) or die ("cant do query2"); extract($row2); echo "id, $bookmaker, $totalbonus, $freebet, sr= $sr, pf= $pf, $minodds, $details, <a href=$link>this is the link</a>"; } ?> Hope this helps! Link to comment https://forums.phpfreaks.com/topic/173787-solved-php-is-echoing-the-same-result-twice-from-mysql/#findComment-916079 Share on other sites More sharing options...
unistake Posted September 10, 2009 Author Share Posted September 10, 2009 Thanks for the help Bricktop, I have tried the code below revised with only one while statement and is still repeats it twice <?php $con = mysql_connect($dbhost, $dbuser, $dbpassword) or die ("cant connect with $dbuser"); mysql_select_db("$db"); $sql = "SELECT bookmaker FROM bets WHERE loginName = 'cudster'"; $result = mysql_query($sql) or die ("cant do query!"); while($row = mysql_fetch_array($result)) { $used = $row['bookmaker']; $sql2 = "SELECT * FROM bookmakers WHERE bookmaker !='$used' ORDER BY id DESC LIMIT 1" or die ("cant find query2"); $result2 = mysql_query($sql2) or die ("cant do query2"); $row2 = mysql_fetch_array($result2); extract($row2); echo "$id, $bookmaker, $totalbonus, $freebet, sr= $sr, pf= $pf, $minodds, $details, <a href=$link>this is the link</a>"; } ?> Link to comment https://forums.phpfreaks.com/topic/173787-solved-php-is-echoing-the-same-result-twice-from-mysql/#findComment-916084 Share on other sites More sharing options...
Bricktop Posted September 10, 2009 Share Posted September 10, 2009 If you echo $used do you get two results? If so, your initial query is pulling two results from the database. You might need to add LIMIT 1 to the end of the first database query. Link to comment https://forums.phpfreaks.com/topic/173787-solved-php-is-echoing-the-same-result-twice-from-mysql/#findComment-916089 Share on other sites More sharing options...
unistake Posted September 10, 2009 Author Share Posted September 10, 2009 you were totally right, it $used was displaying two results and using LIMIT 1 in the first query sorted it out. Do you know why it would pull two results from a mysql table with one row like that? Thanks for the help Link to comment https://forums.phpfreaks.com/topic/173787-solved-php-is-echoing-the-same-result-twice-from-mysql/#findComment-916091 Share on other sites More sharing options...
unistake Posted September 10, 2009 Author Share Posted September 10, 2009 ah actually, that works fine but... I am trying to end up displaying the next bookmaker (row) from the table bookmakers where the bookmaker is not recorded in the 'bets' table. Therefore I dont think I can limit the first query to LIMIT 1 can I? Link to comment https://forums.phpfreaks.com/topic/173787-solved-php-is-echoing-the-same-result-twice-from-mysql/#findComment-916094 Share on other sites More sharing options...
unistake Posted September 10, 2009 Author Share Posted September 10, 2009 got it working, I just needed to turn around the DESC to ASC and it works fine. thanks for the help Link to comment https://forums.phpfreaks.com/topic/173787-solved-php-is-echoing-the-same-result-twice-from-mysql/#findComment-916097 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.