Colin747
Colin747

Reputation: 5013

PHP database connection - error when selecting database

I'm having problems when trying to connect to a database with PHP. I am getting the following errors

Notice: Undefined variable: dbhandle in /opt/lampp/htdocs/connection/Connection.php on line 17

Warning: mysql_select_db() expects parameter 2 to be resource, null given in /opt/lampp/htdocs/connection/Connection.php on line 17
Could not select test

My connection file:

<?php

  function Connection() {
  $username = "root";
  $password = "";
  $hostname = "localhost"; 

  $dbhandle = mysql_connect($hostname, $username, $password) 
    or die("Unable to connect to MySQL");
  echo "Connected to MySQL<br>";

  mysql_query("SET NAMES utf8");
  }

  function SelectDatabase() {
  $name = "test";
  $selected = mysql_select_db("$name",$dbhandle) 
    or die("Could not select $name");
}
    ?>

Index.php

<html>
<head>
<?php include 'Connection.php'; ?>

</head>
<body>
<?php Connection() ?>
<?php SelectDatabase() ?>
</body>
</html>

Upvotes: 0

Views: 2145

Answers (4)

becknr
becknr

Reputation: 66

you don't make your life easier :p

you just have to do this :

$connexion = new PDO("mysql:host=localhost;dbname=test","root","password");

and if you want to make sql request just :

$connexion->query("SQL REQUEST ....");

Upvotes: 0

Daedalus
Daedalus

Reputation: 7722

The problem here is easy to spot; you never pass the $dbhandle parameter to your function. You need to do this so the function can use the parameter; due to scope issues, not all parameters are immediately available to all functions.

Try the following instead:

function SelectDatabase($dbhandle) {
    $name = "test";
     $selected = mysql_select_db("$name",$dbhandle) 
         or die("Could not select $name");
}

And then, when you call the function:

SelectDatabase($dbhandle);

Make sure that $dbhandle is global.

Either that, or as others have pointed out, have Connection() return the variable name, and use it within the call.

Secondly, mysql_* functions are deprecated. I would suggest you use PDO instead.

Upvotes: 0

Eugen Rieck
Eugen Rieck

Reputation: 65284

It's a scoping problem: $dbhandle is local to Connection()

You can either use a global variable (put global $dbhandle; at the start of both functions), which is not very elegant, or

function Connection() {
  $username = "root";
  $password = "";
  $hostname = "localhost"; 

  $dbhandle = mysql_connect($hostname, $username, $password) 
    or die("Unable to connect to MySQL");
  echo "Connected to MySQL<br>";

  mysql_query("SET NAMES utf8");

  return $dbhandle;
}

function SelectDatabase($dbhandle) {
  $name = "test";
  $selected = mysql_select_db("$name",$dbhandle) 
    or die("Could not select $name");
}

and

</head>
<body>
<?php $db=Connection() ?>
<?php SelectDatabase($db) ?>
</body>
</html>

Upvotes: 2

Chris Laplante
Chris Laplante

Reputation: 29658

$dbhandle is out of scope when used in SelectDatabase. You might want to have Connection() return $dbhandle. That way, you could do:

<?php SelectDatabase(Connection()) ?>

Of course, you would have to modify SelectDatabase to take the connection variable as a parameter.

Or, alternatively, you could make $dbhandle a global variable so you can use it anywhere in your script.

Upvotes: 2

Related Questions