Solved

using a php class to do a database search but return an array back

Posted on 2013-06-14
3
311 Views
Last Modified: 2013-06-14
Hi

I have the folling code in one of my classes. its intention is to take a SQL query and pass back an array of results for me to then use alse where, it doesnt seem to do this though, it seems to only send back 1 row?

	
public function executeQuery($query) {
				
      $db = new mysqli(_DBHOST, _DBUSER, _DBPASSWORD, _DBTABLE);
		
      if ($result = $db->query($query)) {	
	$row = $result->fetch_assoc();	
	$result->close();	
	$db->close();	
	return $row;
     }
		
}	

Open in new window



im calling it using :

$searchResultsRS = $db->executeQuery($itemSearchSql);

Open in new window



then, i wanted to pass this record (or records) to a template like this:

while ($row = mysqli_fetch_array($searchResultsRS)) {
      $items[] = array(
            'iID' => $row['iID'],
            'iCategory' => $row['iCategory'],
            'cName' => $row['cName']
                                      );
)



any ideas or tips to do this better please?
0
Comment
Question by:Neil Thompson
  • 2
3 Comments
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39247560
The first code snippet will only return one row.  If you want to get the entire results set, you would use an iterator like while() or maybe use PDO.  This article has parallel examples in different MySQL extensions.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html

If you want, please post the rest of the class and maybe we can come up with something that will make more sense than just this function.  It's missing a lot of things it needs, like error handling.
0
 
LVL 3

Author Closing Comment

by:Neil Thompson
ID: 39247907
Thanks Ray

I've fudged it to work at present as it was actually another search i needed to quickly demo.

Your tutorial is excellent so I'm now going to take a step back and have a good read and learn.

Are you aware of a good class that I can use to add/edit/delete/read from a database with all the great error handling etc build in?

Regards
Neil
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39248036
Thanks for the points.  I'm not aware of any database class.  I tend to write web services (rather than web sites) and as a result of needing ultra-high performance I usually write and optimize my own code.  I'm sure there is something that offers a generalized solution - I just don't know what it is!

Best regards, ~Ray
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question