MoFish Posted November 23, 2013 Author Share Posted November 23, 2013 (edited) abjnoob i'm still not entirely sure what you have been reading, but from my understand we do not have a LIMIT 1 mentioned through-out the posts in the thread (hence our confusion). I also do not understand how the KISS suggestion helps me in getting the first result to display in my array lol. Ch0cu3r I think your reading the correct thread. I tried your suggestion to use mysql_num_rows instead of fetch_array but this did not seem to help. Any other suggestions? Edited November 23, 2013 by MoFish Quote Link to comment https://forums.phpfreaks.com/topic/284200-for-each-unexpected-result/page/2/#findComment-1459706 Share on other sites More sharing options...
Solution Ch0cu3r Posted November 23, 2013 Solution Share Posted November 23, 2013 (edited) Right back on topic. Sorry MoFish for confusion. Hi, I updated the code as suggested - but it now returns no values. Can you clarify if i got this correct? Should the fetch_array still be in the while loop as I had previously? public function get_all_league() { $db = new sql(); $sql = "SELECT * FROM $this->tablename"; $db->query($sql); $data = $db->mysql_num_rows(); $section_object = new section_object(); if(!empty($data)){ while($k = $db->fetch_array($db)) { $total[] = $k; } } return $total; } Not quite correct. Change it to public function get_all_league() { $db = new sql(); $sql = "SELECT * FROM $this->tablename"; $db->query($sql); $section_object = new section_object(); if(mysql_num_rows()){ while($k = $db->fetch_array($db)) { $total[] = $k; } } return $total; } However it would be better to add mysql_num_rows() to a method in your sql class instead, example public function num_rows() { return mysql_num_rows(); } Instead of doing if(mysql_num_rows()){ you'd use if($db->num_rows()) instead. Edited November 23, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/284200-for-each-unexpected-result/page/2/#findComment-1459707 Share on other sites More sharing options...
objnoob Posted November 23, 2013 Share Posted November 23, 2013 Sorry, none of the advice i've given you is good advice. here's some good advice... don't use mysql_* functions. change it to mysqli_* and we'll talk. Quote Link to comment https://forums.phpfreaks.com/topic/284200-for-each-unexpected-result/page/2/#findComment-1459709 Share on other sites More sharing options...
MoFish Posted November 23, 2013 Author Share Posted November 23, 2013 (edited) Thank you objnoob. That has fixed the problem! Bravo!! I have created a num_rows in my sql class as suggested - function num_rows() { $mysql_rows = mysql_num_rows( $this->sql_result ); return $mysql_rows; } Edited November 23, 2013 by MoFish Quote Link to comment https://forums.phpfreaks.com/topic/284200-for-each-unexpected-result/page/2/#findComment-1459710 Share on other sites More sharing options...
MoFish Posted November 23, 2013 Author Share Posted November 23, 2013 Thank you for your help Ch0cu3r and objnoob. Quote Link to comment https://forums.phpfreaks.com/topic/284200-for-each-unexpected-result/page/2/#findComment-1459711 Share on other sites More sharing options...
objnoob Posted November 23, 2013 Share Posted November 23, 2013 Thank you for your help Ch0cu3r and objnoob. You're welcome. Here's some other advice.... Don't use SELECT * to select all of the columns . Specify the columns you're selecting.. be explicit and specific.SELECT column1, column2, column3 FROM tblMyTable; Don't duplicate code for a minor condition. one get leauge method that accepts the league id as 1 optional parameter is good. /** * returns a leauge with $id from $database . if no $id is given, all leauges are returned */ function getLeauge($database, $id = null){ if($id !== null && ! is_int($id)) throw new Exception('Don\'t be stupid.... validate, validate, validate. Make sure we got a integer datatype for $id'); $sql = 'SELECT league.id, league.name, league.captain FROM league'. (($id)?' WHERE league.id=' .$id:null); /* finish your code here, but here's no need to fumble around with get_league, get_all_league, redundancy_code, redundancy_code_, redundancy_code__ this function is nice because 1 is dual purpose, and 2 the database connection is injected which allows you test new database designs without changing this code....... also, if you decide to return the league division (just made this up) you can then add league.division to the $sql here and not have to worry about get_league, get_all_league, redundancy_code, redundancy_code_, redundancy_code__ you could even get crazy and accept $id an array of integers that would return more than one league by IDs. live, learn, love life don't forget to get rid of mysql_ and use mysqli_, or better yet PDO */ } Quote Link to comment https://forums.phpfreaks.com/topic/284200-for-each-unexpected-result/page/2/#findComment-1459718 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.