Matthew Deloughry
Matthew Deloughry

Reputation: 302

php + mysql rows only returning int

Hi I'm starting out on php and I have the following script

<?php

$username = "firstleg_floa";
$password = "**";
$hostname = "***1";
$db = "firstleg_bank";
$guildUser = strtolower('grissiom');

$dbh = mysql_connect( $hostname, $username, $password) or die ("Unable To Connect");
$connectDB = mysql_select_db($db, $dbh);
$results = mysql_query("SELECT * FROM Bank where userId ='" .$guildUser."'");

$i = 0;
$rsArr = array();

While ($row = mysql_fetch_array($results, MYSQL_ASSOC))
{
    $rsArr [$i] [0] = $row{'userId'};
    $rsArr [$i] [1] = $row{'item'};
    $rsArr [$i] [2] = $row{'amount'};
    $rsArr [$i] [3] = $row{'position'};
    $i++;
}
?>
<?="ghdfdgdfg ". $rsArr[$i][1];}." ----";?>
    <? echo $rsArr[$i][0]; ?>
    <table>
    <tr><td>Item</td><td>Amount</td></tr>
    <?for ($x=0;$x <= $i; $x++)
    {?>
    <tr><td><?=$rsArr[$x][3];?></td><td><?=$rsArr[$x][2];?></td></tr>
    <?}?>
    </table>

<?php
mysql_close($dbh); ?>

and I have the following data in the database

1 - grissiom - Silk Cloth - 100 - 1

with the above data and the above script all I can manage to pull out is the amount(100) and the position(1) how come i am unable to pull anything else out? can someone help me please

Upvotes: 0

Views: 279

Answers (4)

karim79
karim79

Reputation: 342635

Possible solutions:

1) Those column names in the DB have different case, e.g. userId could be just userid

2) Those columns don't exist.

3) You are using curly braces, instead of square brackets:

 $rsArr [$i][0] = $row{'userId'}; //wrong
 $rsArr [$i] [0] = $row['userId']; //correct

Try replacing the while loop with this, it will read out the contents of each record as the DB has passed it to PHP:

while ($row = mysql_fetch_array($results, MYSQL_ASSOC))
{
    echo '<pre>' . var_dump($row) . </pre>;
}

to see exactly what is coming back from the DB.

Upvotes: 3

Ivan
Ivan

Reputation: 2262

after loop is finished $i is equal to 1, but $rsArr[$i] is not defined, so variables $rsArr[$i][0] and $rsArr[$i][1] doensn't exists, try using $rsArr[0][0] and $rsArr[0][1] like this:

<?="ghdfdgdfg ". $rsArr[0][1];}." ----";?>
    <? echo $rsArr[0][0]; ?>
    <table>
    <tr><td>Item</td><td>Amount</td></tr>
    <?for ($x=0;$x <= $i; $x++)
    {?>
    <tr><td><?=$rsArr[$x][3];?></td><td><?=$rsArr[$x][2];?></td></tr>
    <?}?>
    </table>

Upvotes: 1

Alekc
Alekc

Reputation: 4770

It seem ok to me, maybe issue is given from using curly braces.

Anyway i'd go with some simpler syntax, i.e:

<?php

$username = "firstleg_floa";
$password = "**";
$hostname = "***1";
$db = "firstleg_bank";
$guildUser = strtolower('grissiom');

$dbh = mysql_connect( $hostname, $username, $password) or die ("Unable To Connect");
$connectDB = mysql_select_db($db, $dbh);
$results = mysql_query("SELECT * FROM Bank where userId ='" .$guildUser."'");

$i = 0;
$rsArr = array();
while ($row = mysql_fetch_array($results, MYSQL_ASSOC))
{
    $rsArr[] = $row;
}
mysql_close($dbh);
echo '<table><tr><td>Item</td><td>Amount</td></tr>';
for ($i = 0;$i<count($rsArr);$i++){
    echo <<<EOF
    <tr><td>{$rsArr["item"]}</td><td>{$rsArr["amount"]}</td></tr>
EOF;
}
echo '</table>';
 ?>

Upvotes: 0

Jeremy DeGroot
Jeremy DeGroot

Reputation: 4506

It could be because you're using curly braces instead of square. The row returned by mysql_fetch_array() is a PHP array, not an object or string.

Upvotes: 4

Related Questions