php - "No database selected" error with mysql_query -
this question has answer here:
here php code
<?php error_reporting(e_all); class ll{ function ll(){ include "sql.php"; } function getuser($id) { $sql="select * users 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(); // works ! $services->getuser(1); // 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 correct (no error)
- setofflineall works fine (no error)
- getuser raises "no database selected error !", select 1 mysql_select_db
i use php5.4
any clue ?
i 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 * users id=$id"; $res = mysql_query($sql) or die (mysql_error()); $obj = mysql_fetch_object($res); return $obj; }
and still have error: no database selected
you should try this:
mysql_select_db($database_con1, $con1);
and should give database variable query too:
mysql_query("query", $con1);
Comments
Post a Comment