0

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

4 Answers 4

4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

arrays in php may also be defined with curly braces. this is not a problem.
Milen, try running this code: <?php $arr = array('test' => 'a test','another test2' => 'test',); echo $arr['test']."\n"; $arr{'test3'} = 'third element'; var_export($arr); echo "\n"; $arr2 = array('test1','test2','test3'); echo $arr{2}."\n"; array_push($arr2,'test4'); var_export($arr2); ?>
the only problem is that you can't define $arr{} = 'value'
3

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.

Comments

1

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>

Comments

0

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>';
 ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.