开发者

PHP - database not selected. What is wrong with my code?

开发者 https://www.devze.com 2022-12-24 13:31 出处:网络
I am learning PHP and tried to connect to MySQL. Altough I am using select DB, is still reports \"No database selected\". What is wrong, please? Thanks.

I am learning PHP and tried to connect to MySQL. Altough I am using select DB, is still reports "No database selected". What is wrong, please? Thanks.

<开发者_StackOverflow中文版?php

$user="test";
$pass="aaa";
ConnectToDb();

function ConnectToDb()
{
$pripojeni=mysql_connect('localhost',$user,$pass);
$selectedDB=mysql_select_db('1a');
    if($query=mysql_query('select * from project')) 
  {
  while($d=mysql_fetch_array($query)) 
  {
        echo "TEST";
        }
  }
  else echo mysql_error($pripojeni);
  }
?>


$user and $pass are in the wrong variable scope.

Pass the values as parameters:

ConnectToDb('test', 'aaa');

function ConnectToDb($user, $pass)
{
    $pripojeni = mysql_connect('localhost', $user, $pass);
    ...
}


You should give the function at least some parameters... try this:

[...]

ConnectToDb($user,$pass);

function ConnectToDb($MyUser,$MyPass) {
  $pripojeni=mysql_connect('localhost',$MyUser,$MyPass);

[...]

...in order to tell your function which user and which password to use. Otherwise the function doesn't know that $user and $pass are related to it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号