PHP output from database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
delk1ch
Forum Newbie
Posts: 1
Joined: Sun Nov 08, 2015 5:33 am

PHP output from database

Post 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’);

?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP output from database

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: PHP output from database

Post 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.
(#10850)
Post Reply