Stephen68 Posted October 12, 2009 Share Posted October 12, 2009 Hello I have two function in my class, one runs a query and the other fetches the row/s. The problem is that it is sending back a multi dimension array. Any help would be great <?php #-----------------------------------------# # Fucntion for running query on the db # #-----------------------------------------# function query($sql) {## Start of query function $this->query_holder = mysql_query($sql,$this->conn); //Test to see if query ran if (!$this->query_holder) { //Query did not run show error message $this->errorMsg("<p>There was an error in running the query, please contact site admin."); return 0; } $this->numRows = mysql_affected_rows($this->conn); return $this->query_holder; }## End of query function #-------------------------------------------------# # Function for fetching all the information from # # the database. # #-------------------------------------------------# function fetchRecords ($rows) { $out = array(); while ($results = mysql_fetch_assoc($rows)) { $out[] = $results; } //Test to see if there are any errors if (!$rows) { $this->errorMsg('There was an error fetching record/s information, please contact site admin.'); return 0; } return $out; }#End of fetchReocrds function ?> There are more functions and what not in there for connect to db and stuff as well. Here is how I call it. <?php $db = new admin(bla, bla); $db->dbConnect(); $sql = "SELECT * FROM TABLE"; $results = $db->query($sql); $rows = $db->fetchRecords($results); print_r($rows); ?> The print_r puts out an 2-d array not sure why? Quote Link to comment https://forums.phpfreaks.com/topic/177382-query-giving-back-multi-array/ Share on other sites More sharing options...
Zane Posted October 12, 2009 Share Posted October 12, 2009 Well if it didn't print out a 2-d array you would only get one row from that select all.. is that what you're after This right here is what is making the 2D array... the little braces after out $out[] = $results; If you just want a 1D array then take them off $out = $results; Quote Link to comment https://forums.phpfreaks.com/topic/177382-query-giving-back-multi-array/#findComment-935251 Share on other sites More sharing options...
Stephen68 Posted October 12, 2009 Author Share Posted October 12, 2009 So if I will have to make another function that will just grab one for and another one that will grab more then one run? The one the will give me all the rows will just have to be a multi array? or is there a way I could make it a 1 d array? Quote Link to comment https://forums.phpfreaks.com/topic/177382-query-giving-back-multi-array/#findComment-935435 Share on other sites More sharing options...
trq Posted October 12, 2009 Share Posted October 12, 2009 No there is no way you could make multiple results into a one dimensional array, each record needs to go into its own index to keep separated. These classes really add nothing in the way of functionality. People however persist and insist in writing them over and over again. Quote Link to comment https://forums.phpfreaks.com/topic/177382-query-giving-back-multi-array/#findComment-935437 Share on other sites More sharing options...
Stephen68 Posted October 12, 2009 Author Share Posted October 12, 2009 Well I'm just really fooling around with classes for the first time and thought it would be neat to make this. I do find is useful in that I can now run an update query from a forum with just a few lines of code, I don't have to create the SQL. Quote Link to comment https://forums.phpfreaks.com/topic/177382-query-giving-back-multi-array/#findComment-935506 Share on other sites More sharing options...
Skepsis Posted October 12, 2009 Share Posted October 12, 2009 Change $out = array(); while ($results = mysql_fetch_assoc($rows)) { $out[] = $results; }to while ($results = mysql_fetch_assoc($rows)) { return $results; }I wouldn't use error reporting for that function, since the query function will return an error if you have found one already, so it would show two errors if the query function fails. Quote Link to comment https://forums.phpfreaks.com/topic/177382-query-giving-back-multi-array/#findComment-935519 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.