0

I've got a file connect.php.

public static function connect() {
    $file= file('data.txt');
        $hostname = rtrim($file[1]);
        $dbname   = rtrim($file[4]);
        $username = rtrim($file[7]);
        $password = rtrim($file[10]);
        mysql_connect($hostname, $username, $password) or DIE('Connection to host isailed, perhaps the service is down!');
        mysql_select_db($dbname) or DIE('Database name is not available!');
};

How can I access this function in other .php files?

connect();

doesn't work.

John_H_Smith
  • 334
  • 2
  • 12
  • 1
    1. Learn about variable scopes (you need to return it from the function). 2. Make sure you actually include the `connect.php` and if that function is part of an object, create it first. 3.. `mysql_*` functions are deprecated since PHP 5.5 (and **removed entirely** in PHP 7) and you should [stop using them](http://stackoverflow.com/q/12859942) if you can. You should choose another API that allows you to use prepared statements (which you *really should*), like `mysqli_*` or PDO - see [choosing an API](http://php.net/manual/en/mysqlinfo.api.choosing.php). – Qirel Jul 20 '16 at 20:33
  • Like others have said, you have to use include(), require() or similar to load code from files other than the one being executed. A common obstacle is to get the file path/include argument right. If you execute _echo ini_get('include_path')_ you will see which folder(s) PHP use as base folders when using relative paths. Absolute paths can also be used, of course. – Torbjörn Stabo Jul 20 '16 at 20:45
  • If that is your entire connect.php, you are missing `` tags around your code. – bytesized Jul 20 '16 at 20:49
  • @bytesized thanks... it works, thanks ;D fail. – John_H_Smith Jul 20 '16 at 20:58

2 Answers2

0

You need to include the file, so your php file has access to the function. You can use require to do this.

require('path/to/connect.php');

Ryan27
  • 453
  • 4
  • 16
0

That's because it's not possible. You have to access the code in order for it to work. If you want THIS function available on another page then:

<?php include 'connect.php' ?>

With that, you can make your function call on that page and it will work.

That's pretty much it.

durbnpoisn
  • 4,666
  • 2
  • 16
  • 30
  • then the code is only printed on the site. And it is not working. Error: Call to undefined function connect() – John_H_Smith Jul 20 '16 at 20:39
  • 1
    @12Hannibal12 considering that this says `public static`, I assume that this is part of a class. You would need to include the file with the class and then access the function like a static method such as `ClassName::connect();`. If the code is only printed on the site though, you likely need to add php tags to the class file as well. – Jonathan Kuhn Jul 20 '16 at 20:41
  • @JonathanKuhn There is no class. the code above is the whole connect.php file. – John_H_Smith Jul 20 '16 at 20:42
  • @12Hannibal12 Then it doesn't need the `public static` modifiers. Those are only used for classes to make a function (or method for a class) able to be run from outside or inside the class. If there is no class, all functions are "public" and there is no such thing as static. – Jonathan Kuhn Jul 20 '16 at 20:43
  • And @durbnpoisn, short tags? really? – Jonathan Kuhn Jul 20 '16 at 20:46
  • Well, alright... It is assumed that the page had been declared and this function was just being included. But @JonathanKuhn is correct. It should have a "php" after the first question mark. (edited) – durbnpoisn Jul 20 '16 at 20:48
  • so the code is function connect() {...} and in the .php just add the include path and it should work with executing connect(); ? It does not work. Again, the code of the connect.php is printed on the site. – John_H_Smith Jul 20 '16 at 20:55
  • Oh - you mean it's just spitting out the plain text? If that's the case, then your server is not executing the PHP. That is a whole different matter. – durbnpoisn Jul 20 '16 at 20:58
  • Okay, it works. I forgot to set this into ;D – John_H_Smith Jul 20 '16 at 20:59