Php Blog

Post here is you are having problems with any of the tutorials.
Post Reply
RealTuty
Posts: 11
Joined: Thu Dec 15, 2011 4:29 am

Php Blog

Post by RealTuty »

Hello i have some code i copied from the php blog tutorial that is not working. Any help is accepted.
[syntax=php]<?php

$connect = mysql_connect('localhost', 'root', 'root');
$databse = mysql_select_db(blog, $connect);

echo mysql_error();

function valid_pid($pid) {
$pid = (int)$pid;

$total = mysql_query("SELECT COUNT(post_id) FROM posts WHERE post_id='$pid'");
$total = mysql_result($total, 0);

if ($total !=1) {
return false;
} else {
return else;
}

}

function get_posts() {

$sql = "SELECT
posts.post_id AS id,
posts.post_title AS title,
LEFT(posts.post_body, 512) AS preview,
posts.post_user AS user,
DATE_FORMAT(posts.post_date, '%d/%m/%y %H:%i:%s') AS date,
comments.total_comments,
DATE_FORMAT(comments.last_comment, '%d/%m/%y %H:%i:%s') AS last_comment,
FROM posts
LEFT JOIN (
SELECT
post_id,
COUNT(comment_id) AS total_comments,
MAX(comment_date) AS last_comment,
FROM comments
GROUP BY post_id
) AS comments
ON posts.post_id = comments.post_id
ORDER BY posts.post_date DESC";

echo mysql_error();

$posts = mysql_query($sql);

$rows = array();

while (($row = mysql_fetch_assoc($posts)) !== false) {
$rows[] = array(
'id' => $row['id'],
'title' => $row['title'],
'preview' => $row['preview'],
'user' => $row['user'],
'date' => $row['date'],
'total_comments' => (),
'last_comment' => ()
);
}

}

function get_post($pid) {

}

function add_post($name, $title, $body) {

}

echo 'Hello';


?>[/syntax]
Last edited by jacek on Fri Dec 16, 2011 11:38 am, edited 1 time in total.
Reason: removed email address
Jaami
Posts: 38
Joined: Wed Nov 23, 2011 11:22 pm
Location: GER

Re: Php Blog

Post by Jaami »

i see you didn`t use backtics at all?
the functions get_post, add_post are not defined yet?

and what is your error message/s or what is not working exactly?

[syntax=php]$sql = "SELECT
`posts`.`post_id` AS `id`,
`posts`.`post_title` AS `title`,
`posts`.`post_video` AS `video`,
...
[/syntax]
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Php Blog

Post by jacek »

Well, in what way is it not working ?
Image
RealTuty
Posts: 11
Joined: Thu Dec 15, 2011 4:29 am

Re: Php Blog

Post by RealTuty »

this is the error
[syntax=text]#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM posts LEFT JOIN ( SELECT ' at line 9[/syntax]
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Php Blog

Post by jacek »

If you look just before the part of the SQL that it quotes in the error, in this case "FROM users" the problem is usually there. On the line above the FROM, you have a comma at the end that should not be there since it's the list item in the list.
Image
RealTuty
Posts: 11
Joined: Thu Dec 15, 2011 4:29 am

Re: Php Blog

Post by RealTuty »

thank you that is working now it turns out i did that twice in that same query now i know to look for and now i am able to watch the rest of the video. Thanks again

Jason
RealTuty
Posts: 11
Joined: Thu Dec 15, 2011 4:29 am

Re: Php Blog

Post by RealTuty »

ok so that is working but now i have this code giving me an error the comments.func.inc.php
[syntax=php]<?php

function get_comments($pid) {
$pid = (int)$pid;

$sql = "SELECT
comment_body AS body,
comment_user AS user,
DATE_FROMAT(comment_date, '%d/%m/%Y %H:%i:%s') AS date
FROM comments
WHERE post_id='$pid'";

$result = mysql_query($sql);

$comments = array();
while (($row = mysql_fetch_assoc($result)) !===false) {
$comments[] = $row;
}

return $comments;

}

function add_comment($pid, $user, $body) {

if (valid_pid($pid) === false) {
return false;
}

$pid = (int)$pid;
$user = mysql_real_escape_string(htmlentities($user));
$body = mysql_real_escape_string(nl2br(htmlentities($body)));

mysql_query("INSERT INTO comments (post_id, comment_user, comment_body, comment_date) VALUES ('$pid', '$user', '$body', NOW())");

return true;

}

echo 'x';

?>[/syntax]
Jaami
Posts: 38
Joined: Wed Nov 23, 2011 11:22 pm
Location: GER

Re: Php Blog

Post by Jaami »

what is the error message?
RealTuty
Posts: 11
Joined: Thu Dec 15, 2011 4:29 am

Re: Php Blog

Post by RealTuty »

i don't know
Jaami
Posts: 38
Joined: Wed Nov 23, 2011 11:22 pm
Location: GER

Re: Php Blog

Post by Jaami »

eh? you should turn "error_reporting(E_ALL);" on.

btw. i found the error in line 16 you have one "=" to much..
RealTuty
Posts: 11
Joined: Thu Dec 15, 2011 4:29 am

Re: Php Blog

Post by RealTuty »

ok thank you that was the error
:D
RealTuty
Posts: 11
Joined: Thu Dec 15, 2011 4:29 am

Re: Php Blog

Post by RealTuty »

Ok. now I am having a problem with the mysql [syntax=sql]NOW()[/syntax] function here is the query code
[syntax=php]$sql = "INSERT INTO blog_posts (post_title, post_body, post_user, post_date, post_keywords, post_des) VALUES ('$title', '$body', 'Jason', 'NOW()', '$keywords', '$des')";
$add_post = mysql_query($sql);[/syntax]
bowersbros
Posts: 534
Joined: Thu May 05, 2011 8:19 pm

Re: Php Blog

Post by bowersbros »

And what exactly is the problem your having?
I don't like to brag, but I wasn't circumcised. I was circumnavigated. ;)

Want to learn something new? Or maybe reinforce what you already know? Or just help out? Please subscribe to my videos: http://goo.gl/58pN9
Jaami
Posts: 38
Joined: Wed Nov 23, 2011 11:22 pm
Location: GER

Re: Php Blog

Post by Jaami »

mmmh...can you please tell the exact problem and the error message! so we have an idea what we should look for, from the beginning!

if you dont get any error messages, use "error_reporting(E_ALL);" at the top of your page, or set it in your php.ini to:

"; error_reporting
; Default Value: E_ALL & ~E_NOTICE"

..so it will be always enabled, as it normaly should be.
RealTuty
Posts: 11
Joined: Thu Dec 15, 2011 4:29 am

Re: Php Blog

Post by RealTuty »

it is inserting 0/0/0000 00:00:00 always
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Php Blog

Post by jacek »

you should not have ' around NOW() since it is a function.
Image
Post Reply