I dont know why this code will not show any data from the mysql.........................

$query = "SELECT * FROM g_attack WHERE winner ='$username' ORDER BY created ASC LIMIT 10"; 
$row = mysql_query($query) or die("Could not connect to db"); 
while ( $result = mysql_fetch_assoc($row)) { 
$created = $row['created'];
$looser = $row['looser'];
$stolengold = $row['stolengold'];
 
echo '<tr>'; 
echo '<td>'.$created.'</td><td>You won over <b>'.$looser.'</b> and stole <b>'.$stolengold.'</b></td>';
echo '</tr>';

}

the database is connected, it is data in the database, but it just dont wanna get any info from it....

Dani AI

Generated

Two quick, practical reminders that help avoid this kind of bug and make the code more robust and future-proof.

  1. Use the fetched row variable, not the query resource. In your loop you must read columns from the associative array returned by mysql_fetch_assoc, e.g. $created = $result['created']; not $row['created'];. That was the actual cause here.

  2. Add simple error checks so failures are visible. Replace

    $row = mysql_query($query) or die("Could not connect to db");

    with a check that shows the real MySQL error:

    $result = mysql_query($query);
    if (! $result) {
     die('MySQL error: ' . mysql_error());
    }
  3. Consider updating code now. The mysql_* extension is removed in modern PHP. For new code, use mysqli or PDO with prepared statements to avoid SQL injection and gain clearer APIs. Example migration paths:

  • mysqli (procedural or object-oriented)
  • PDO with prepared statements

These steps prevent silent mistakes, improve debugging, and make the code safer and maintainable.

Recommended Answers

All 3 Replies

Change row to result on line 2 and swap the two variables on line 3.

you did a silly mistake in your code....
check your line 4,5,6
replace it with

$created = $result['created'];
$looser = $result['looser'];
$stolengold = $result['stolengold'];

So the complete code will be....

$query = "SELECT * FROM g_attack WHERE winner ='$username' ORDER BY created ASC LIMIT 10"; 
$row = mysql_query($query) or die("Could not connect to db"); 
while ( $result = mysql_fetch_assoc($row)) { 
$created = $result['created'];
$looser = $result['looser'];
$stolengold = $result['stolengold'];
 
echo '<tr>'; 
echo '<td>'.$created.'</td><td>You won over <b>'.$looser.'</b> and stole <b>'.$stolengold.'</b></td>';
echo '</tr>';
 
}

PS-If your problem is solved mark the thread as solved

yeah i saw it like 1 hour after i posted it, but really, thanks for help. When you're "wired-in" its easy to write quick and deep but easier to do small mistakes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.