CLU Posted January 30, 2012 Report Share Posted January 30, 2012 Hi everyone, I wonder if anyone can see what i am doing wrong here... I have created a table called blog_posts and just want to echo the posts onto my index page. I have created the cms application from Bens tutorials, and essentially i am trying to copy what he did with his code to create two new functions (display_blog and load_blog) All i want to do is use this function on my index page... But cannot see where i am going wrong.... Heres my code: (m_cms.php) function load_blog() { // get contents from database if ($stmt = $this->FS->Database->prepare("SELECT * FROM blog_posts ORDER BY id")) { $stmt->execute(); $stmt->store_result(); if ($stmt->num_rows > 0) { $stmt->bind_result($blog_posts); $stmt->fetch(); $stmt->close(); return $blog_posts; } else { $stmt->close(); return FALSE; } } } function display_blog() { // get content $blog_posts = $this->load_blog(); // check login status if ($this->FS->Auth->checkLoginStatus()) { echo'You are logged in'; } else { echo $blog_posts; } } Code on my index page : <?php $FS->Cms->display_blog();?> If anyone can give me some guidance I would appreciate it alot Kind Regards CLU Quote Link to comment Share on other sites More sharing options...
falkencreative Posted January 30, 2012 Report Share Posted January 30, 2012 With that code in place, what are you currently getting? Errors? No errors? Quote Link to comment Share on other sites More sharing options...
Symphlion Posted January 30, 2012 Report Share Posted January 30, 2012 Several things are going wrong, first of all, $blog_posts is currently infinity looping. $blog_posts = $this->load_blog(); While inside the load_blog(); you are using the $blog_posts to bind something. The thing is, you cannot bind them yet since there`s nothing returned before that function is done. So binding $blog_posts to the statement is a no-go. Second thing, not a biggy, is this: you are loading in blog_posts to display only if a user is NOT logged in. Users that have logged in, aren`t going to get any results. And either way, you should only start loading the $blog_posts if you know that you are going to need them. Now you load them regardless of whether you need them or not. Quote Link to comment Share on other sites More sharing options...
CLU Posted January 30, 2012 Author Report Share Posted January 30, 2012 Really appreciate your replies guys... @Ben the page loads as normal... But does not display anything to do with the stated function. But when i am logged in it does echo out the ''You are logged in"... Really don't know what i am doing wrong? @symphlion I thought by having 'return $blog_posts;' That i would be returning the result? The second thing was just testing to see if just echoing anything out would work. Still trying to work it out... Can you guys see something I'm missing? Thanks again Quote Link to comment Share on other sites More sharing options...
Symphlion Posted January 30, 2012 Report Share Posted January 30, 2012 Well, like I said, the $blog_posts is wrong. You are binding an empty ( not finished ) value which is weird. i`m referring to this: $blog_posts = $this->load_blog(); // tadidumtida // No code has yet been returned by load_blog(); // but somehow, you are binding it to the statement $stmt->bind_result($blog_posts); // The thing is, $blog_posts is not defined anywhere. It`s not magically appearing // $blog_posts should mean something, but in the current context, it doesn`t. It`s nothing. Quote Link to comment Share on other sites More sharing options...
CLU Posted January 31, 2012 Author Report Share Posted January 31, 2012 Thant for your reply Symphlion, excuse my ignorance but i still can't work it out... I have changed the code to which i think you was talking about... function load_blog() { // get contents from database if ($stmt = $this->FS->Database->prepare("SELECT * FROM blog_posts ORDER BY id")) { $stmt->execute(); $stmt->store_result(); if ($stmt->num_rows > 0) { $stmt->bind_result($blog_posts); $stmt->fetch(); $stmt->close(); return $status; } else { $stmt->close(); return FALSE; } } } function display_blog() { // get content $status = $this->load_blog(); echo $status; // check login status if ($this->FS->Auth->checkLoginStatus()) { echo $status; } else { } } I changed the return to $status which is a column within the database and have echoed that out.. Im still having trouble... I appreciate your time and help... How would you write it out... Im sure this is probably something simple that i am over looking :-/ Quote Link to comment Share on other sites More sharing options...
Symphlion Posted January 31, 2012 Report Share Posted January 31, 2012 Ah wait a minute.. Im looking at a mysqli object.. I think.. I dont know, i never watch the video. However, i`m thinking something totally different right now. Do this; $status = $this->load_blog(); echo $status; // And add this: if($status){ echo "Result set is positive"; } Quote Link to comment Share on other sites More sharing options...
falkencreative Posted January 31, 2012 Report Share Posted January 31, 2012 @Symphilion seems a little confused and may not be providing accurate advice. As far as I understand it, stmt->bind_result() is only used when you are wanting to select specific columns from the database, not all the columns. You may want to look at http://www.killersites.com/community/index.php?/topic/3064-basic-php-system-view-edit-add-delete-records-with-mysqli/ for samples of different ways to use MySQLi to get/add/edit/delete data from the database. Here's what I'd suggest you do (not tested, but it's a starting point): function load_blog() { // get contents from database if ($stmt = $this->FS->Database->prepare("SELECT * FROM blog_posts ORDER BY id")) { $stmt->execute(); $stmt->store_result(); if ($stmt->num_rows > 0) { $data = '<ul>'; while ($row = $result->fetch_object()) { //assemble your string of results here. You can access row data like this: $row->column_name; $data .= '<li></li>'; } $data .= '</ul>' $stmt->close(); return $data; } else { $stmt->close(); return FALSE; } } } function display_blog() { // get content $blog_posts = $this->load_blog(); // check login status if ($this->FS->Auth->checkLoginStatus()) { echo'You are logged in'; } else { echo $blog_posts; } } Quote Link to comment Share on other sites More sharing options...
CLU Posted February 1, 2012 Author Report Share Posted February 1, 2012 HI Ben, thanks for your reply. I tired that code out but as soon as that code is added and the call of <?php $FS->Cms->display_blog(); ?> is added to my index page the page breaks and i get a HTTP ERROR 500 (Internal Error Page) display. This is really confusing as to why it is not working... Because from the code above it seems logical to me and it should work... Any other suggestions? Quote Link to comment Share on other sites More sharing options...
falkencreative Posted February 1, 2012 Report Share Posted February 1, 2012 Do you have PHP and/or MySQLi error messages turned on? That would help sort things out. I did notice one error though. This line: $data .= '</ul>' Needs a ";". Quote Link to comment Share on other sites More sharing options...
CLU Posted February 1, 2012 Author Report Share Posted February 1, 2012 Hi Ben, There errors that i am getting are the following: Notice: Undefined variable: result in /Applications/MAMP/htdocs/ukmocks/app/cms/models/m_cms.php on line 211 Fatal error: Call to a member function fetch_object() on a non-object in /Applications/MAMP/htdocs/ukmocks/app/cms/models/m_cms.php on line 211 That line seems to be 'while ($row = $result->fetch_object())'... Any ideas whats going on? Thanks again... Quote Link to comment Share on other sites More sharing options...
falkencreative Posted February 2, 2012 Report Share Posted February 2, 2012 A couple additional things I noticed about your code the second time I looked at it and checked it against the link I provided above with some sample MySQLi queries: function load_blog() { // get contents from database if ($stmt = $this->FS->Database->prepare("SELECT * FROM blog_posts ORDER BY id")) { $stmt->execute(); $stmt->store_result(); if ($stmt->num_rows > 0) { $data = '<ul>'; while ($row = $result->fetch_object()) There's not need to use prepare if you aren't using a prepared statement. So I would think you would be able to rewrite that code like this: function load_blog() { // get contents from database if ($result = $this->FS->Database->query("SELECT * FROM blog_posts ORDER BY id")) { if ($stmt->num_rows > 0) { $data = '<ul>'; while ($row = $result->fetch_object()) Try that and let me know? Quote Link to comment Share on other sites More sharing options...
CLU Posted February 2, 2012 Author Report Share Posted February 2, 2012 Hi Ben, Thanks again for your help... It would seem whilst we have came to the same'ish conclusion.... I did this lastnight function display_blog() { $blog_posts = $this->load_blog(); if ($this->FS->Auth->checkLoginStatus()) { $blog_start = '<div class="fs_edit">'; $blog_link = '<a class="fs_edit_quotes" href="' . SITE_PATH . 'app/blog/index.php">Edit Blog</a>'; $blog_end = '</div>'; echo $blog_start . $blog_link . $blog_end; } else { echo $blog_posts; } } function load_blog() { $stmt = $this->FS->Database->query("SELECT id, type, LEFT(title, 10)AS title_excerpt, title, author FROM blog_posts ORDER BY id Limit 3"); $row = mysqli_fetch_array($stmt); if ($row['type'] == 'text') { $stmt = $this->FS->Database->prepare('SELECT id, title, body, type FROM blog_posts ORDER BY id Limit 1'); $stmt->execute(); $stmt->bind_result($id, $title, $body, $type); while ( $row = $stmt->fetch() ) : echo'<div id="last_posts">'; echo $title; echo $body; echo'</div>'; endwhile; } } I am using some if statements to make sure the posts are text, as the last thing i want on the home page is image' and/or videos... Seems to be working fine. I also tried the above code and that worked fine aswell... LEt me know if there is anything i shouldnt be doing or maybe could i refactor this code to be more effective? So out of curiosity when should i prepared statements... Is that when im only going to bind the results? It just seems that someone wont be able to bind '*' as its everything... Let me know your thoughts and once again thanks for your help... Quote Link to comment Share on other sites More sharing options...
falkencreative Posted February 2, 2012 Report Share Posted February 2, 2012 As far as I understand it, you only need to use prepare() when either selecting data from specific columns or using a "WHERE" statement where you need to insert a variable into the query (for example, something like "WHERE id = $id"). Prepared statements are intended to be used to automatically escape any data provided in variables to avoid SQL injection. To be honest, I don't understand your load_blog() function above at all -- mainly, I don't understand why you have multiple MySQLi queries. The first query selects 3 results from your blog_posts table, and then the next only selects 1 result from the same blog_posts table... and in both cases, you are getting just about the same data? I'm also not seeing your load_blog() function return anything -- it just echos out any blog post with a type of "text", as far as I can tell. Quote Link to comment Share on other sites More sharing options...
CLU Posted February 2, 2012 Author Report Share Posted February 2, 2012 So do you think i could have it in just one function... Maybe this could be easier... Basically for the two mysqli quieries is checking to see if the typwe within the database is test, and if it is then it will echo the title and body, I have updated this to use the Exerpt so that it only shows 10 characters form the post. But I think (correct me if im wrong) but could i just fetch the array in the top query and then just display the results in the following $row['title'] and the same for post. I think by watching your tutorials on here has really helped me learning OOP... As it seems other tuts online made it confusing... Let me know what you think. Also im not sure if you have found this bug within safari (seemingly is a css bug) but it seems that sometime if you float content within the WYSIWYG editor the content overlaps the content below... I have added <div>'s that clear but as soon as you log out it is fine... This issue is not present in Mozzilla, but in the Webkit browsers its present... Just thought I'd give you a heads up if you have experienced this before? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.