Jump to content

adamjnz

Members
  • Posts

    30
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

adamjnz's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. This has the same result as originally reported in the thread.
  2. [quote author=fenway link=topic=111020.msg455929#msg455929 date=1161548246] Sorry I never got back to this... that query may work, but the "subquery" is really just a unnamed view a.k.a. derived table, which should be fine.  But only if the word select is inside the parentheses -- otherwise, you'll get a syntax error on all versions. [/quote] I tired putting select in the brackets but still getting exactly the same error message. Query was: SELECT BlogPost.BlogPostID , ImgSrc , PostTitle , PostText , BlogPost.DateTime , Category , Deleted , User.Name , x.commentcount AS CommentCount FROM BlogPost JOIN User on BlogPost.userID=User.userID   LEFT JOIN (select BlogPostID, count(*) as commentcount from BlogPostComment) x       ON BlogPost.BlogPostID = x.BlogPostID WHERE BlogPost.Deleted = '0' GROUP BY BlogPost.BlogPostID ORDER BY BlogPost.BlogPostID
  3. A friend at work (An SQL Guru of sorts) suggested the following query, however he used MS SQL and MySQL Doesnt seem to like the subquery (in the brackets) and throws an error... --------------------------------------------------------------------------------------------- SELECT BlogPost.BlogPostID , ImgSrc , PostTitle , PostText , BlogPost.DateTime , Category , Deleted , User.Name , x.commentcount AS CommentCount FROM BlogPost JOIN User on BlogPost.userID=User.userID LEFT JOIN select(BlogPostID, count(*) as commentcount from BlogPostComment) x ON BlogPost.BlogPostID = x.BlogPostID WHERE BlogPost.Deleted = '0' GROUP BY BlogPost.BlogPostID ORDER BY BlogPost.BlogPostID ------------------------------------------------------------------------------------- Comments? Thoughts?
  4. I will try and put it in better context, here is what I am trying to do: 1)       Pull BlogPostID, ImgSrc, PostTitle, PostText, BlogPost.DateTime, Category, Deleted values from the BlogPost table 2)       Pull the Name value from the User table based on the UserID stored against the record in the BlogPost table. 3)       Calculate the number of comments linked to a BlogPost from the BlogPostComment table based on the BlogPostID stored in the BlogPost table. The following query works fine for points 1 & 2 but when I try and add point 3 into the mix with the 2nd query it does the following to the results: 1)       Adds up the total comments for all blog posts and return the value to each record returned 2)       Stop returning the Name for the User table for all but the first record returned. [u][b]Points 1 & 2:[/b][/u] SELECT BlogPost.BlogPostID, ImgSrc, PostTitle, PostText, BlogPost.DateTime, Category, Deleted, User.Name FROM BlogPost JOIN User USING (userID) WHERE BlogPost.Deleted = '0' [b]Returns something like:[/b] [table]   [tr]     [td]Record [/td]     [td]BlogPostID [/td]     [td]ImgSrc [/td]     [td]PostTitle [/td]     [td]PostText [/td]     [td]DateTime [/td]     [td]Category [/td]     [td]Deleted [/td]     [td]Name [/td]   [/tr]   [tr]     [td]1 [/td]     [td]00001 [/td]     [td]Post1.jpg [/td]     [td]Title 1 [/td]     [td]Text 1… [/td]     [td]2006-10-10…. [/td]     [td]Other [/td]     [td]0 [/td]     [td]Adam Jobbins [/td]   [/tr]   [tr]     [td]2 [/td]     [td]00002 [/td]     [td]Post2.jpg [/td]     [td]Title 2 [/td]     [td]Text 2… [/td]     [td]2006-10-10…. [/td]     [td]Announcements [/td]     [td]0 [/td]     [td]Test User 2 [/td]   [/tr]   [tr]     [td]N [/td]     [td]0000N [/td]     [td]PostN.jpg [/td]     [td]Title N [/td]     [td]Text N… [/td]     [td]2006-10.. [/td]     [td]Whatever [/td]     [td]0 [/td]     [td]Users Name [/td]   [/tr] [/table] [u][b]Points 1, 2 & 3:[/b][/u] SELECT BlogPost.BlogPostID, ImgSrc, PostTitle, PostText, BlogPost.DateTime, Category, Deleted, User.Name, COUNT(BlogPostComment.BlogCommentID) AS CommentCount FROM BlogPost JOIN User USING (userID) RIGHT JOIN BlogPostComment ON BlogPost.BlogPostID = BlogPostComment.BlogPostID WHERE BlogPost.Deleted = '0' GROUP BY BlogPostID [b]Returns something like: (2 Records Total in the BlogPostComment table, both linked to BlogPostID = 00001)[/b] [table]   [tr]     [td]Record [/td]     [td]BlogPostID [/td]     [td]ImgSrc [/td]     [td]PostTitle [/td]     [td]PostText [/td]     [td]DateTime [/td]     [td]Category [/td]     [td]Deleted [/td]     [td]Name [/td]     [td]CommentCount [/td]   [/tr]   [tr]     [td]1 [/td]     [td]00001 [/td]     [td]Post1.jpg [/td]     [td]Title 1 [/td]     [td]Text 1… [/td]     [td]2006-10-10…. [/td]     [td]Other [/td]     [td]0 [/td]     [td]Adam Jobbins [/td]     [td]2 [/td]   [/tr]   [tr]     [td]2 [/td]     [td]00002 [/td]     [td]Post2.jpg [/td]     [td]Title 2 [/td]     [td]Text 2… [/td]     [td]2006-10-10…. [/td]     [td]Announcements [/td]     [td]0 [/td]     [td][/td]     [td]2 [/td]   [/tr]   [tr]     [td]N [/td]     [td]0000N [/td]     [td]PostN.jpg [/td]     [td]Title N [/td]     [td]Text N… [/td]     [td]2006-10.. [/td]     [td]Whatever [/td]     [td]0 [/td]     [td][/td]     [td]2 [/td]   [/tr] [/table] [b]SHOULD RETURN:[/b] [table]   [tr]     [td]Record [/td]     [td]BlogPostID [/td]     [td]ImgSrc [/td]     [td]PostTitle [/td]     [td]PostText [/td]     [td]DateTime [/td]     [td]Category [/td]     [td]Deleted [/td]     [td]Name [/td]     [td]CommentCount [/td]   [/tr]   [tr]     [td]1 [/td]     [td]00001 [/td]     [td]Post1.jpg [/td]     [td]Title 1 [/td]     [td]Text 1… [/td]     [td]2006-10-10…. [/td]     [td]Other [/td]     [td]0 [/td]     [td]Adam Jobbins [/td]     [td]2 [/td]   [/tr]   [tr]     [td]2 [/td]     [td]00002 [/td]     [td]Post2.jpg [/td]     [td]Title 2 [/td]     [td]Text 2… [/td]     [td]2006-10-10…. [/td]     [td]Announcements [/td]     [td]0 [/td]     [td]Test User 2 [/td]     [td]0 [/td]   [/tr]   [tr]     [td]N [/td]     [td]0000N [/td]     [td]PostN.jpg [/td]     [td]Title N [/td]     [td]Text N… [/td]     [td]2006-10.. [/td]     [td]Whatever [/td]     [td]0 [/td]     [td]Users Name [/td]     [td]N [/td]   [/tr] [/table]
  5. [quote author=fenway link=topic=111020.msg449898#msg449898 date=1160494244] Very strange looking query... I'm not sure what you mean by (1); as for (2), you have what looks like the right GROUP BY statement, so I'm confused. [/quote] Ok, so for 1) it should be returning the Name of the person who wrote the post, this is got by taking the UserID and looking it up in the User table and returning the associated username. However, it is only returning a result for the 1st record returned by the query, all other results this feild returns nothing. 2) This is working but it is giving me the total for all blog posts, not just the blog post the comments are related to. Hope this makes sense
  6. UPDATE: After fiddling with the code I can finally get it to run something without errors, however there are 2 problems:( 1) It will only return a Name from User.Name for the first result 2) The comment count is an aggregate for all Blog Posts, where I want it to be for each Blog Post Any ideas? [code]SELECT BlogPost.BlogPostID, ImgSrc, PostTitle, PostText, BlogPost.DateTime, Category, Deleted, User.Name, COUNT(BlogPostComment.BlogCommentID) AS CommentCount FROM BlogPost JOIN User USING (userID) RIGHT JOIN BlogPostComment ON BlogPostComment.BlogPostID = BlogPost.BlogPostID WHERE BlogPost.Deleted = '0' GROUP BY BlogPostID[/code]
  7. Hey Guys, I downloaded a tutorial on joining MySQL tables in a query from DMXZone however what I am trying to do goes slightly past the scope of that tutorial. I have tried playing around with the SQL query I want but cannot get it to work. If someone has some insight into this it would be much appreciated. The query as I was trying it: [code]SELECT BlogPostID, ImgSrc, PostTitle, PostText, DateTime, Category, Deleted, User.Name, COUNT(`BlogPostComment.BlogCommentID`) AS `CommentCount` FROM BlogPost JOIN User USING (userID) JOIN BlogPostComment USING (BlogPostID) WHERE BlogPost.Deleted = '0'[/code] Essentially what I am trying to do is this: 1) Pull the blog post data out of the 'BlogPost' Table 2) Crossreference the UserID stored in the 'BlogPost' table to pull out the Full Name of the Author from the 'User' Table 3) Count the number of comments for that particular blog post from the 'BlogPostComment' table using the BlogPostID I can get 1 and 2 working togeather fine, but when I try and add in step 3 it all goes pear shaped (including telling me that DateTime is not ambiguous  ???)
  8. What I am trying to do is get the total of items sold from a table. On the site when the user places an order is stores their username, product, quantity, price etc in a table called order_products. I have an SQL query that filters the results by the username (as to only get the results for the person in question) What I need to be able to do is say something like "Hey, since you signed up you have ordered a total of XXXXX products! - Your best selling product is XXXXXX" I would really appreciate a point in the right direction.
  9. Solved now. I copied and pasted my code out of the PHP file and pasted it back in bit by bit to try and find the errors. But once it was all pasted back in it worked fine.
  10. [!--quoteo(post=365456:date=Apr 17 2006, 05:00 PM:name=ypirc)--][div class=\'quotetop\']QUOTE(ypirc @ Apr 17 2006, 05:00 PM) [snapback]365456[/snapback][/div][div class=\'quotemain\'][!--quotec--] It means if your code has an error and complains about it to the browser, it is sending headers. Also, if you look at your code you have session_start() twice...remove the second one and that will probably solve your problem. [/quote] Still get same error
  11. [!--quoteo(post=365453:date=Apr 17 2006, 04:41 PM:name=ypirc)--][div class=\'quotetop\']QUOTE(ypirc @ Apr 17 2006, 04:41 PM) [snapback]365453[/snapback][/div][div class=\'quotemain\'][!--quotec--] If your code is spitting out another error that counts as html too.[/quote] I don't know what this means [!--quoteo(post=365453:date=Apr 17 2006, 04:41 PM:name=ypirc)--][div class=\'quotetop\']QUOTE(ypirc @ Apr 17 2006, 04:41 PM) [snapback]365453[/snapback][/div][div class=\'quotemain\'][!--quotec--]Also, why are you initializing the session twice?[/quote] Didn't know I was
  12. I am having a 'Cannot modify header information - headers already sent' problem. I have read the other posts about the cause being trying to send header information AFTER the HTML starts. I have checked my code and the line it is erroring on is BEFORE the HTML starts. Any idea what would cause this? [code]<?php require_once('../Connections/ppl_frontend.php'); ?> <?php require_once('../Connections/ppl_frontend.php'); ?> <?php //initialize the session session_start(); // ** Logout the current user. ** $logoutAction = $_SERVER['PHP_SELF']."?doLogout=true"; if ((isset($_SERVER['QUERY_STRING'])) && ($_SERVER['QUERY_STRING'] != "")){   $logoutAction .="&". htmlentities($_SERVER['QUERY_STRING']); } if ((isset($_GET['doLogout'])) &&($_GET['doLogout']=="true")){   //to fully log out a visitor we need to clear the session varialbles   session_unregister('MM_Username');   session_unregister('MM_UserGroup');        $logoutGoTo = "./";   if ($logoutGoTo) {     header("Location: $logoutGoTo");     exit;   } } ?> <?php session_start(); $MM_authorizedUsers = "Administrator"; $MM_donotCheckaccess = "false"; // *** Restrict Access To Page: Grant or deny access to this page function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) {   // For security, start by assuming the visitor is NOT authorized.   $isValid = False;   // When a visitor has logged into this site, the Session variable MM_Username set equal to their username.   // Therefore, we know that a user is NOT logged in if that Session variable is blank.   if (!empty($UserName)) {     // Besides being logged in, you may restrict access to only certain users based on an ID established when they login.     // Parse the strings into arrays.     $arrUsers = Explode(",", $strUsers);     $arrGroups = Explode(",", $strGroups);     if (in_array($UserName, $arrUsers)) {       $isValid = true;     }     // Or, you may restrict access to only certain users based on their username.     if (in_array($UserGroup, $arrGroups)) {       $isValid = true;     }     if (($strUsers == "") && false) {       $isValid = true;     }   }   return $isValid; } $MM_restrictGoTo = "login.php"; if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {     $MM_qsChar = "?";   $MM_referrer = $_SERVER['PHP_SELF'];   if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";   if (isset($QUERY_STRING) && strlen($QUERY_STRING) > 0)   $MM_referrer .= "?" . $QUERY_STRING;   $MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);   header("Location: ". $MM_restrictGoTo);   exit; } ?> <?php mysql_select_db($database_ppl_frontend, $ppl_frontend); $query_admin = "SELECT * FROM ppl_admin"; $admin = mysql_query($query_admin, $ppl_frontend) or die(mysql_error()); $row_admin = mysql_fetch_assoc($admin); $totalRows_admin = mysql_num_rows($admin); mysql_select_db($database_ppl_frontend, $ppl_frontend); $query_products = "SELECT * FROM ppl_products"; $products = mysql_query($query_products, $ppl_frontend) or die(mysql_error()); $row_products = mysql_fetch_assoc($products); $totalRows_products = mysql_num_rows($products); mysql_select_db($database_ppl_frontend, $ppl_frontend); $query_orders = "SELECT * FROM ppl_orders ORDER BY orderID DESC"; $orders = mysql_query($query_orders, $ppl_frontend) or die(mysql_error()); $row_orders = mysql_fetch_assoc($orders); $totalRows_orders = mysql_num_rows($orders); ?> <?php function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") {   $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;   switch ($theType) {     case "text":       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";       break;         case "long":     case "int":       $theValue = ($theValue != "") ? intval($theValue) : "NULL";       break;     case "double":       $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";       break;     case "date":       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";       break;     case "defined":       $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;       break;   }   return $theValue; } $editFormAction = $_SERVER['PHP_SELF']; if (isset($_SERVER['QUERY_STRING'])) {   $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']); } ?> <?php do { ?> <?php if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "add_user")) {   $insertSQL = sprintf("INSERT INTO ppl_user_products (username, productID, product_price, product_quantity) VALUES (%s, %s, %s, %s)",                        GetSQLValueString($_POST['username'], "text"),                        GetSQLValueString($row_products['productID'], "int"),                        GetSQLValueString($row_products['default_price'], "int"),                        GetSQLValueString($row_products['default_quantity'], "int"));   mysql_select_db($database_ppl_frontend, $ppl_frontend);   $Result1 = mysql_query($insertSQL, $ppl_frontend) or die(mysql_error()); } ?> <?php } while ($row_products = mysql_fetch_assoc($products)); ?> <?php if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "add_user")) {   $insertSQL = sprintf("INSERT INTO ppl_users (username, password, access, email, real_name, company, post_add1, post_add2, post_add_sub, post_add_city, ship_add1, ship_add2, ship_add_sub, ship_add_city) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",                        GetSQLValueString($_POST['username'], "text"),                        GetSQLValueString($_POST['password'], "text"),                        GetSQLValueString($_POST['access'], "text"),                        GetSQLValueString($_POST['email'], "text"),                        GetSQLValueString($_POST['real_name'], "text"),                        GetSQLValueString($_POST['company'], "text"),                        GetSQLValueString($_POST['post_add1'], "text"),                        GetSQLValueString($_POST['post_add2'], "text"),                        GetSQLValueString($_POST['post_add_sub'], "text"),                        GetSQLValueString($_POST['post_add_city'], "text"),                        GetSQLValueString($_POST['ship_add1'], "text"),                        GetSQLValueString($_POST['ship_add2'], "text"),                        GetSQLValueString($_POST['ship_add_sub'], "text"),                        GetSQLValueString($_POST['ship_add_city'], "text"));   mysql_select_db($database_ppl_frontend, $ppl_frontend);   $Result1 = mysql_query($insertSQL, $ppl_frontend) or die(mysql_error());   $insertGoTo = "users.php";   if (isset($_SERVER['QUERY_STRING'])) {     $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";     $insertGoTo .= $_SERVER['QUERY_STRING'];   }   header(sprintf("Location: %s", $insertGoTo)); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head>etc etc etc[/code]
  13. [code]<?php echo date("jS M Y",strtotime($row_orders['order_date'])); ?>[/code] Does anyone know why this is returning the date 1st Jan 1970 no matter what is in the Database? I just copied/pasted this code from another website I did a while ago that works fine. Values in the DB are stored using TIMESTAMP in MySQL
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.