Jump to content

[SOLVED] php is echoing the same result twice from mysql?!


unistake

Recommended Posts

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>";
}
}
?>

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!

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>";

}
?>

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?

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.