M.Mihaylov
M.Mihaylov

Reputation: 43

Print results from mysql query in table

I need to show some results in table, but the query data shows up to the table. Here is the code:

    $db = new mysqli('localhost','root','123','news');
if($db->connect_errno > 0){
    die('Unable to connect to database [' . $db->connect_error . ']');
}
$sql = <<<SQL
            SELECT Id FROM news
SQL;
    $result=mysqli_query($db,$sql);
    print "<table><tr><td>№</td></tr>";
    while($row = mysqli_fetch_row($result)){
        print '<tr><td>'+$row[0]+'</td></tr>';
    }
    print "</table>";

The result is like this:

1234
№

It should look like this:

№
1
2
3
4

Upvotes: 0

Views: 140

Answers (3)

Jeffrey Goudzwaard
Jeffrey Goudzwaard

Reputation: 103

Replace the "+" with "." The "+" is used to add values, the "." is used to concatenate strings

$db = new mysqli('localhost','root','123','news');
if($db->connect_errno > 0){
    die('Unable to connect to database [' . $db->connect_error . ']');
}
$sql = <<<SQL
            SELECT Id FROM news
SQL;
    $result=mysqli_query($db,$sql);
    print "<table><tr><td>№</td></tr>";
    while($row = mysqli_fetch_row($result)){
        print '<tr><td>' . $row[0] . '</td></tr>';
    }
    print "</table>";

or use the following

print "<tr><td>$row[0]</td></tr>";

Upvotes: 1

user8791572
user8791572

Reputation:

Use . Instead of + and use echo instead of print

Upvotes: 1

ANdy
ANdy

Reputation: 11

print '<tr><td>'+$row[0]+\n'</td></tr>';

Upvotes: -1

Related Questions