Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat's going wrong here in oop?
    text
    copied!<p>I have the following code where it is meant to select everything from a the users table. </p> <pre><code>&lt;?php class DB{ protected $db_name = 'oop'; protected $db_user = 'root'; protected $db_pass = ''; protected $db_host = 'localhost'; //Open a connection to the database. Make sure this is called //on evey page that needs to use the database. public function connect(){ $connection = mysql_connect($this-&gt;db_host, $this-&gt;db_user, $this-&gt;db_pass); mysql_select_db($this-&gt;db_name); return true; } //Takes mysql row set and returns and associative array, where the keys //in the array are the column names in the row set. If singleRow is set to //true, then it will return a single row instead of an array of rows. public function processRowSet($rowSet, $singleRow=false){ $resultsArray = array(); while($row = mysql_fetch_assoc($rowSet)){ array_push($resultsArray, $row); } if($singleRow === true){ return $resultsArray[0]; } return $resultsArray; } //Select rows from the database. //Returns a full row or rows from $table using $where as the where clause. //Return value is an associative array with column names as keys. public function select($table, $where){ $sql = "SELECT * FROM $table"; $result = mysql_query($sql); if(mysql_num_rows($result) == 1){ return $this-&gt;processRowSet($result, true); } return $this-&gt;processRowSet($result); } //Updates a current row in the database. //Takes an array of data, where the keys in the array are the columns names //and the values are the data that will be inserted into those columns. //$table is the name of the table and $where is the sql where clause. public function update($data, $table, $where){ foreach ($data as $column =&gt; $value){ $sql = "UPDATE $table SET $column = $value WHERE $where"; mysql_query($sql) or die (mysql_error()); } return true; } //Inserts a new row into the database. //Takes an array of data, where the keys in the array are the column names //and the values are the data that will be inserted into those columns. //$table is the name of the table public function insert($data, $table) { $columns = ""; $values = ""; foreach ($data as $column =&gt; $value) { $columns .= ($columns == "") ? "" : ", "; $columns .= $column; $values .= ($values == "") ? "" : ", "; $values .= $value; } $sql = "INSERT INTO $table ($columns) VALUES ($values)"; mysql_query($sql) or die(mysql_error()); //return the ID of the user in the database. return mysql_insert_id(); } } ?&gt; </code></pre> <p>I am trying to call it like this:</p> <pre><code>$db = new DB(); $db-&gt;connect(); $db-&gt;select('users', ''); $results = $db-&gt;processRowSet(); print_r($results); </code></pre> <p>What am I doing wrong as I keep getting errors such as:</p> <blockquote> <p>Warning: Missing argument 1 for DB::processRowSet(), called in /opt/lampp/htdocs/xampp/www/oop/editProperty.php on line 15 and defined in /opt/lampp/htdocs/xampp/www/oop/classes/dbClass.php on line 22</p> <p>Notice: Undefined variable: rowSet in /opt/lampp/htdocs/xampp/www/oop/classes/dbClass.php on line 26</p> <p>Warning: mysql_fetch_assoc() expects parameter 1 to be resource, null given in /opt/lampp/htdocs/xampp/www/oop/classes/dbClass.php on line 26</p> </blockquote> <p>Your help would be very much appreciated. Thanks</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