michelle
michelle

Reputation: 653

Illegal string offset Undefined offset: 2

I'm new to php\mysql and I'm having issuing querying my database and formulating a top 10 list of users arranged by the column 'points'.

The error: Illegal string offset 'username' Illegal string offset 'points' Illegal string offset 'username' Illegal string offset 'points' Undefined offset: 2 Undefined offset: 2 Undefined offset: 3 Undefined offset: 3 Points: c c points1 1 points points points

My current mysql query:

$result = mysql_query("SELECT username, points FROM users ORDER BY points DESC LIMIT 10")
or die(mysql_error());

$rows=mysql_fetch_array($result);
$top10 = "Points: ";
for($i=0;$i<count($rows);$i++)
{
$top10 .= $rows[$i]['points']." ".$rows[$i]['username']." points";
}
echo $top10;

Upvotes: 0

Views: 284

Answers (3)

Pinu
Pinu

Reputation: 412

$result = mysql_query("SELECT username, points FROM users ORDER BY points DESC LIMIT 10")
or die(mysql_error());
$top10 = "Points: ";
while ($row = mysql_fetch_array($result)) {
    $top10 .= $row[0]." ".$row[1]." points";
}
echo $top10;

Upvotes: 1

Matin Rahman
Matin Rahman

Reputation: 1

$row = mysql_fetch_array($result, MYSQL_NUM);

In this line you are assigning the first row of the data array to $row.

you can check the structure of $row by the function print_r. Try print_r($row).

To access a column in that row use the db table's column name as index. like $row['ColumnName']; if you print this with "echo $row['column name']" you will get the value of the column that has been fetched.

Upvotes: 0

Tyranicangel
Tyranicangel

Reputation: 189

This will be your answer

while($row=mysql_fetch_array($result,MYSQL_ASSOC))
{
    echo $row['username'];
    echo $row['points'];
}

The same using fetch_all

$rows=mysql_fetch_all($result);
for($i=0;$i<count($rows);$i++)
{
    echo $rows[$i]['username'];
    echo $rows[$i]['points'];
}

Upvotes: 0

Related Questions