Jump to content

Return Mysql data from function


muffinsincream

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;

}

}

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.