Jump to content
Larry Ullman's Book Forums

MikeMikeMike

Members
  • Content Count

    55
  • Joined

  • Last visited

Everything posted by MikeMikeMike

  1. Hi Larry, I have two different answers to your question. However, either way it is the same problem. First, i was using MySQL 5.1 (I believe) set up through MAMP. Second, since my post about this problem, I updated MAMP and now I'm using MySQL 5.5.9. I tried the workbench again and it looks exactly the same as in my original post. I'm working on a MacBook Pro OS 10.6.6 Thanks for any guidance you can provide.
  2. Hi, I'm reading the 2007 edition of PHP 5 Advanced Techniques, where Larry tells us to download MySQL Administrator and then to create a function. MySQL Administrator is End of Line, so I had to download MySQL Workbench. I am in the SQL Editor, but it doesn't have a "Create Function" button. The closest thing I see is "Add Routine" But when I click to add a routine, it looks like this It uses language like "Create Procedure" and DELIMITER $$. I erased all of the code that it prefilled and inserted the "CREATE FUNCTION" SQL from the download (see below), but when I inserted it, it had several syntax errors, so I think it was expecting something different. Am I supposed to integrate this SQL somehow with this Create Proceudure prefilled language? How? Thanks Note, I also tried entering the function in the Query1 box as shown below, but it had 3 syntax errors and I'm not even sure that's the right place to put it. CREATE FUNCTION zips.return_ distance (lat_a DOUBLE, long_ a DOUBLE, lat_b DOUBLE, long_b DOUBLE) RETURNS DOUBLE BEGIN DECLARE distance DOUBLE; SET distance = SIN(RADIANS(lat_a)) * SIN(RADIANS(lat_) + COS(RADIANS(lat_a)) * COS(RADIANS(lat_) * COS(RADIANS(long_a - long_); RETURN((DEGREES(ACOS(distance))) * 69.09); END
  3. I'm on page 99 of Advanced PHP 5. I was able to enter the U.S. zip code file into zips database following Larry's instructions. I then crated the "stores" table successfully. Larry then suggestst that we enter some sample data into the stores table, so I downloaded the sql (see below) from his website. However, after I copied and pasted it into the command line, all it did was respond with '> . In other words, it did nothing. I checked the database through phpmyadmin and stores is empty You can see from the image on my command line that about half way down the arrows -> changed to '> . I don't know if that means anything. Any ideas what might be going wrong? INSERT INTO stores (name, address1, address2, zip_code, phone) VALUES (‘Ray''s Shop', ‘49 Main Street', NULL, ‘63939', ‘(123) 456-7890'), (‘Little Lulu''s', ‘12904 Rockville Pike', ‘#310', ‘10580', ‘(123) 654- 7890'), (‘The Store Store', ‘8200 Leesburg Pike', NULL, ‘02461', ‘(123) 456- 8989'), (‘Smart Shop', ‘9 Commercial Way', NULL, ‘02141', ‘(123) 555-7890'), (‘Megastore', ‘34 Suburban View', NULL, ‘31066', ‘(555) 456-7890'), (‘Chain Chain Chain', ‘8th & Eastwood', NULL, ‘80726', ‘(123) 808-7890'), (‘Kiosk', ‘St. Charles Towncenter', ‘3890 Crain Highway', ‘63384', ‘(123) 888-4444'), (‘Another Place', ‘1600 Pennsylvania Avenue', NULL, ‘05491', ‘(111) 456- 7890'), (‘Fishmonger''s Heaven', ‘Pier 9', NULL, ‘53571', ‘(123) 000-7890'), (‘Hoo New', ‘576b Little River Turnpike', NULL, ‘08098', ‘(123) 456-0000'), (‘Vamps ‘'R'' Us', ‘Our Location', ‘Atwood Mall', ‘02062', ‘(222) 456- 7890'), (‘Five and Dime', ‘9 Constitution Avenue', NULL, ‘73503', ‘(123) 446- 7890'), (‘A & P', ‘890 North Broadway', NULL, ‘85329', ‘(123) 456-2323'), (‘Spend Money Here', ‘1209 Columbia Pike', NULL, ‘10583', ‘(321) 456- 7890');
  4. My question relates to the code for view_tasks2.php (copied below) on or about page 35 in larry's book. About 3/4 of the way down the code you will see this snippet // Complete the query and execute: $q = substr($q, 0, -2) . ')'; $r = mysqli_query($dbc, $q); I am wondering why he's decided to set the substr parameters to 0 and -2. I believe -2 in this case will mean that it finishes 2 from the end of the length of q, but why do that? Thanks if you can help. <!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" xml:lang="en" lang="en"> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> <title>View Tasks</title> </head> <body> <h3>Current To-Do List</h3> <?php # Script 1.5 - view_tasks2.php /* This page shows all existing tasks. * A recursive function is used to show the * tasks as nested lists, as applicable. * Tasks can now be marked as completed. */ // Function for displaying a list. // Receives one argument: an array. function make_list ($parent) { // Need the main $tasks array: global $tasks; // Start an ordered list: echo '<ol>'; // Loop through each subarray: foreach ($parent as $task_id => $todo) { // Display the item: // Start with a checkbox! echo <<<EOT <li><input type="checkbox" name="tasks[$task_id]" value="done" /> $todo EOT; // Check for subtasks: if (isset($tasks[$task_id])) { // Call this function: make_list($tasks[$task_id]); } // Complete the list item: echo '</li>'; } // End of FOREACH loop. // Close the ordered list: echo '</ol>'; } // End of make_list() function. // Connect to the database: $dbc = @mysqli_connect ('localhost', 'root', 'root', 'test', 8889) OR die ('<p>Could not connect to the database!</p></body></html>'); // Check if the form has been submitted: if (isset($_POST['submitted']) && isset($_POST['tasks']) && is_array($_POST['tasks'])) { // Define the query: $q = 'UPDATE tasks SET date_completed=NOW() WHERE task_id IN ('; // Add each task ID: foreach ($_POST['tasks'] as $task_id => $v) { $q .= $task_id . ', '; } // Complete the query and execute: $q = substr($q, 0, -2) . ')'; $r = mysqli_query($dbc, $q); // Report on the results: if (mysqli_affected_rows($dbc) == count($_POST['tasks'])) { echo '<p>The task(s) have been marked as completed!</p>'; } else { echo '<p>Not all tasks could be marked as completed!</p>'; } } // End of submission IF. // Retrieve all the uncompleted tasks: $q = 'SELECT task_id, parent_id, task FROM tasks WHERE date_completed="0000-00-00 00:00:00" ORDER BY parent_id, date_added ASC'; $r = mysqli_query($dbc, $q); // Initialize the storage array: $tasks = array(); while (list($task_id, $parent_id, $task) = mysqli_fetch_array($r, MYSQLI_NUM)) { // Add to the array: $tasks[$parent_id][$task_id] = $task; } // For debugging: //echo '<pre>' . print_r($tasks,1) . '</pre>'; // Make a form: echo '<p>Check the box next to a task and click "Update" to mark a task as completed (it, and any subtasks, will no longer appear in this list).</p> <form action="view_tasks2.php" method="post"> '; // Send the first array element // to the make_list() function: make_list($tasks[0]); // Complete the form: echo '<input name="submitted" type="hidden" value="true" /> <input name="submit" type="submit" value="Update" /> </form> '; ?> </body> </html>
×
×
  • Create New...