ExamBank Scot · AH Computing Science question bank · All AH Computing Science questions
14 marks · PHP database functions
The website must connect to a database called ‘PetStyling’ which is hosted on the database server ‘pawsomeServer’ with the username ‘petStylist’ and password ‘pa$$word!23’. The code below is used to implement step 2 of the design, but generates an error. mysqli_connect("PetStyling", "pawsomeServer", "petStylist", "pa$$word!23"); Rewrite this code to correct the error. When the search is executed by the database server, one of the following three outcomes will be generated: • execution failed • no bookings found for date provided • 5 bookings found — details in the table below. Before the search is executed at step 4, a connection with the database server is established and assigned to the PHP variable $connection and the SQL query statement is stored in the PHP variable $query. Write the code to check for successful execution of this query and assign the results to the PHP variable $results. One of the stylists uses the search facility to check her appointments for 23 May 2026. Once step 4 of the search process has been executed, the contents of $results is shown below. apptDate apptTime petName 23‑05‑2026 9 am Buddie 23‑05‑2026 11 am Spike 23‑05‑2026 12 pm Rosie 23‑05‑2026 1 pm Goldie 23‑05‑2026 2 pm Disco Using pseudocode, refine step 6 to process the search results and display the list of appointments shown in Screenshot 1 on the previous page. Screenshot 2 below shows the page used by the stylists to rate the behaviour of a pet during an appointment. Screenshot 2 The ratings used in the drop‑down list are assigned to the PHP variable $ratings as shown in the incomplete code below. <?php $ratings = ["Green", "Yellow", "Red"]; // drop‑down list of ratings … ?> Write the code needed to implement the drop‑down list using the values stored in $ratings. The updated behaviour ratings for each pet are stored in a database table called Pet. A sample of the data stored in this table is shown below. petID … greenRatings yellowRatings redRatings banned 098 … 10 1 0 FALSE 171 … 0 2 2 TRUE 340 … 4 2 0 FALSE 999 … 3 1 2 FALSE At the end of each day, an SQL query is used to retrieve the full details of all pets stored in the Pet table and assign the results to the PHP variable $allPets. The query results are then processed by applying the following rules: • Data associated with pets that have already been banned from the salon is ignored, for example petID 171. • 10 green ratings result in a 10% discount being awarded and the value in green ratings field is reset to 0. • 3 yellow ratings result in a 15% surcharge being added and the value in the yellow ratings field is reset to 0. • 2 red ratings result in the pet being banned from the salon; the red ratings value is not reset but the value of banned field is changed to TRUE. Using pseudocode, design an algorithm to process the query results. The output should indicate that a discount has been awarded, a surcharge has been added, or the pet has been banned. The database should be changed where appropriate. The manager of the salon needs to know how many pets have been banned. Write the SQL query to perform this task. The salon owner has decided to add an artificially intelligent chatbot feature to the website. State the type of maintenance that would be required in this situation. Justify your answer. The owner of a pet styling salon has recently introduced a database‑driven website. The website is used by pet stylists who work at the salon. Screenshot 1 below shows the search facility which is used by the pet stylists each morning to display a list of appointments for the day. Screenshot 1 The design of the search process is shown below. 1. Assign date submitted by user to $searchDate 2. Connect to database server 3. Assign SQL query statement to $query 4. Execute query and assign results to $results 5. Close database connection 6. Process search results
Connection parameters database server, and database are in the wrong order. See correct code below. Award 1 mark for correct sequence of connection parameters. mysqli_connect("pawsomeServer", "PetStylist", "pa$$word!23", "petStyling"); See sample solution below. Award 1 mark for correct use of mysqli_query() function with correct use of $results, $query and $connection variables. Award 1 mark for correct use of die() function. $results = mysqli_query($connection, $query) or die("Connection Failure"); See sample solution below. Alternative solutions are possible. Award 1 mark for display of appointment time with name of pet (traversing search results with step 7). Note: apptDate should not be displayed. Award 1 mark for comparing results returned with times to be displayed to display – when no pet has been booked 1. set $hours = [“9am”, “10am”, “11am”, “12pm”, “1pm”, “2pm”] 2. set count = numRows($results) 3. loop for time from 0 to 5 4. set found = false 5. loop for appointments from 0 to count-1 6. 7. 8. 9. 10. end loop 11. if found = false then if apptTime of current $results entry = $hours(time) then display apptTime of current $results entry and pet name of petID set found = true end if 12. 13. end if display apptTime of current $results entry and “-” 14. end loop See sample solution below. Award 1 mark for correct use of HTML SELECT with name and value attributes. Award 1 mark for correct use of $ratings to populate options. echo '<select name = "behaviourRating">'; foreach ($ratings as $rate) { echo '<option value = "'.($rate).'">'.($rate).'</option>'; } echo '</select>'; Alternative solution echo '<select name = "behaviourRating">'; for ($i=0; $i<2; $i++) { echo '<option value = "'.($ratings(i)).'">'.($ratings(i)).'</option>'; } echo '</select>'; See sample solution below. Award 1 mark for loop with numRows to process rows of $allPets but only processing details of pets that have not yet been banned. Award 1 mark for indicating output messages for all 3 conditions. Award 1 mark for indicating correct use of update queries for all 3 conditions. 1. loop for (number of rows in $allPets –1) 2. if current pet not banned then 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. end if 14. end loop if greenRatings of current pet = 10 then display pet details with discount message use an update query to set value of greenRatings = 0 else if yellowRatings of current pet = 3 then display pet details with surcharge message use an update query to set value of yellowRatings field = 0 else if redRatings of current pet = 2 then display pet details with banned message use an update query to set value of banned field = true end if SELECT COUNT(*) FROM PET WHERE banned = TRUE; Award 1 mark for use of SELECT query with COUNT(*) Award 1 mark for correct condition. Perfective maintenance • this is an additional feature that was not a requirement in the original software specification • system is working so there are no errors that require corrective maintenance to be carried out Award 1 mark for perfective maintenance. Award 1 mark for any valid reason.