Jump to content



Photo

Function For Mysql

mysql php functions

  • Please log in to reply
4 replies to this topic

#1 nick

nick

    Member

  • Members
  • PipPip
  • 29 posts

Posted 5 August 2012 - 10:44 PM

How can I change this into a function and include the file and call the function
I assumed it would be something similar to this but I guess I was wrong. I was hoping for some ideas thanks

funtion function_name($id){
$qv2='SELECT * FROM coaches WHERE sport_id = $id';
$rv2 = mysqli_query($dbc, $qv2);

if(mysqli_num_rows($rv2) > 0){

while($row = mysqli_fetch_array($rv2, MYSQLI_ASSOC)) {
$title = $row['title'];

if($title == 'Head Coach'){
echo'<p align="left" style="font-family: Verdana, Geneva, sans-serif; font-size: 10pt; text-align: center;"> ' ;
echo $row['coach_name'] . '&nbsp;' . $row['title'];
echo "\n<br />\n";
echo '<a href="mailto:' . $row['coach_email'] . ' ">' . $row['coach_email'] . '</a>';
echo "\n<br />\n";
echo "<br />\n";
//echo "\n</p>";
}



if ($title != 'Head Coach'){
echo $row['coach_name'] . ' - ' . $row['title'];
echo '<br />';

}
}

echo'</p>';
}else{echo 'nothing here to see';}


}
  • 0

#2 HartleySan

HartleySan

    Advanced Member

  • Members
  • PipPipPip
  • 2,892 posts
  • LocationColumbus, OH USA

Posted 5 August 2012 - 11:06 PM

Make sure you change the "funtion" keyword to "function". Other than that, just put the code in between some PHP tags, save the file and include it. For example:

File name: php_function.php
<?php

  function function_name($id) {

    // Code here

  }

?>

File in which you want to include the php_function.php file:
<?php

  include (path-to-file/php_function.php);

  // More code here. After the file is included, you can call the function.

?>

That's about it.
  • 1

#3 nick

nick

    Member

  • Members
  • PipPip
  • 29 posts

Posted 6 August 2012 - 9:04 AM

I had tried that and was getting an error. saying a system error had occured. because of the code i am using from the book. I am including a header file a config file and a mysql file where should this include be placed.

Also it should not matter it I have the code laid out like this should it


<?php

include('whateverFile/etc...');
include(path-to-file/php_function.php);

?>

Some Html

<?php some more code?>

more html
so on and so on

Hope this makes sense
  • 0

#4 nick

nick

    Member

  • Members
  • PipPip
  • 29 posts

Posted 6 August 2012 - 9:33 AM

It is saying that the $dbc file is Undefined variable: dbc
  • 0

#5 Larry

Larry

    Administrator/Writer

  • Administrators
  • 3,842 posts
  • LocationState College, PA (USA)

Posted 6 August 2012 - 10:58 AM

Then you have to make the database connection available within the function.
  • 0