Page 1 of 1

SQL results won't be printed.

Posted: Tue Oct 13, 2009 1:44 pm
by mrpaplu
Hello there,

I'm currently building a website for our gaming clan. I began creating the code for index.php. On index.php all of the news should be printed. The news should come from my SQL Database containing the following:

members(member_nr, e-mail, username, etc....)
news(news_nr, member_nr, title, content, date, time)

I allready added some members and a news-item. But when I trie to print all of the news-items, the code gives no results. Here's my code:

Code: Select all

<?php 
    session_start();
    include("include/pageheader.php");
    $include_add = false;
    $bewerk = "";
    if($_SESSION['function'] == "admin"){
        $include_add = true;
        $bewerk =  "<a href=\"edit_news.php\">Bewerk artikel</a>";
    }
    
    //actual printing
    print("<div class=\"pagetitle\">[PDM]Home</div>");
    include("include/connect.php");
   [color=#FF0000] $query = "SELECT news.title, news.content, news.date, members.gamertag FROM news [/color]
             [color=#FF0000]INNER JOIN members ON news.member_nr = members.member_nr";[/color]
    $result = mysql_query($query);
    while(mysql_fetch_array($result)){
        print("<div class=\"news\">");
        print("<div class=\"title\">".$row['title']."</div>");
        print("<div class=\"content\">".$row['content']."</div>");
        print("<div class=\"date\">".$row['date']." by [PDM]".$row['gamertag']." ".$bewerk);
    }
    print("</div>");
    if($include_add){
        include("include/menu_home.php");
    }
    include("include/pagefooter.php"); 
?>
I'm affraid the problem is my query, but I don't know for sure, so I just posted my problem in PHP - Code. I don't know a lot about using the JOIN, so the problem might be in there.

So could anyone help me?

Gr. Mr.Paplu

Re: SQL results won't be printed.

Posted: Tue Oct 13, 2009 1:53 pm
by PHPHorizons
Hello mrpaplu,,

It looks like the while loop condition is a bit off.
while(mysql_fetch_array($result)){
Change the code above to:

while($row = mysql_fetch_array($result)){
Hope that helps.

Re: SQL results won't be printed.

Posted: Tue Oct 13, 2009 1:56 pm
by mrpaplu
Nice, thanks for the fast reply.
Indeed this solved my problem, thank you.

Gr. Mr.Paplu

Re: SQL results won't be printed.

Posted: Tue Oct 13, 2009 2:01 pm
by PHPHorizons
You're welcome ;)