Note that there are some explanatory texts on larger screens.

plurals
  1. POimproper check of mysql result?
    text
    copied!<p>Why do I still get a </p> <blockquote> <p>Warning: mysql_fetch_assoc() expects parameter 1 to be resource, string given in</p> </blockquote> <p>when using the following function:?</p> <pre><code>public function getRowsWhere($table, $order_by, $where_field, $where_match, $limit_start = 0, $limit_end = 1) { // query Table $query_string = "SELECT * FROM $table WHERE $where_field='$where_match' ORDER BY '$order_by' DESC LIMIT $limit_start, $limit_end"; // run query $sql = new DBQuery($this-&gt;database); $result = $sql-&gt;prepareQuery($query_string); if ($result) { // initialize results array $match_array = []; // iterate through array and place results in an array at row index $r = 0; while ($row = mysql_fetch_assoc($result)) { $row_array = []; $c = 0; while ($c &lt; mysql_num_fields($result)) { $col = mysql_fetch_field($result, $c); $row_array[$col-&gt;name] = $row[$col-&gt;name]; $c++; } $match_array[$r] = $row_array; $r++; } } // return data return $match_array; } </code></pre> <p>Which is called from the following:</p> <pre><code>if (!$breadcrumbs-&gt;getCrumb(2)) { $project_details = $copy-&gt;getRows('projects', 'id', 'DESC', 0, 1); } else { $project_details = $copy-&gt;getRowsWhere('projects', 'id', 'id', $breadcrumbs-&gt;getCrumb(2), 0, 1); if (!$project_details) { $project_details = $copy-&gt;getRows('projects', 'id', 'DESC', 0, 1); } } </code></pre> <p>As requested: DBQuery class. It's all fairly basic stuff.</p> <pre><code>&lt;?php class DbQuery extends DbMan { /** * PARAMETERS * * ^ @param array $database -&gt; database connection information * -------------------------------------------------------------------------- */ protected $database; /** * -------------------------------------------------------------------------- * PUBLIC * __construct () * = set database object to use * * ^ @param string $database -&gt; database connection information * -------------------------------------------------------------------------- * */ public function __construct($database) { $this-&gt;database = $database; } /** * -------------------------------------------------------------------------- * PUBLIC * prepareQuery () * = checks Sql, executes query to return requested data set * * ^ @param string $query_request -&gt; query string * ~ @return object $result_dataset -&gt; result data set * -------------------------------------------------------------------------- * */ public function prepareQuery($query_request) { // retrieve result dataset $result_dataset = $this-&gt;executeQuery($query_request); // return result data set return $result_dataset; } } </code></pre> <p>And the class it extends, DBMan:</p> <pre><code>&lt;?php class DbMan { /** * PARAMETERS * * ^ @param array $database -&gt; database connection information * @param object $mysql_connection -&gt; database connection object * @param object $mysql_connection -&gt; database object * -------------------------------------------------------------------------- */ protected $database; private $mysql_connection; private $mysql_db; /** * -------------------------------------------------------------------------- * PUBLIC * __construct () * = empty constructor * * ^ @param -&gt; * -------------------------------------------------------------------------- * */ public function __construct() { } /** * -------------------------------------------------------------------------- * PROTECTED * dbConnect () * = open connection to MySql and connect to database * * ^ @param -&gt; * -------------------------------------------------------------------------- * */ protected function dbConnect() { // connect to MySQL $this-&gt;mysql_connection = mysql_connect($this-&gt;database['hostname'], $this-&gt;database['username'], $this-&gt;database['password']) or die('Unable to connect to MySQL : ' . mysql_error()); // connect to database $this-&gt;mysql_db = mysql_select_db($this-&gt;database['database'], $this-&gt;mysql_connection) or die('Unable to connect to database : ' . mysql_error()); } /** * -------------------------------------------------------------------------- * PROTECTED * executeQuery () * = run query on database * * ^ @param string $query -&gt; sql statement string * ~ @return result $query_result -&gt; result of query * -------------------------------------------------------------------------- * */ protected function executeQuery($query) { // open connection to the database $this-&gt;dbConnect(); // run sql query on database $query_result = mysql_query($query) or die('Could not run query on table:&lt;br /&gt;' . mysql_error()); // close database connection mysql_close($this-&gt;mysql_connection); // Check to make sure data is returned if (mysql_num_rows($query_result)) { // return result data set return $query_result; } else { // return nothing return "No Data Available"; } } } </code></pre> <p>I hope this makes this clearer..</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