Page 1 of 1

PHP output from database

Posted: Sun Nov 08, 2015 5:40 am
by delk1ch
[QUOTE=delk1ch;1480607]Hello,
I'm trying to output my database on my website but it's not working, it shows up just a blank page ( IMAGE ), here's the code :

Code: Select all

<html>
  <head>
    <title>Untitled</title>
  </head>
  <body>
<?php

require_once(‘conn.php’);

$sql = ("SELECT * FROM oglasi");

$query = mysql_query($sql);

while ($row = mysql_fetch_array($query)) {

$naziv = $row[‘naziv’];
$sadrzaj = $row[‘sadrzaj’];

echo $naziv;

echo $sadrzaj;

};

?>
  </body>
</html>
And conn.php is:

Code: Select all

<?php

$conn = mysql_connect(‘localhost’, ‘root’, ‘ELsk0l@r00t’)
or die (mysql_error());

mysql_select_db(‘elskoglasi’)
or die (‘Database selection incorrect’);

?>

Re: PHP output from database

Posted: Sun Nov 08, 2015 7:20 am
by Celauran
Is error reporting turned on? That's step one. It looks like you have smart quotes in there instead of normal single/double quotes, so I suspect your require_once statement fails, throws a fatal error, and displays nothing because error reporting is off.

Re: PHP output from database

Posted: Sun Nov 08, 2015 4:08 pm
by Christopher
It is good to wrap your database queries with some error checking:

Code: Select all

$sql = "SELECT * FROM oglasi";     // no parens needed

$result= mysql_query($sql);
if (! mysql_error($result)) {
    while ($row = mysql_fetch_array($query)) {
        $naziv = $row[‘naziv’];
        $sadrzaj = $row[‘sadrzaj’];
        echo $naziv;
        echo $sadrzaj;
    }
    $errmsg = '';
} else {
    $errmsg = mysql_errmsg($result);
}
Also, please use the Mysqli extension. The Mysql extension is no longer supported.