0

This is for a self test for a college job application so I am not necessarily looking for the exact statement I need, instead I am trying to get a hint as to what direction to look, I DO NOT have access to the actual database to view it:

This problem tasks us with using JOIN to select data about users from one table based on if they have a hold on their account on another table, the problem also ONLY wants students in the College that is given by the $college argument.

I am pretty new to PHP so I've been using w3schools as a guide and I have used their INNER JOIN example exactly, yet it keeps giving me a 'unknown column E(or any other letter given by $college) in 'where clause'', or if I change the "" to '' it changes to 'unknown column $college in 'where clause''

Here is my code:

<?php

function hasHold($college)
{
    $query = "SELECT interview_student.Student_ID, 
    interview_student.Last_Name, 
    interview_student.First_Name, 
    interview_student.College, 
    interview_account_hold.Amount_Due 
       FROM interview_student INNER JOIN interview_account_hold 
       ON interview_student.Student_ID = interview_account_hold.Student_ID 
       WHERE interview_student.College = $college";

    return $query;
}
?>

Assuming access to the interview_student table from the previous question...

+------------------------------------------------------+
| interview_student                                    |
+------------+-----------+------------+---------+------+
| Student_ID | Last_Name | First_Name | College | GPA  |
+------------+-----------+------------+---------+------+
| 463127376  | Willemsen | Lucas      | A       | 2.37 |
+------------+-----------+------------+---------+------+

The accounting office has provided us a list of students who currently have holds on their accounts because of unpaid tuition. They are only listed by ID and amount due; and they are only listed if they have an amount due. Students who have no amount due are not listed.

+-------------------------+
| interview_account_hold  |
+-------------------------+
| Student_ID | Amount_Due |
+------------+------------+
| 416996983  | 3,403.20   |
| 287856417  | 2,318.13   |
| 537713123  | 3,678.06   |
| 187296716  | 2,202.83   |
| 145768187  | 2,379.32   |
+------------+------------+

Write a function called hasHold that takes one argument ($college) and returns a string (a SELECT statement).

The argument passed to the function will be a college code (such as 'A' for Agriculture).

The string returned should be a SELECT statement that will return all the students who are in the given college that have an account hold in place. The resulting query should return the following fields in the following order: Student ID, Last Name, First Name, College, Amount Due.

Once again, the function should return the SELECT statement as a string. You do not need to handle the actual connection, submitting the query, parsing the results, etc. Just return a string.

applecrusher
  • 5,508
  • 5
  • 39
  • 89
  • Post the code, not images. You also aren't actually using `mysql` with the code you presented, that just builds a string. – chris85 May 06 '18 at 16:54
  • It works now I took down the photo and added the text from the problem page so you can see the table info they give. –  May 06 '18 at 16:57
  • 1
    Your `$college` is a string so it needs to be quoted. You also should use parameterized queries this is open to SQL injections. This should have the same behavior with/without a `join`. – chris85 May 06 '18 at 16:57
  • It works now thank you for your help, so I need to use '$college' since what it contains is a string, whereas on an earlier problem I did used $min with no quotes and it worked because $min is a float? –  May 06 '18 at 16:59
  • Yes, integers don't need to be quoted. You should use parameterized queries though. A user can manipulate this, or you could forget to put a quote somewhere. With parameterized the driver handles it all for you. – chris85 May 06 '18 at 17:01
  • thank you again I will read into parameterized queries –  May 06 '18 at 17:03
  • See http://php.net/manual/en/mysqli.quickstart.prepared-statements.php or http://php.net/manual/en/pdo.prepared-statements.php I'd recommend learning with the PHP/mysq manuals rather than w3. – chris85 May 06 '18 at 17:04

0 Answers0