Solved

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

Posted on 2013-06-14
3
309 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:NeilT
  • 2
3 Comments
 
LVL 108

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:NeilT
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 108

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

759 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now