0

look, i have a class that connects to a mysql database, but i have another file that contents the username, host, password and table names, If i don't include it and write them, it works fine, but the problem begins when I include it, it returns me "Undefined variable". Thank you, here's my class:

<?php
include '../config/conexiongeneral.php';
class DbConnector {
var $theQuery;
var $link;
public function DbConnector(){
        $host = $elnombredelhost;
        $db = $labasededatos;
        $user = $elnombredelusuario;
        $pass = $lacontasena;

        $this->link = mysql_connect($host, $user, $pass);
        mysql_select_db($db);
        register_shutdown_function(array(&$this, 'close'));
}
    function query($query) {

        $this->theQuery = $query;
        return mysql_query($query, $this->link);

    }
    function fetchArray($result) {

        return mysql_fetch_array($result);

    }
    function close() {

        mysql_close($this->link);

    }
}
?>

Well, i forget that i call this function with:

<?php
include 'dbConnector.php';
$connector = new DbConnector();

$username = trim(strtolower($_POST['username']));
$username = mysql_real_escape_string($username);
$query = "SELECT usuario FROM $latablatres WHERE usuario = '$username' LIMIT 1";
$result = $connector->query($query);
$num = mysql_num_rows($result);

echo $num;
mysql_close();
?>
4
  • 1
    "but the problem begins when I include it" But, what IS the Problem? Commented Jul 15, 2013 at 0:17
  • This: "Undefined variable" Commented Jul 15, 2013 at 0:17
  • @Darkness Please try to include as much useful information as you can: the name of your "undefined variable", the number of the line with the error, etc.). Commented Jul 15, 2013 at 0:19
  • Yes, $elnombredelhost, $labasededatos, $elnombredelusuario, $lacontasena. Commented Jul 15, 2013 at 0:20

2 Answers 2

4

The problem is that you're including the file outside of the class so it's in the wrong scope (the class doesn't know about those variables).

Try this:

public function DbConnector(){
    include '../config/conexiongeneral.php';
    $host = $elnombredelhost;
    $db = $labasededatos;
    $user = $elnombredelusuario;
    $pass = $lacontasena;

    $this->link = mysql_connect($host, $user, $pass);
    mysql_select_db($db);
    register_shutdown_function(array(&$this, 'close'));
}
Sign up to request clarification or add additional context in comments.

4 Comments

What's inside your conesiongeneral.php file?
<?php $elnombredelhost = "host"; $elnombredelusuario = "user"; $labasededatos = "database"; $latablauno = "table"; $latablados = "table"; $latablatres = "teble"; $lacontasena = "pass"; ?>
Oh... forget it, You had all the reason, you was right, the problem was <? ?> originally In conexiongeneral.php i wrote <? ?> and correctly is <?php ?> I don't why... but anyway...
Ahahaha Vale, muchas gracias! :D
1

Use the global keyword to make the variables available inside the function. E.g.:

public function DbConnector(){
    global $elnombredelhost;
    $host = $elnombredelhost;
    // ... rest of your code
}

Alternatively, you could declare the connection parameters a constants in the included file and use the constants in the connection script:

In conexiongeneral.php

define('DB_HOST', 'your_db_host');

In your connection class

public function DbConnector(){
    $host = DB_HOST;
    // ... rest of your code
}

1 Comment

+1 because global solves current problem. We aren't talking about how bad Global state is

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.