Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are all kinds of things wrong with this class, so I will focus on a few key points and maybe you can fix it from there without us having a total rewrite</p> <p>First, consider this:</p> <pre><code>public $connection; function connectToDatabase() { //connect to database &amp; check connection $connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME); if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } } </code></pre> <p>You declare a class variable $connection, which is ok. But in your function you assign a local variable instead of accessing the class variable. From within a class, you would call all class members using $this. So the function should be:</p> <pre><code>function connectToDatabase() { //connect to database &amp; check connection $this-&gt;connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME); if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } } </code></pre> <p>Now, other methods attempt to call functions but they need to do the same thing:</p> <pre><code>function displayMenu() { //connect to database &amp; check connection // this should just check to see if $connection is populated. if not then call this method $this-&gt;connectToDatabase(); </code></pre> <p>And this method is not accessing the class var for connection:</p> <pre><code>function queryDatabase($select, $from) { $result = mysqli_query($this-&gt;connection,"SELECT $select FROM $from"); } </code></pre> <p>those few examples should give you the basic idea of accessing class methods and variables. You can ask specific questions and we may update, but for now this should get you going.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload