How to display following using while loop :

<div style="background-color:orange;text-align:center">
<p>Header Section</p>
</div>
<div style="border:1px solid black">
<p>Content section</p>
</div>

I want to display all my database in above manner, It gives Parse error

I want to fetch my database using while like :
for 1st record
<div style="background-color:orange;text-align:center">
<p>Header Section For 1st record</p>
</div>
<div style="border:1px solid black">
<p>Content section</p>
</div>

2nd record
<div style="background-color:orange;text-align:center">
<p>Header Section For 2nd record</p>
</div>
<div style="border:1px solid black">
<p>Content section</p>
</div>

Dani AI

Generated

There are two separate problems in the thread: mixing APIs (old mysql_* vs mysqli) and quoting/concatenation mistakes that produce parse errors. switched to mysqli but the loop examples still used mysql-style functions, which is why PHP complained “mysql_fetch_assoc() expects parameter 1 to be resource, object given.” was right to point out the missing query, but the real fix is to use one API consistently (prefer mysqli or PDO) and escape output to avoid HTML/XSS issues.

A compact, safe pattern using mysqli prepared statements and bound results — which avoids the resource/object mismatch and keeps HTML quoting simple — looks like this:

<?php
$mysqli = new mysqli('localhost','user','pass','dbname');
$sql   = "SELECT header, content FROM posts ORDER BY id";
if ($stmt = $mysqli->prepare($sql)) {
    $stmt->execute();
    $stmt->bind_result($header, $content);
    while ($stmt->fetch()) {
        echo '<div style="background-color:orange;text-align:center"><p>'
             . htmlspecialchars($header) . '</p></div>';
        echo '<div style="border:1px solid black"><p>'
             . nl2br(htmlspecialchars($content)) . '</p></div>';
    }
    $stmt->close();
}
$mysqli->close();
?>

Troubleshooting checklist: 1) Don’t mix mysql_* and mysqli — use one API. 2) If you still get parse errors, check for unmatched quotes, missing semicolons, or stray HTML inside single-quoted PHP strings; an alternative is to close PHP and write the HTML block between loop iterations to avoid complex string concatenation. 3) Always escape output with htmlspecialchars() (and nl2br() for stored newlines). 4) Add simple error checks like if (!$result) die($mysqli->error) or var_dump($result) when debugging. These steps will remove the warnings and prevent common security/formatting issues.

Recommended Answers

All 5 Replies

<?php
$heading=array(0=>'Heading 1',1=>'heading 2');
for ($i=0;$row=mysql_fetch_assoc($result);$i++) {
echo '<div style="background-color:orange;text-align:center">
<p>'.$heading[$i].'</p>
</div>
<div style="border:1px solid black">
<p>Content section</p>
</div>';
}
<?php
$heading=array(0=>'Heading 1',1=>'heading 2');
for ($i=0;$row=mysql_fetch_assoc($result);$i++) {
echo '<div style="background-color:orange;text-align:center">
<p>'.$heading[$i].'</p>
</div>
<div style="border:1px solid black">
<p>Content section</p>
</div>';
}

I receive for yor code

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, object given

I receive for yor code

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, object given

That was because you were meant to add the rest of the code which included a mysql query. $result is assigned to $result=mysql_query('SELECT blah...

<?php
$heading=array(0=>'Heading 1',1=>'heading 2');
for ($i=0;$row=mysql_fetch_assoc($result);$i++) {
echo '<div style="background-color:orange;text-align:center">
<p>'.$heading[$i].'</p>
</div>
<div style="border:1px solid black">
<p>Content section</p>
</div>';
}

Here is my code to fetch from database

<?php
$mysqli = new mysqli("localhost", "hrushi", "123456", "testlist");

// Create the query
$query = "SELECT sku, name, price FROM products ORDER by name";

// Send the query to MySQL
$result = $mysqli->query($query, MYSQLI_STORE_RESULT);
// Iterate through the result set
while(list($sku, $name, $price) = $result->fetch_row())
printf("(%s) %s: \$%s <br />", $sku, $name, $price);
?>

I'm not very familiar with mysqli that is with the i at the end but I will give you an example that doesn't use mysql.

<?php
$heading=array(0=>'Heading 1',1=>'heading 2');
for ($i=0;$i<count($heading);$i++) {
echo '<div style="background-color:orange;text-align:center">
<p>'.$heading[$i].'</p>
</div>
<div style="border:1px solid black">
<p>Content section</p>
</div>';
}

That should give you something to base your code on.

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.