Jump to content

[SOLVED] while(array) cross-querying problem


adrian5117

Recommended Posts

My problem is that my script, now undergoing editing to support 3 different 'bets' (its for a slot machine) simply will not return the number of $coin a player has from the database.

 

At the end of the script, I used a foreach to display the $_SESSION values, but $money always comes up blank.

 

The entire script as follows, and below it, my original which works fine. (Orginal has only 1 $bet of 5)

 

<?
session_start();
?>
<?

include '../style.php';
include "../ip.php";

mysql_connect("$dbhost", "$dbuser", "$dbpass")
or die("<body scroll=no><font color=red><center>Error connecting to mysql.</font><br><button onclick=top.location='http://www.l2-x.co.nr/game'>OK</button>");

mysql_select_db("$dbname")
or die ("<body scroll=no><font color=red><center>Error connecting to Database.</font><br><button onclick=top.location='http://www.l2-x.co.nr/game'>OK</button>");

$user = mysql_real_escape_string($_POST["name"]);
$pass = mysql_real_escape_string($_POST["pass"]);
$spass = mysql_real_escape_string($_POST["pass"]);

$coin = 57; // item to use
$beta = 25; // wager 1
$betb = 50; // wager 2
$betc = 100; // wager 3

$idq = mysql_query("SELECT * FROM `accounts` WHERE `login` = '$user'");
$idr = mysql_num_rows($idq);

if($idr == '0') {
session_destroy();
die ("<body scroll=no><font color=red><center>ERROR!<br>Account does not exist.<br><button onclick=top.location='http://www.l2-x.co.nr/game'>OK</button>");
}

while ($ida = mysql_fetch_array($idq))
{

$spass = base64_encode(pack('H*', sha1($pass)));
if($ida[password] != $spass) {
session_destroy();
die ("<body scroll=no><font color=red><center>ERROR!<br>Login/password combination does not match.<br><button onclick=top.location='http://www.l2-x.co.nr/game'>OK</button>");
}

$result = mysql_query("SELECT `account_name`,`char_name`,`online`,`sex` FROM `characters` WHERE `account_name` = '$user' ORDER BY `account_name` ASC");
$row = mysql_num_rows($result);

if($row == 0) {
session_destroy();
die ("<body scroll=no><font color=red><center>ERROR!<br>No characters exist on this account.<br><button onclick=top.location='http://www.l2-x.co.nr/game'>OK</button>");
}

$oresult = mysql_query("SELECT `online` FROM `characters` WHERE `online` = '0' AND `account_name` = '$user'");
$orow = mysql_num_rows($oresult);

if($orow == 0) {
session_destroy();
die ("<body scroll=no><font color=red><center>ERROR!<br>In order to use this function, your characters cannot be playing on the server.<br><button onclick=top.location='http://www.l2-x.co.nr/game'>OK</button>");
}

$resultid = mysql_query("SELECT `account_name`,`char_name`,`charId`,`online`,`accesslevel` FROM `characters` WHERE `account_name` = '$user' AND `online` = '0' AND `accesslevel` = '0' ORDER BY `account_name` ASC, `char_name` ASC");
$idrow = mysql_num_rows($resultid);

if($idrow == 0) {
session_destroy();
die ("<body scroll=no><font color=red><center>ERROR!<br>GM characters cannot use this.<br><button onclick=top.location='http://www.l2-x.co.nr/game'>OK</button>");
}

$coins = @mysql_query("SELECT `count` FROM `items` WHERE `item_id` = '$coin' AND `owner_id` = '$rowid[2]'");
$coinrow = @mysql_num_rows($coins);

echo "<table border='1'>";
echo "<tr>";
echo "<th class='Stil5' align=center>Character</th>";
echo "<th class='Stil5' align=center>Coins</th>";
echo "<th class='Stil5' colspan=3 align=center>Cost</th>";
echo "</tr>";

while ($rowid = mysql_fetch_array($resultid))
{

$ownername = $rowid[1];
$ownerid = $rowid[2];

echo "<tr>";
echo "<td class='Stil5' align=center>$rowid[1]</td>";

if($coinrow == 0) {
$coina[3] = 0;
$neg = ' style=background-color:red';
}

while ($coina = @mysql_fetch_row($coins))
{
$money = $coina;
}

$cashc = number_format($money, 0, ".", ",");

echo "<td class='Stil5' align=center$neg>$cashc</td>";

if($money < $beta) {
$play1 = "<button hidefocus disabled style=background-color:FF0000>$beta</button>";
} else {
$play1 = "<input type=submit value=\"$beta\" hidefocus style=background-color:00FF00><input type=hidden value=$beta name=bet>";
}

if($money < $betb) {
$play2 = "<button hidefocus disabled style=background-color:FF0000>$betb</button>";
} else {
$play2 = "<input type=submit value=\"$betb\" hidefocus style=background-color:00FF00><input type=hidden value=$betb name=bet>";
}

if($money < $betc) {
$play3 = "<button hidefocus disabled style=background-color:FF0000>$betc</button>";
} else {
$play3 = "<input type=submit value=\"$betc\" hidefocus style=background-color:00FF00><input type=hidden value=$betc name=bet>";
}

echo "<td align=center><form style=margin-bottom:0 action=game.php method=post name=$rowid[1]>$play1</td>";
echo "<input type=hidden value=$money name=money>";
echo "<input type=hidden value=$coin name=coin>";
echo "<input type=hidden value=$rowid[1] name=ownername>";
echo "<input type=hidden value=$rowid[2] name=ownerid>";
echo "<input type=hidden value=$user name=name>";
echo "<input type=hidden value=$pass name=pass>";
echo "</form>";

echo "<td align=center><form style=margin-bottom:0 action=game.php method=post name=$rowid[1]>$play2</td>";
echo "<input type=hidden value=$money name=money>";
echo "<input type=hidden value=$coin name=coin>";
echo "<input type=hidden value=$rowid[1] name=ownername>";
echo "<input type=hidden value=$rowid[2] name=ownerid>";
echo "<input type=hidden value=$user name=name>";
echo "<input type=hidden value=$pass name=pass>";
echo "</form>";

echo "<td align=center><form style=margin-bottom:0 action=game.php method=post name=$rowid[1]>$play3</td>";
echo "<input type=hidden value=$money name=money>";
echo "<input type=hidden value=$coin name=coin>";
echo "<input type=hidden value=$rowid[1] name=ownername>";
echo "<input type=hidden value=$rowid[2] name=ownerid>";
echo "<input type=hidden value=$user name=name>";
echo "<input type=hidden value=$pass name=pass>";
echo "</form>";

}

echo "</tr>";

}

echo "</table>";

$_SESSION['coin'] = $coin;
$_SESSION['money'] = $money;
$_SESSION['ownername'] = $ownername;
$_SESSION['ownerid'] = $ownerid;
$_SESSION['beta'] = $beta;
$_SESSION['betb'] = $betb;
$_SESSION['betc'] = $betc;
$_SESSION['name'] = $user;
$_SESSION['pass'] = $pass;

foreach($_SESSION as $item => $val)
{
print "<font color=white>Item: ".$item." => ".$val."<br>";
}

mysql_close();

?>

 

 

 

 

 

And my original:

 

<?
session_start();
?>
<?

include '../style.php';
include "../ip.php";

mysql_connect("$dbhost", "$dbuser", "$dbpass")
or die("<body scroll=no><font color=red><center>Error connecting to mysql.</font><br><button onclick=top.location='http://www.l2-x.co.nr/game'>OK</button>");

mysql_select_db("$dbname")
or die ("<body scroll=no><font color=red><center>Error connecting to Database.</font><br><button onclick=top.location='http://www.l2-x.co.nr/game'>OK</button>");

$user = mysql_real_escape_string($_POST["name"]);
$pass = mysql_real_escape_string($_POST["pass"]);
$spass = mysql_real_escape_string($_POST["pass"]);

$coin = 57; // item to use
$bet = 5; // wager

$idq = mysql_query("SELECT * FROM `accounts` WHERE `login` = '$user'");
$idr = mysql_num_rows($idq);

if($idr == '0')
{
session_destroy();
die ("<body scroll=no><font color=red><center>ERROR!<br>Account does not exist.<br><button onclick=top.location='http://www.l2-x.co.nr/game'>OK</button>");
}

while ($ida = mysql_fetch_array($idq))
{

$spass = base64_encode(pack('H*', sha1($pass)));
if($ida[password] != $spass)
{
session_destroy();
die ("<body scroll=no><font color=red><center>ERROR!<br>Login/password combination does not match.<br><button onclick=top.location='http://www.l2-x.co.nr/game'>OK</button>");
}

$result = mysql_query("SELECT `account_name`,`char_name`,`online` FROM `characters` WHERE `account_name` = '$user' ORDER BY `account_name` ASC");
$row = mysql_num_rows($result);

if($row == 0)
{
session_destroy();
die ("<body scroll=no><font color=red><center>ERROR!<br>No characters exist on this account.<br><button onclick=top.location='http://www.l2-x.co.nr/game'>OK</button>");
}

$oresult = mysql_query("SELECT `online` FROM `characters` WHERE `online` = '0' AND `account_name` = '$user'");
$orow = mysql_num_rows($oresult);

if($orow == 0)
{
session_destroy();
die ("<body scroll=no><font color=red><center>ERROR!<br>In order to use this function, your characters cannot be playing on the server.<br><button onclick=top.location='http://www.l2-x.co.nr/game'>OK</button>");
}

$resultid = mysql_query("SELECT `account_name`,`char_name`,`charId`,`online`,`accesslevel` FROM `characters` WHERE `account_name` = '$user' AND `online` = '0' AND `accesslevel` = '0' ORDER BY `account_name` ASC, `char_name` ASC");
$idrow = mysql_num_rows($resultid);

if($idrow == 0)
{
session_destroy();
die ("<body scroll=no><font color=red><center>ERROR!<br>GM characters cannot use this.<br><button onclick=top.location='http://www.l2-x.co.nr/game'>OK</button>");
}

echo "<table border='1'>";
echo "<tr>";
echo "<th class='Stil5' align=center>Character</th>";
echo "<th class='Stil5' align=center>Coins</th>";
echo "<th class='Stil5' align=center>Cost</th>";
echo "</tr>";
echo "<tr>";

while ($rowid = mysql_fetch_array($resultid))
{

$ownername = $rowid[1];
$ownerid = $rowid[2];

echo "<td class='Stil5' align=center>$rowid[1]</td>";

$coins = @mysql_query("SELECT `count` FROM `items` WHERE `item_id` = $coin AND `owner_id` = '$rowid[2]'");
$coinrow = @mysql_num_rows($coins);

while ($coina = @mysql_fetch_array($coins))
{

$money = $coina[0];

$cashc = number_format($coina[0], 0, ".", ",");

if($money < $bet)
{
$play = "<button hidefocus disabled style=background-color:FF0000>$bet Coins</button>";
} else {
$play = "<input type=submit value=\"$bet Coins\" hidefocus style=background-color:00FF00>";
}

}

echo "<td class='Stil5' align=center>$cashc</td><td align=center><form style=margin-bottom:0 action=play.php method=post name=$rowid[1]>$play</td></tr>";

echo "<input type=hidden value=$money name=money>";
echo "<input type=hidden value=$coin name=coin>";
echo "<input type=hidden value=$rowid[1] name=ownername>";
echo "<input type=hidden value=$rowid[2] name=ownerid>";
echo "<input type=hidden value=$bet name=bet>";
echo "<input type=hidden value=$user name=name>";
echo "<input type=hidden value=$pass name=pass>";

echo "</form>";

}

echo "</table>";

}

$_SESSION['coin'] = $coin;
$_SESSION['money'] = $money;
$_SESSION['ownername'] = $ownername;
$_SESSION['ownerid'] = $ownerid;
$_SESSION['bet'] = $bet;
$_SESSION['name'] = $user;
$_SESSION['pass'] = $pass;

/*
foreach($_SESSION as $item => $val)
{
print "Item: ".$item." => ".$val." ";
}
*/

mysql_close();

?>

 

 

 

 

 

I suspect it is the order of the cascading while(array) loops, which I've used many times before. I tried using just $value = mysql_fetch_array($query), then simply assigning a $value[index], but I'm still new to php, believe it or not, and I'm not quite sure what the difference is, or if it will even process correctly.

 

Thanks for the help.

Link to comment
Share on other sites

For those interested in helping, I have solved this problem myself after much pondering and scrolling up and down the document.

 

The problem was on line 92, rowid[2] was referenced in a query before the while(array) for that query { rowid[...] } was processed. (Down near line 97 I think.)

 

Thanks for your time though!

:D

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.