muffinsincream Posted September 27, 2008 Share Posted September 27, 2008 I am trying to separate my view from my database more, because it is getting far to annoying to edit. I was trying to a make a function that would retrieve data from my MySQL database, so I could then use it in my view. Basically what I want the view to look like is: get_classes($user); echo '<b>' . $class['name'] . '</b><br/>'; How would I code the function that would make that work? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/126091-return-mysql-data-from-function/ Share on other sites More sharing options...
corbin Posted September 28, 2008 Share Posted September 28, 2008 As far as I can tell, the only relation between get_classes($user) and $class['name] is that both obviously pertain to classes. What exactly are you trying to do? Quote Link to comment https://forums.phpfreaks.com/topic/126091-return-mysql-data-from-function/#findComment-652193 Share on other sites More sharing options...
muffinsincream Posted September 28, 2008 Author Share Posted September 28, 2008 As far as I can tell, the only relation between get_classes($user) and $class['name] is that both obviously pertain to classes. What exactly are you trying to do? I want to keep my MySQL queries out of my view. In my view I would like to print a list of classes, and in my functions file I want to create the function get_classes. I want to keep all of the view code (html and stuff) in the view, and all the database access code in my functions file. Quote Link to comment https://forums.phpfreaks.com/topic/126091-return-mysql-data-from-function/#findComment-652206 Share on other sites More sharing options...
corbin Posted September 28, 2008 Share Posted September 28, 2008 You're talking about separating DB calls from a view and you don't know how to do this? What a strange order in which to learn PHP. Just make a function that returns what you want. function get_something() { return 'something'; //obviously this would be a DB call or something. } Then in your view: $var = get_something(); echo "Say something: {$var}"; (The handling of the return would of course differ depending on the return type. IE, if it were an array, you would loop through it or something instead of just straight editing it out.) Quote Link to comment https://forums.phpfreaks.com/topic/126091-return-mysql-data-from-function/#findComment-652208 Share on other sites More sharing options...
muffinsincream Posted September 29, 2008 Author Share Posted September 29, 2008 You're talking about separating DB calls from a view and you don't know how to do this? What a strange order in which to learn PHP. Just make a function that returns what you want. function get_something() { return 'something'; //obviously this would be a DB call or something. } Then in your view: $var = get_something(); echo "Say something: {$var}"; (The handling of the return would of course differ depending on the return type. IE, if it were an array, you would loop through it or something instead of just straight editing it out.) I know the basic use of functions, but I dont want get_something() to print the whole table. I want mysql data to display in a table, but I want all the table code in the view and all the mysql code in the functions file. Quote Link to comment https://forums.phpfreaks.com/topic/126091-return-mysql-data-from-function/#findComment-652737 Share on other sites More sharing options...
Acs Posted October 1, 2008 Share Posted October 1, 2008 Then why don't you just do that? It's not that hard, just do what you are saying. Put the mysql code in the function and the use a require or something to access them in the view. Quote Link to comment https://forums.phpfreaks.com/topic/126091-return-mysql-data-from-function/#findComment-654604 Share on other sites More sharing options...
getmukesh Posted October 1, 2008 Share Posted October 1, 2008 You can use this function for populate single field from table of spacific record function GetField($TableName,$InputFieldName,$InputFieldValue,$OutputFieldName) { $sql= "Select ".$OutputFieldName." from ".$TableName ." Where ". $InputFieldName."='".$InputFieldValue."'"; $res=mysql_query($sql) or die(mysql_error()) ; $str=""; if (mysql_num_rows($res) > 0 ) { $res_arr=mysql_fetch_array($res); $str=$res_arr[0]; } //echo $str; return $str; } ------------------------------------------------------------- Another is, using this you will get two dimensional array. like if you pass $arr = select_row("select * from employee"); you can retrieve the fields like 1st record == $arr[0]['name'] 2nd record == $arr[1]['name'] Please check it, if you have any problem, please let me know.. function select_row($sql) { if ($sql!="") { $result = mysql_query($sql) or die(mysql_error()) ; if ($result) { while($row = mysql_fetch_array($result)) $data[] = $row; } return $data; } } Quote Link to comment https://forums.phpfreaks.com/topic/126091-return-mysql-data-from-function/#findComment-654616 Share on other sites More sharing options...
muffinsincream Posted October 5, 2008 Author Share Posted October 5, 2008 Thanks, getmukesh I see what I must do. I was thinking about a single function before but i really need several, that makes complete sense. So glad someone thought of it. Quote Link to comment https://forums.phpfreaks.com/topic/126091-return-mysql-data-from-function/#findComment-657346 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.