1

I have a function loadContent() that when called, queries a database, formats the data and should return the result. This is being used to build a very primitive CMS and this function is used to load the articles from the database.

Here is the function is question:

    $articles = mysql_query("SELECT `id`, `name`, `content`, `keywords`, `description`, `access_level` FROM `tb_articles` WHERE `page_id`='$pageID'");
            if(!$articles){
                die('Error!: ' . mysql_error());
            }
            while($arts = mysql_fetch_assoc($articles)){
                $content .= '<div class="article" id="art-' . $arts['id'] . '">';
                $content .= '<h1>' . $arts['name'] . '</h1>';
                $content .= $arts['content'];
                $content .= '</div>';
            }
        return $content; 
    }else{

My problem is that nothing is being returned. The SQL is verified, it should return two rows. I have commented out everything in the loop and added the line $content .= 'test'; and changed the return statement to echo $content; and the function successfully echo'ed "test" twice. So whats wrong with what I have done?

Thanks for helping me out, I'm pretty new to PHP.

UPDATE:

The post now represents all valid changes.

I got somewhere with var_dump. I dumped $content just before the return statement. All of the articles in the database (2 of them) were displayed inside of string(2445) "". This confirms there are no problems with the query. I am now thinking it has something to do with the return statement.

14
  • I assume $arts is short for $articles, and in the real code one of those two variable names is being used consistently? Commented May 12, 2012 at 15:37
  • 1
    Try changing $content = to $content .= at the first row in your loop. Commented May 12, 2012 at 15:38
  • Ahhh... no, nevermind, I understand how it works. Commented May 12, 2012 at 15:38
  • Are you sure there is no mysql_error? Commented May 12, 2012 at 15:47
  • 1
    Are you actually using mysql_connect() elsewhere to connect to the DB? Commented May 12, 2012 at 15:48

4 Answers 4

3

Not sure if this is related, but it doesn't look like you ever close the first div tag. You don't close the id attribute quote either (opened with an apostrophe, and you concatenate a quote).

More specifically:

... id='art-" . $arts['id'] . "'></div>";
Sign up to request clarification or add additional context in comments.

1 Comment

You are right about this, I corrected these issues but unfortunately I didn't solve my problem. Thanks though.
1

Try updating your code to this:

function loadContent(){
   $content = '';
   $pageID = 1;
   //
   $articles = mysql_query("SELECT `id`, `name`, `content`, `keywords`, `description`, `access_level` 
                            FROM `tb_articles` 
                            WHERE `page_id`='$pageID'");

        // you only need the associative array
        while($arts = mysql_fetch_array($articles, MYSQL_ASSOC)){
            // close your tag properly and use the concat operator
            $content .= '<div class="article" id="art-' . $arts['id'] . '">';
            $content .= '<h1>' . $arts['name'] . '</h1>';
            $content .= $arts['content'];
            $content .= '</div>';
        }

    return $content;

 }

Also you could try running mysql_error() after your call to mysql_query() to see if anything goes wrong.

Another thing that you should check is so that there actually is a row with page_id equal to 1 in your database. You should also make sure that your column names are correct.

4 Comments

Unfortunately, this didn't change anything.
@JohnA - And you have tried echo mysql_error(); after your mysql_query() call? I updated my answer, not sure that it helps, but try to remove the { and } characters in your query.
I implimented this:if(!$articles){ die('Error!: ' . mysql_error()); }
+1 for the MYSQL_ASSOC part, very important so you don't get double results.
1
$content .= "<div class='article' id='art-" . $arts['id'] . '"'; 

and it should be

$content .= "<div class='article' id='art-" . $arts['id'] . "'"; 

Comments

0

try this

error_reporting(E_ALL);
ini_set("display_errors", 1);

function loadContent(){
   $content = '';
   $pageID = 1;
   $myQuery = "SELECT `id`, `name`, `content`, `keywords`, `description`, `access_level` FROM `tb_articles` WHERE `page_id`='{$pageID}'";
   echo $myQuery;
   $myQuery = "SELECT `id`, `name`, `content`, `keywords`, `description`, `access_level` FROM `tb_articles` WHERE `page_id`=" . $pageID;
   echo $myQuery;
   $articles = mysql_query($myQuery) or die("Error found with the Query" . mysql_error());

        while($arts = mysql_fetch_assoc($articles)){
                $content .= "<div class='article' id='art-" . $arts['id'] . '"';
            $content .= '<h1>' . $arts['name'] . '</h1>';
            $content .= $arts['content'];
            $content .= '</div>';
        }

    return $content;

}

Try and tell me, and I'll update with the explanation, if it works.

UPDATE

The code has been updated, there are two queries now, execute each one alone and check.

2 Comments

Unfortunately, this didn't affect anything.
ok, put this at the beginning of your page error_reporting(E_ALL); ini_set("display_errors", 1); Try to run the script again (better after clearing your browser cache)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.