erzulie Posted January 9, 2013 Share Posted January 9, 2013 Hello I tried to move the bold section of the below code into a function getWeights() located in a m_weight.php file (I want to implement MVC pattern) <?php $get_weights_q = mysql_query (" SELECT * FROM users_weight WHERE weight_uid = '$user_id' ORDER BY weight_date DESC ", toctoc() ) or die(mysql_error() ); while ($weight = mysql_fetch_array($get_weights_q)) { ?> <li> <?php echo date( " d M-y H:i (l) " , $weight['weight_date']). ' : ' . $weight['weight'] . ' <a href="'.$_SERVER['SCRIPT_NAME'].'?del=' . $weight['weight_id'] . '">Changer</a> ' ; } ?> </li> </ul> ---------------- Here is the function: function getWeights($user_id) { $get_weights_q = mysql_query (" SELECT * FROM users_weight WHERE weight_uid = '$user_id' ORDER BY weight_date DESC ", toctoc() ) or die(mysql_error()); } The result is : Notice: Undefined variable: get_weights_q in H:\home\user\documents\DEV-H\EasyPHP-5.3.8.1\www\Food-Tribe\v_weight.php on line 44 Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in H:\home\user\documents\DEV-H\EasyPHP-5.3.8.1\www\Food-Tribe\v_weight.php on line 44 If I let the code just above the While, in procedural mode it works fine... Any idea? Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 9, 2013 Share Posted January 9, 2013 You need to read up on variable scope. Quote Link to comment Share on other sites More sharing options...
erzulie Posted January 9, 2013 Author Share Posted January 9, 2013 (edited) You need to read up on variable scope. Aie Sorry that is pure esoterism for me. Could you elaborate? "print_r($get_weights_q) " within the function, gives me Resource id #7 and out of the function above the while:Undefined variable... Edited January 9, 2013 by erzulie Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 9, 2013 Share Posted January 9, 2013 Did you google PHP variable scope? If it's too complicated what part of it don't you understand? Quote Link to comment Share on other sites More sharing options...
erzulie Posted January 9, 2013 Author Share Posted January 9, 2013 Did you google PHP variable scope? If it's too complicated what part of it don't you understand? Thanks for clarifying I did now and I globalized the variable $get_weight_q Here's the new code: function getWeights($user_id) { global $get_weights_q ; $get_weights_q = mysql_query (" SELECT * FROM users_weight WHERE weight_uid = '$user_id' ORDER BY weight_date DESC ", toctoc() ) or die(mysql_error()); } It works! thank you so much .; I'm in love Take care Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 9, 2013 Share Posted January 9, 2013 *smh*. You shouldn't use global. You should either put all of the logic that uses that variable in the function, (all your sql stuff) or return the variable you need. Quote Link to comment Share on other sites More sharing options...
erzulie Posted January 10, 2013 Author Share Posted January 10, 2013 (edited) *smh*. You shouldn't use global. You should either put all of the logic that uses that variable in the function, (all your sql stuff) or return the variable you need. Damn ... Here I was thinking i'd hit the holy grail... So you got me thinking.Clearly I didn't know how to return an sql array. Here's the new code for the function: function getWeights($user_id) { //global $get_weights_q ; $get_weights_q = mysql_query (" SELECT * FROM users_weight WHERE weight_uid = '$user_id' ORDER BY weight_date DESC ", toctoc() ) or die(mysql_error()); return $get_weights_q ; } Bye bye global, I simply return the sql_query and call it from the controller file (c_file.php) $get_weights = getWeights($_SESSION['id']); then while it in the view_file: while ($weight = mysql_fetch_array($get_weights)) It now works. You are food for thought, thank you Edited January 10, 2013 by erzulie Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 10, 2013 Share Posted January 10, 2013 Much better Quote Link to comment 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.