sql - PHP simple function not working -
i can't make simple echo.
i have admin.class.php
public static function get_quota() { return self::find_by_sql("select * quota"); } public static function find_by_sql($sql="") { global $database; $result_set = $database->query($sql); $object_array = array(); while ($row = $database->fetch_array($result_set)) { $object_array[] = self::instantiate($row); } return $object_array; }
and echo code in index.php
<?php $admin = user::find_by_id($_session['user_id']); $admin_class = new admin(); $get_quota = admin::get_quota(); $sql = "select * quota"; $get_quota = admin::find_by_sql($sql); ?> . . . <?php echo $get_quota->daily_a; ?>
so problem is, code not working. cannot echo data. can me, please?
you have couple of problems here:
<?php echo $get_quota->daily_a; ?>
this line references $get_quota
variable , searches member field daily_a
. try this:
<?php echo "quota is:".var_export($get_quota->daily_a,true); ?>
this show that empty variable.
however, note:
$get_quota = admin::get_quota(); $sql = "select * quota"; $get_quota = admin::find_by_sql($sql);
here calling 2 separate methods admin
, setting variable $get_quota
result. second overwrites first. therefore get_quota()
method doesn't here: need know find_by_sql()
method returns.
edit (post new code added question)
you can implement logging/echoing within function you've problem with:
public static function find_by_sql($sql="") { global $database; //note bad practice (1). $result_set = $database->query($sql); echo "result set: ".var_export($result_set,true)."\n";//this should return if you're getting database. also, remove runtime (obviously). if (count($result_set) <= 0) { //constraint checking good! , cheap! error_log("an unexpected number of rows (0) received database query='".$sql."'."); } $object_array = array(); while ($row = $database->fetch_array($result_set)) { $object_array[] = self::instantiate($row); //ensure instantiate function returns something! } echo "object array: ".var_export($object_array, true)."\n";//double-check instantiate function working return $object_array; }
based on code, problem instantiate function; if it's not returning anything, $object_array
empty. (but not null!).
(1) should avoid grabbing global variables this. instead, instantiate class holds , manages database connection. make find_by_sql
function non-static , have member field pointing database.
Comments
Post a Comment