Jump to content
Andreycon

How to print out multiple rows from database

Recommended Posts

Let's assume this is my table

Name           Amount            Gender

Stone Cold.  1245.                 Male

Kingsley.       500                    Male.

Stone Cold   2367                  Male

Stone Cold.  5678.                  Male

Now I want to print stone cold rows. Which is row 1,3 and 4 just exactly the same format with he above table. How do I get to do that.

My code is   

<?php
                       $id = $_SESSION['login'];
                        $sqlB = "SELECT * FROM activities WHERE username=? ORDER BY No  DESC LIMIT 10";
$stmtB = $connection->prepare($sqlB); 
$stmtB->bind_param('s', $id);
$stmtB->execute();
$resultB = $stmtB->get_result();

while($rowB = $resultB->fetch_all(MYSQLI_ASSOC)){
 foreach ($rowB as $out) {
 
    echo implode(' ', $out) . "<br>";
    exit;
}
    }
     //print_r($rowB);

// exit; 
 ?>

 

 

I don't understand how to put each row for stone cold into HTML row. Please don't be annoyed by my question, I know its a simple stuff but I've tried several stuffs, I can't get what I need still. If I print $rowB I get all want from the database in arrays. Thanks!!!

Share this post


Link to post
Share on other sites
Posted (edited)

Add:

AND name='Stone Cold'

to your WHERE clause in the query. Assuming 'name' is that column name.

Edited by gw1500se

Share this post


Link to post
Share on other sites

With mysqli, when you use prepare() you get a statement object.

The fetch_all() method you are using is a mysqli result object method (You get a result object when you use mysqli query() ).

mysqli was built by two teams of developers who never spoke to one another, and so you get one set of methods for statements and a completely different set for results.

Use PDO and this mess goes away.

  • Like 1

Share this post


Link to post
Share on other sites
Posted (edited)
20 minutes ago, gw1500se said:

Add:

AND name='Stone Cold'

to your WHERE clause in the query. Assuming 'name' is that column name.

The $id is its name. So am indexing it with the name. It finds it but only output the first row but if I print $rowB I get all the rows for stone cold in arrays though

Edited by Andreycon

Share this post


Link to post
Share on other sites
Posted (edited)

You don't need the 'while'. Do the 'fetchall' once then run your 'for' loop. Anyway, I agree with Barand, PDO is a better choice.

Edited by gw1500se

Share this post


Link to post
Share on other sites

Tanx all. I got an approach which now works

 

echo "<table>"; foreach ($rowB as $row) { echo "<tr>"; foreach ($row as $column) { echo "<td>$column</td>"; } echo "</tr>"; } echo "</table>";

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.