Your usage is off though...What you need to do is to include the functions.php page and then call a particular function..also any error handling should be with the function itself
I handle mine a little differently....try this: <?php //remote_conn.php //check the password from the DB and get the access level = type function conn($sql) { $username = "root"; $pwd = "*****"; $host = "localhost"; $dbname = "apis";
//echo "commnecing connection to local db<br>";
if (!($conn=mysql_connect($host, $username, $pwd))) {
printf("error connecting to DB by user = $username and pwd=$pwd");
exit;
}
$db3=mysql_select_db($dbname,$conn) or die("Unable to connect to local database");
$result = mysql_query($sql) or die ("Can't connect because ". mysql_error());
return $result;
}//end function ?>
Then I include the function like this
require("conn.php");
$sql = "select * from table";
$result = conn($sql); //runs the query and passes back the result
if ($result){ //do something here } ?>
In your case...i changed your code somewhat....
<?php function connect() { //global $link; $result = ""; if (!$link = mysql_connect("localhost","username","password")){ $link = mysql_error(); } if(!mysql_select_db("database",$link)) { $result = mysql_error(); } if ($result==""){ return $link; //pass the link without globals }else{ die($result); } }
function links() { //global $link; $sql = "select * from jozef_links"; $link = connect(); //call the connect function to create the link $result = mysql_query($sql, $link) or die (mysql_error()); echo "<li>"; while ($array = mysql_fetch_array($result)) { $id = $array['link_name']; $url= $array['link_url']; echo "<ul><a href=\"$url\">$id</a></ul>\n"; } echo "</li>"; } ?>
<? include("functions.php"); links(); //call the links function ?>
bastien
From: "J. Connolly" <web@xxxxxxxxxx> To: PHP list <php-db@xxxxxxxxxxxxx> Subject: referencing external functions Date: Tue, 15 Feb 2005 12:45:29 -0500
Sorry if this is a duplicate. I accidentally sent to the listserve from the wrong address.
I am having trouble calling external functions. I am trying to use this to clean up the code in the display. I have this as the function.php
<?php function connect() { global $link; $link = mysql_connect("localhost","username","password") or die ("Could not Connect to DB"); mysql_select_db("database",$link) or die (mysql_error()); }
function links() { global $link; $sql = "select * from jozef_links"; $result = mysql_query($sql, $link) or die (mysql_error()); echo "<li>"; while ($array = mysql_fetch_array($result)) { $id = $array['link_name']; $url= $array['link_url']; echo "<ul><a href=\"$url\">$id</a></ul>\n"; } echo "</li>"; } ?>
and am trying to use it in this links3.php
<?php include('functions.php') or die (mysql_error()); ?>
but I keep getting the error message
*Warning*: main(1): failed to open stream: No such file or directory in *C:\Accounts\dbadream\wwwRoot\links3.php* on line *4*
*Warning*: main(): Failed opening '1' for inclusion (include_path='.;c:\php4\pear') in *C:\Accounts\dbadream\wwwRoot\links3.php* on line *4*
Can anyone explain what is wrong. Both files are in the same directory and and both have the normal permissions set to access.
Thanks,
jzf
-- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
-- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php