Jump to content
Larry Ullman's Book Forums

chop

Members
  • Content Count

    192
  • Joined

  • Last visited

  • Days Won

    5

Everything posted by chop

  1. Correction the the above post. To clarify, if I want to order the output by the town in which a particular pet was lost (column= town_lost) AND at the same time not get duplicate data returned (because there may be many returns in the "town_lost" column that are not unique) I have to include the unique column "pet_id" which is my primary key. The correct way to do this is: $order_by = 'town_lost ASC, pet_id'; with the primary key being the second sort and with no ASC or DESC In other words, Larry had it correct... sorry Larry!
  2. Yes, exactly! This worked perfectly: $order_by = 'pet_id DESC, town_lost DESC'; First, order by the primary key "pet_id", then the town names (it's really towns I use not states) follow in the same order. No duplication, no missed data. I never knew I could nest one ORDER BY inside another. thanks!
  3. I have run into a bit of a problem where I get repeated row results on separate pages within a paginated SELECT query. It works okay if I sort on, say, the unique "pet_id" column. But if I SELECT and sort by a column that can have similar items in the column (such as sort by state), it's very possible that results for row 15 (which comes up on page 4), for example, also shows up on page 7. This is a problem not just because things are repeated within the same query, but also, because I am using "LIMIT $start, $display", and if one $result row gets on the page twice, that means that another row (that should appear) is not getting on at all! I tried using SELECT DISTINCT but, of course, that is for a use within a single query. Every page number in the pagination scheme is a separate query. Thanks for any offerings. chop
  4. On page 516 script 16.9: Line 20: setcookie (session .... //DESTROYS COOKIE This line destroys a cookie set earlier (that was set in login.php? or in the Header?) However I cannot find the corresponding cookie that was set. The value, i see, is PHPSESSID thank you Also, is it always necessary to set a cookie when using a Session?
  5. My book is PHP 6 and MySQL 5, though it doesn't have an edition printed on it (copyright 2008). I might be in the wrong forum but the 3rd edition forum for this book doesn't seem to be active. That said, I am in chapter 16, looking at the code for login.php. I can't find any code for setting cookies in it though the logout.php has the code for destroying the cookies. Unless my eyes have gone buggy, I just can't find it. It seems like the login relies just on sessions. Also, login.php page does print the text "Your browser must allow cookies in order to log in" What am I missing?
  6. Thanks, Larry - I'm glad to hear you say it's tricky. I felt like I was asking another dumb question because it seemed like it should be easy. Too bad I just can't set NULL as a default for the whole column. That would make it easy, then write over it with the md5() when it should be validated. Anyway, I'll probably keep the code as 2 queries written under two conditionals. If I've learned nothing else about programming, it's that code should be easy to understand at first glance after not seeing it for a few months. It might come at the cost of a few extra lines of code but, if I am able to immediately see the logic, it's worth it. P.S. My PHP6 and MySQL5 book is in several chunks now after having rebound it twice. I admit I've not been easy on it though. Hope to get a newer version some day when one comes out (??) chop
  7. Is is possible to Insert a NULL value into a table by assigning it to a variable. For a low level user I want the activated column to have the following inserted: $a = md5( uniqid( rand(), true ) ); // put 32 char code into column "activated" for email verification But for a higher level user, I don't want them to have to verify their email address. So I thought I could set $a = "NULL" or $a= NULL I only end up with the word NULL instead a real null . I also thought there might be away of setting the column to default to NULL. Then the md5 () would write over it and that would solve the problem. But I couldn't do that either. Now I just have two separate INSERT queries under a conditional with a "SET activated=NULL" for one of them. It works, but I hate writing the code twice. thanks, chop
  8. Okay, I found it.. a <?php ?> in the line above. I think you probably meant I "could" be printing something instead of "should". thanks
  9. I should mention that there is an index.html it should go to. Also, I have tried it with a specific page.php as part of the location.
  10. Sorry, I copied it incorrectly. The code is as follows. Also, I don't know what you mean when you say "print". Do you mean a php print statemet? What is the purpose of that? $url = 'index.php'; // Define the URL. echo "Hello, I should be redirecting now..."; header('Location: https://tenthousandeyes.org'); exit(); // Quit the script.
  11. I've used header() a hundred times. For some reason this just gives me a blank screen. The url exists and the script gets into the conditional and exits. This is a modification of Larry's script in chapter 16 that I get get to work because of this. thanks very much if (!isset($_SESSION['first_name']) || $_SESSION['user_level'] == 0 ) { echo "Hello, I should be redirecting now..."; header('Location: https://tenthousandeyes.org/.php'); exit(); // Quit the script. }
  12. I'm getting old... It's not the code that's at fault, it's the non-code. That is the lack of code in line number 1, which is blank. A blank space outside of any <?php ?> tags is also known as "whitespace". I swore I'd never fall victim to whitespace again after the first 3 times. header() cannot follow any whitespace, ever. header() cannot follow any whitespace, ever. header() cannot follow ANY whitespace, ever. Officially, the php documentation: "Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include(), or require(), functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file" There. That should do it! It is now my mantra. It will never happen again... really
  13. Here is a snippet of code right out of this book. I have been using it for years and it has always worked. Now, it just seems to die on me. It is supposed to take me from login.php to logged in.php. Instead, it goes to the line right after the header(), prints it and stops via exit(). The url in the browser still shows I am at login.php. There are no errors generated. After I log in with login.php (and it stops without going to loggedin.php), I will type the url for loggedin.php directly into the browser and it will get me to loggedin.php with the session already running. I.E., the privileges will all be set. So why isn't "login.php" redirecting me to "loggedin.php"? <?php # Script 11.8 - login.php #3 USING SESSIONS if (isset($_POST['submitted'])) { require_once ('includes/login_functions.inc.php'); require_once (CONNECTION); list ($check, $data) = check_login($dbc, $_POST['email'], $_POST['pass']); if ($check) { // OK! // Set the session data:. //session_name('glee');// this will refer to the last session by name instead of starting a new one ob_start();// start output buffering session_start(); $_SESSION['user_id'] = $data['user_id']; $_SESSION['first_name'] = $data['first_name']; $_SESSION['last_name'] = $data['last_name']; $name = $_SESSION['first_name'].$_SESSION['last_name']; // Redirect: $url = absolute_url ('loggedin.php'); //line below isn't working even though the value for $url is //http://localhost:8888/sites_in_progress/gill_truslowBS4/loggedin.php //(which is correct) header("Location:$url"); ob_end_clean();// stop output buffering echo "We have now gone past the header() function and are still at login.php"; exit(); } else { // Unsuccessful! $errors = $data; } mysqli_close($dbc); } // End of the main submit conditional. include ('includes/login_page.inc.php'); // THIS INCLUDES HEADER.HTML ?>
  14. Hi Larry- I just noticed that you have PHP for the Web 5th edition available last month. I have a couple of questions: 1. An Amazon preview of the book is avail. for Kindle but not paperback. Is there much difference in content between the two? Much rather have the physical book. 2. I own PHP 6 and MySQL 5. Though mySQL is not in the title of PHP for the Web 5th edition, it has chapters on it. So, what is the main difference between the two? From what I understand, you are due to come out with the next edition of PHP (without the 6) and MySQL 5 this fall. In light of the fact that I already own the former version, would I, in your opinion, be better off to wait for the new version or purchase the PHP for the Web 5th edition book. Most of my interest is in building websites. thanks, (p.s.) My current copy has been through hell with my cats and having been out for a day in a thunderstorm. Thanks to Shoe Goo and some heavy cloth repair material, it is still usable but I feel it has earned at least a sem-retirement status.
  15. Peter- I don't know the answer to your first question but, as for the second (and I assume you are talking about using it for PHP code), no, don't use Word. Use a plain text editor. such as notepad. Better yet, get a free code editor that will give you the functions you need most like code highlighting and line numbering etc. Do a search for "Best free php code editors" or maybe someone here can suggest something for Windows, if that's what you are using.
  16. (I tried to edit above once more but ran out of time so... last words) My own way of thinking is that "it" would have known that the first 'image_id' meant 'art.image_id' because it is followed by the phrase 'FROM art' which, should be the default table for any column to its left that is NOT prefaced by 'table_name.' . Make sense? Anyway, I'm not going to write anyone a letter explaining the world according to Chop. I have what I need, working code Thanks for hanging in there, Larry.
  17. Wow! Works Perfectly now. Who'd a thunk it? For the record, this is the working code; SELECT image_title, art.image_id, COUNT(other_sales.image_id) FROM art JOIN other_sales ON (art.image_id=other_sales.image_id) GROUP BY (other_sales.image_id) I changed : "SELECT image_title, image_id" to "SELECT image_title, art.image_id" Just one last thought about ambiguity: Why, then, doesn't it think that within "SELECT image_title, art.image_id," that 'image_title' is also ambiguous and should be 'art.image_title' ? Could it be that 'image_id' is used more that once and is being referenced from 2 different tables? I'm hanging this one on my wall.
  18. I tried that first thing: SELECT image_title, image_id, COUNT(other_sales.image_id) FROM art JOIN other_sales ON (art.id=other_sales.image_id) GROUP BY (other_sales.image_id) Now I get: #1052 - Column 'image_id' in field list is ambiguous I don't know why it is ambiguous. COUNT(other_sales.image_id) is the column where I wan to count how many times the number (image_id) is listed. I couldn't find any examples like this on a Google search.
  19. I ran your suggested query in phpMyAdmin. Here are the results: SELECT image_title, image_id, COUNT(other_sales.*) FROM art JOIN other_sales ON (art.id=other_sales.image_id) GROUP BY (other_sales.image_id) LIMIT 0, 30 MySQL said: Documentation #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 art JOIN other_sales ON (art.id=other_sales.image_id) GROUP BY (other_s' at line 1 Open new phpMyAdmin window I've been trying other variations and have downloaded the MySQL Manual (1000 pages) to find a close example.
  20. Don't know why I didn't think about that. Probably because all the queries I've ever needed have been simple enough to just write them out and they'd work. If I remember correctly, PHP admin will also give me the php code for queries I run. Queries with joins (of different sorts) have always been a weakness of mine. So perhaps messing around in this way will be good exercise. Thanks for the suggestion, I will post the result if I get something that works.
  21. thank you for that work.. I assume you meant art.image_id in "ON (art.id=other_sales.image_id)" I tried the following and it works: 1. THIS WORKS: $q = "SELECT image_id, image_title FROM art ORDER BY $order_by"; 2. THIS DOES NOT WORK: $q = "SELECT image_id, image_title, COUNT(other_sales.*) FROM art JOIN other_sales ON (art.image_id=other_sales.image_id) GROUP BY (other_sales.image_id)" 3. THIS DOES NOT WORK: $q = "SELECT image_id, image_title, COUNT(other_sales.*) FROM art" I did number 3 just to test if the error might be in the JOIN part of number 2 In each case I get: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given I also tried it without the comma after "image_title," in case the COUNT() didn't count as as the last item in the list. ------- I didn't know that you could put COUNT(other_sales.*) before the FROM art because it is referring to another table (it is to the left of FROM art. I'm glad to have learned that! Sorry to be a pain! I've searched other sites for the answer but I can't find anything that specifically illustrates this. Also, other forums don't seem to explain things quite as well as you (and some others) on your forums. Also, I rechecked the table names.. no problem there. Would it help if I included a snapshot of the mySQL tables? Probably not but here they are anyway: table art table other_sales on this table, for instance, I am trying to count how many times image_id 369 is listed in the image_id column. The 369 refers to the unique image_id in the art table. Sorry if I'm over explaining, but it's as much for me as anyone else.
  22. I have 2 tables for which I want to write a select query containing an inner join: Table 1 is "art" Table 2 is "other_sales" the (simplified) query so far looks like this (without the JOIN) : $q = "SELECT image_id, image_title FROM art ORDER BY $order_by"; The" image_id" here is a primary key which is also used as a foreign key in the table "other_sales". Let's say that the image_id in "art" = 7. The number 7 may appear any amount of times in the table "other_sales" under the foreign key "image_id" I am trying to write a query that creates a report something like this" A Sunny Day, 7, 12 Where "A Sunny Day" is the image_title (from "art") 7 is the image_id (from "art"- primary key) 12 is the amount of times that the number (7) appears in the column "image_id" (foreign key) in table "other_sales" I have tried many different ways to work this with only the error " no result for $r" -- which basically means the query isn't working. Here is one example of what I have tried: $q = "SELECT image_id, image_title FROM art INNER JOIN other_sales ON other_sales.image_id = art.image_id $order_by"; I know that I have to include a COUNT(*) somewhere in there and have tried in many ways. I sifted through the joins section in the book and found COUNT() on page 178 but that just returns the number of values in a column. I have used inner joins before but not this complicated. I hope I made this clear without going on too much. thanks
  23. Well, okay, not quite onto other things... just one more complication here. I need to add an anchor to the code below: header( 'Location: http://localhost:8888/sites_in_progress/PNE_development/edit_image.php?id='.$image_id ) ; so that it redirects to a specified location on the page (id="this_location") on the page. I can do it without the variable added on to the header() but not with it there.
  24. Okay, I think I have it: header( 'Location: http://localhost:8888/sites_in_progress/PNE_development/edit_image.php?id='.$image_id ) ; As a final end to this post, the redirect actually does work and I am not having any more problem with the browser refreshing and re-posting duplicate values. The main problem was that the redirect needed to be accompanied with a value that I needed carried over to the next page, which i finally did (above). I also discovered that a "headers have already been sent" error will be caused by things that can easily be overlooked, such as html that has been commented out (seemingly benign). Or by <?php header with a space after it. Unforgiving it is. Also went back to your book & re-read about output buffering which helped. thanks for you assistance.. onto other things
×
×
  • Create New...