0

I'm trying to create a database connection, with the connection details on another page.

Before, all these details where on the same page as all the other code, like this:

 $host="localhost"; // Host name
 $username="username"; // Mysql username
 $password="password"; // Mysql password
 $db_name="database"; // Database name
 $tbl_name="table"; // Table name

 // Connect to server and select database.
 mysql_connect("$host", "$username", "$password")or die("cannot connect");
 mysql_select_db("$db_name")or die("cannot select DB");

 $sql="SELECT * FROM $tbl_name";
 $result=mysql_query($sql);

and so on. This is the way I establish my db connection right now. Every page has this code, only the sql is different for each page.

Now, what I'm trying to do:

every page should have:

 include_once '../includes/db_connect.php';

the page db_connect.php contains this line:

 $mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE);

Every page should connect automatically to the database using this code. I don't want to have all the details on every single page. So on the page itself, the $sql should be connected already so I can start my code from the $sql part.

How can this be done? Because now I'm getting an error that the $sql cannot be executed because there's no database connection.

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
Jeffrey van Zwam
  • 105
  • 1
  • 15
  • 1
    Please, [don't use `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). [This article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide. – Jay Blanchard Oct 20 '14 at 21:48
  • 1
    It looks like you're mixing functions between `mysql_` and `mysqli_` – Jay Blanchard Oct 20 '14 at 21:49
  • And using a relative path for your include will get you into trouble unless all files that include it, are in the same directory. – jeroen Oct 20 '14 at 21:57
  • Maybe use a [singleton](http://www.phptherightway.com/pages/Design-Patterns.html) class to keep one copy of the db connection and share it around? – ethrbunny Oct 20 '14 at 22:10
  • I've created a while loop `while($row = $result->fetch_array())` using `$result = $mysqli->query("SELECT * FROM messages");` which results in `Fatal error: Call to a member function fetch_array() on a non-object` I changed everything to `mysqli` but this returns in the fatal error – Jeffrey van Zwam Oct 20 '14 at 22:16

1 Answers1

0

Take this as a connection file:

<?php
            $sql = new mysqli ("HOST","USER","PASS" ,"DB");
            $sql -> set_charset ( 'utf8' );
            if ($sql->connect_errno) {
            printf("Connect failed: %s\n", $sql->connect_error);
            exit();}

     ?>

You can simply include this file by

include('filename.php');

or, if it's in another dir

include('../path/to/file/filename.php');

You were mixing mysql_ and mysqli_, in hope you are using mysqli_ I used this, too.

baao
  • 71,625
  • 17
  • 143
  • 203