0

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>
4
  • 1
    Why are you still using an outdate API and why do you mix database code with HTML code? Commented Sep 30, 2012 at 22:42
  • What would be the most current API? Commented Sep 30, 2012 at 22:44
  • 1
    @Colin747 - PDO or mysqli. See php.net/manual/en/mysqlinfo.api.choosing.php Commented Sep 30, 2012 at 22:45
  • "$var" is a sign of cargo-cult programming. Commented Sep 30, 2012 at 23:01

4 Answers 4

2

$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.

Sign up to request clarification or add additional context in comments.

Comments

2

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>

Comments

0

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.

Comments

0

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 ....");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.