Jump to content
Larry Ullman's Book Forums

spookie

Members
  • Posts

    7
  • Joined

  • Last visited

spookie's Achievements

Newbie

Newbie (1/14)

2

Reputation

  1. Thanks Hartley for your input. After posting in this forum, I was googling for an answer and found out the way to use sub query. SELECT p.page_name, p.content, u.first_name, ( SELECT count( * ) FROM comments AS c WHERE c.page_id = p.page_id ) AS count FROM pages AS p INNER JOIN users AS u USING ( user_id ) WHERE p.page_id =2 I think this version is clearer but not sure about performance tho. About your question, when user registers to my website. They provide First, Middle and Last name separately. So when I want to display them at a full name, I have to use CONCAT_WS(), unless there are better way?
  2. Oh, in an effort to figure out the problem, which is haunting me since last night. I have come up with a solution with LEFT JOIN and GROUP BY. Here is the update query. Please give advice if you think it is indeed a solution for my question. SELECT p.page_name, p.content, DATE_FORMAT( p.post_on, '%b %d, %y' ) AS date, CONCAT_WS( ' ', u.first_name, u.last_name ) AS name, u.user_id, count(c.comment_id) as count FROM users AS u INNER JOIN pages AS p USING ( user_id ) LEFT JOIN comments AS c ON p.page_id = c.page_id WHERE p.page_id = 1 group by p.page_name ORDER BY date ASC
  3. I have three tables: posts, author and comments. I want to perform a join cross three tables to retrieve Post details, author and the comments to that post id. My query is as follow: $q = " SELECT p.page_name, p.content,"; $q .= " DATE_FORMAT(p.post_on, '%b %d, %y') AS date, "; $q .= " CONCAT_WS(' ', u.first_name, u.last_name) AS name, u.user_id, "; $q .= " COUNT(c.comment_id) AS count"; $q .= " FROM pages AS p "; $q .= " INNER JOIN users AS u "; $q .= " USING (user_id) "; $q .= " INNER JOIN comments as c "; $q .= " USING (page_id) "; $q .= " WHERE p.page_id = {$pid} "; $q .= " ORDER BY date ASC"; This query runs well and good if the $pid is valid or exists. But if the Post ID does not exists, it will be a blank page. Because the query still returns one row contains NULL value and number of comment of 0 due to the aggregation function count(). This row renders my my else clause useless. if(mysqli_num_rows($r) > 0) { // code to print post } else { echo "post not found"; } I know there is must be a good way to overcome this, but I seem cannot get it to work. Any help will be very much appreciated. I am sorry if the question seem ambiguos.
  4. Thanks Hartley! So according to that thread using hidden field is the most effective including fixing for IE browser. But I'm somewhat skeptical about this over request_method tho. Any explaination for my other questions tho?
  5. I think when you call the function session_start() at the begining of the script. It does two things 1) set the session if it does not exists 2) does nothing if it exists.
  6. Did you set the value type of the active column in your database is NULL by default? Please check with your PHPmyadmin to see if the column is indeed NULL, or esle, you can never activate the account.
  7. Hi there, I notice that there are few ways Larry used to check if the from is submitted. 1. Use hidden field 2. REQUEST_METHOD == 'POST' // used most frequently 3. $_POST['submitted'] Is this the matter of personal preference or one method is better than the others? Another question also about the filter_var() function. The parameter which is an array('min_range'=>1). I can't seem to understand it. And it said sometime in the book that a malicious user can attemp to hack a HTML form to send a different value than expected from an <option>. How is it done? Not I want to hack, but just curious how it is done since the form is in the server and the end user is only able to view source. Thank you so much.
×
×
  • Create New...