Page 1 of 1

Php Blog

Posted: Thu Dec 15, 2011 4:32 am
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]

Re: Php Blog

Posted: Thu Dec 15, 2011 5:36 am
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]

Re: Php Blog

Posted: Fri Dec 16, 2011 11:38 am
by jacek
Well, in what way is it not working ?

Re: Php Blog

Posted: Sat Dec 17, 2011 12:10 am
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]

Re: Php Blog

Posted: Sat Dec 17, 2011 3:26 pm
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.

Re: Php Blog

Posted: Sat Dec 17, 2011 3:55 pm
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

Re: Php Blog

Posted: Sat Dec 17, 2011 5:48 pm
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]

Re: Php Blog

Posted: Sat Dec 17, 2011 5:51 pm
by Jaami
what is the error message?

Re: Php Blog

Posted: Sat Dec 17, 2011 6:00 pm
by RealTuty
i don't know

Re: Php Blog

Posted: Sat Dec 17, 2011 6:05 pm
by Jaami
eh? you should turn "error_reporting(E_ALL);" on.

btw. i found the error in line 16 you have one "=" to much..

Re: Php Blog

Posted: Sat Dec 17, 2011 6:08 pm
by RealTuty
ok thank you that was the error
:D

Re: Php Blog

Posted: Mon Dec 19, 2011 1:20 am
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]

Re: Php Blog

Posted: Mon Dec 19, 2011 7:31 am
by bowersbros
And what exactly is the problem your having?

Re: Php Blog

Posted: Mon Dec 19, 2011 7:38 am
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.

Re: Php Blog

Posted: Mon Dec 19, 2011 7:38 am
by RealTuty
it is inserting 0/0/0000 00:00:00 always

Re: Php Blog

Posted: Mon Dec 19, 2011 11:39 am
by jacek
you should not have ' around NOW() since it is a function.