-1

Here is my php code

<?php
error_reporting(E_ALL);

class ll{
          function ll(){
            include "sql.php";
          }

         function getUser($ID) {
            $sql="select * FROM users where ID=$ID";
            $res = mysql_query($sql) or die (mysql_error());
            $obj = mysql_fetch_object($res);
            return $obj;
    }

        function setOfflineALL() {
            $sql="update users set online=0";
            mysql_query($sql);
        }

 }
        $services = new ll();
        $services->setOfflineALL(); // THIS WORKS !
        $services->getUser(1); // THIS GIVES error: No database selected 


        ?>

and sql.php is:

<?php   $hostname_con1 = "localhost";
        $database_con1 = "db1";
        $username_con1 = "root";
        $password_con1 = "mypass";
        $con1 = mysql_connect($hostname_con1, $username_con1, $password_con1) or trigger_error(mysql_error(),E_USER_ERROR);
        mysql_select_db($database_con1);
        mysql_set_charset('utf8',$con1);

?>
  • sql.php is correct (no error)
  • setOfflineALL works fine (no error)
  • getUser raises "no database selected error !", however I do select one with mysql_select_db

I do use php5.4

Any clue ?

I also tried:

function getUser($ID) {
            $hostname_con1 = "localhost";
            $database_con1 = "db1";
            $username_con1 = "root";
            $password_con1 = "mypass";
            $con1 = mysql_connect($hostname_con1, $username_con1, $password_con1) or trigger_error(mysql_error(),E_USER_ERROR);
            mysql_select_db($database_con1);
            mysql_set_charset('utf8',$con1);

                $sql="select * FROM users where ID=$ID";
                $res = mysql_query($sql) or die (mysql_error());
                $obj = mysql_fetch_object($res);
                return $obj;
        }

And still have error: No database selected

2
  • 2
    Note: The mysql_* functions are deprecated, they will be removed from PHP in future versions and your code will stop working then. You should not write new code using them, use mysqli_* or PDO instead. Commented Dec 16, 2014 at 12:35
  • ok.. I know that . Howver error_reporting(E_ALL); does not show me any warning or errors ! Commented Dec 16, 2014 at 12:38

1 Answer 1

1

You should try this:

mysql_select_db($database_con1, $con1);

And you should give your database variable to your query too:

mysql_query("query", $con1);

And I strongly advise you not to use mysql but use PDO or mysqli PDO Mysqli

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

6 Comments

I tried this: STILL SAME problem !!!
providing the $con1 like this will not work due to the variable scopes. You'd have to store the variable in your class to be able to use it in all functions.
@GeraldSchneider Please don't state the obvious, I just wrote it out as example and not how the OP should use it in his code.
The way the class in the OP is written makes it clear that it is not obvious.
@GeraldSchneider Please, you see that OP is using multiple functions in a class so YES it is obvious that he should set a private variable inside his class.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.