Rakete1111 Rakete1111 - 2 months ago 11
MySQL Question

SQL values are 0, even though they exist

I'm trying to query some vars in my MySQL db, but for some reasons it returns 0.
Here's the code:

<html>
<body>
<?php
session_start();

$connection = mysqli_connect("", "", "", "");

if (empty($connection))
$error = "Could not connect to Database, please contact an admin with this code: DBE1";


$tnews = mysqli_query($connection, "SELECT * FROM Test ORDER BY ID DESC");

while ($news = mysqli_fetch_array($tnews))
{
$title = $news['Title'];
$pimg = $news['PrevImg'];
$ptext = $news['PrevText'];
$text = $news['Text'];
$img1 = $news['Img1'];

echo "<h1>" + $title + "</h1><br><br>";
echo "<p>" + $pimg + "</p>";
echo "<br><p>" + $ptext + "</p><br><br>";
echo "<p>" + $text + "</p>";
echo "<br><p>" + $img1 + "</p>";
echo $title;
}
mysqli_close($connection);

?>
</body>
</html>


Oh and the result
00000


I have another site where almost (var names, echo's, ...) everything is the same, it works there! I have really have no idea what's wrong, does anyone have an idea?

P.S: The DB Connection has empty strings because it has the ip, username, password and db of my database! ;D

Thanks for the help!

Answer

The + is a concatenate operator in JS, use PHP's equivalent, being a dot .

Now, as stated in comments, you are outputting content before sessions.

You may get a warning similar to this:

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /path/to/file:3) in /path/to/file.php on line 4

Corrected code, is you still want to use sessions. If not, just remove session_start();

Sidenote: It is important not to have a space before <?php or any other output such as HTML, a cookie or a byte order mark, which will also account as outputting before header.

<?php 
    session_start();
?>

<html>
<body>
<?php

    $connection = mysqli_connect("", "", "", "");

    if (empty($connection))
        $error = "Could not connect to Database, please contact an admin with this code: DBE1";


    $tnews = mysqli_query($connection, "SELECT * FROM Test ORDER BY ID DESC");

    while ($news = mysqli_fetch_array($tnews))
    {
        $title = $news['Title'];
        $pimg = $news['PrevImg'];
        $ptext = $news['PrevText'];
        $text = $news['Text'];
        $img1 = $news['Img1'];

        echo "<h1>" . $title . "</h1><br><br>";
        echo "<p>" . $pimg . "</p>";
        echo "<br><p>" . $ptext . "</p><br><br>";
        echo "<p>" . $text . "</p>";
        echo "<br><p>" . $img1 . "</p>";
        echo $title;
    }
    mysqli_close($connection);

?>
    </body>
</html>

Add error reporting to the top of your file(s) which will signal warnings/errors as shown further up in my answer.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Error reporting should only be done in staging, and never production.