-1

I have a problem with my solution, the result of my while statement doesn't show up.

I have example of this:

function my_function(){

    $str = '<div class="my_div">';

    $con = mysqli_connect('host','user','password','database');

    $sql = "SELECT cola, colb FROM table";
    $sql_result = mysqli_query($con, $sql) or die(mysqli_error($con));

    while($row = mysqli_fetch_assoc($sql_result)){
        $vara = $row['cola'];
        $varb = $row['colb'];
        ...

        $str .= 'My text:'.$vara.$varb.'';
    }

    $str .= '</div>';

    return $str;
    mysqli_close($con);

}

As result I got only:

<div class="my_div"></div>

Why? How can I display the data from database (MySQL)?

Thanks for help!

1 Answers1

2

Your table name is a reserved word in MySQL.

From,

$sql = "SELECT cola, colb FROM table";

To,

$sql = "SELECT cola, colb FROM `table`";

Notice the back ticks.

Reading Material

https://dev.mysql.com/doc/refman/5.5/en/keywords.html

Script47
  • 14,230
  • 4
  • 45
  • 66