开发者

php my_sql connection problem

I have a problem trying to connect to a my_sql database. I'm very new to PHP, so thi开发者_开发问答s is probably a very simple problem. At the top of my index.php I have the following code:

try
   {
      echo 'here 1';
      $db=mysql_connect('localhost', 'root', 'password') or die(mysql_error());
      echo 'here 2';
      if(!$db)
      {
         echo 'here 3';
      }
         $db_selected=mysql_select_db("alphaes", $db);
      echo 'here 4';

      if (!$db_selected) {
         die ('Can\'t use foo : ' . mysql_error());
      }
      echo 'here 5';
   }
   catch (Exception $e)
   {
      echo 'Caught exception: ',  $e->getMessage(), "\n";
   }

The problem is that the only output from the page is 'here 1'. If I comment out all the database code the page loads ok. There's something wrong with the connect code, however I don't see the mysql_error or exception written to the browser. Do these get logged to a file somewhere? Can anyone see a problem with the code?

The username and password are correct.

Any help is much appreciated,

Mark


here's a very simple example that uses the newer mysqli extension in conjunction with exception handling:

<?php

ob_start(); 

try
{
    $db = new mysqli("localhost", "foo_dbo", "pass", "foo_db", 3306);

    if ($db->connect_errno) 
        throw new exception(sprintf("Could not connect: %s", $db->connect_error));

    $sqlCmd = "select * from users order by username";
    $result = $db->query($sqlCmd);

    if(!$result) throw new exception(sprintf("Invalid query : %s", $sqlCmd));

    if($db->affected_rows <= 0){
        echo "no users found !";
    }
    else{
        $users = $result->fetch_all(MYSQLI_ASSOC);
        foreach($users as $u) echo $u["username"], "<br/>";
    }
    $result->close();
}
catch(exception $ex)
{
    ob_clean(); 
    echo sprintf("zomg borked - %s", $ex->getMessage());
}

if(!$db->connect_errno) $db->close();
ob_end_flush();
?>


Thanks to everyone who replied - turns out that it was very 'beginners' error - mysql was not enabled in the php configuration file.


If it only outputs "here 1" that means the code stops at:

$db=mysql_connect('localhost', 'root', 'password') or die(mysql_error());

I think it is because mysql_error needs to get the last connection from mysql_connect, and the connection fails.

From the docs:

The MySQL connection. If the link identifier is not specified, the last link opened by mysql_connect() is assumed. If no such link is found, it will try to create one as if mysql_connect() was called with no arguments. If no connection is found or established, an E_WARNING level error is generated.

(emphasis mine)

http://php.net/manual/en/function.mysql-error.php

Try changing mysql_error with some string output and see if it works. If it works, then the error is in the database connection.


Try without a password: $db=mysql_connect('localhost', 'root', '') or die(mysql_error());

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜