Solved

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

Posted on 2013-06-14
3
315 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 110

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 110

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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
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 …

719 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