0

我正在将此代码用于mysql连接

    $con = mysql_connect("localhost", "username" , "password");
    if (!$con)
    {
       die('Could not connect: ');
    }
    else 
     { echo "connection failed....";}
   mysql_select_db("ManagersDatabase", $con);

mysql 数据库位于/var/lib/mysql/ManagersDatabase. 我的 php 页面位于/var/www/html/. 它不打印任何东西。我的代码有什么问题?

4

4 回答 4

3

这就是你应该如何连接,通过使用 PDO:并在查询时使用准备好的查询。

<?php 
try{
    $con = new PDO('mysql:host=127.0.0.1;dbname=your_database','root','password');
    $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $con->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    $con->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC);
}catch (Exception $e){
    die('Cannot connect to database. Details:'.$e->getMessage());
}
?>

或 mysqli并在查询时使用准备好的查询

<?php
$con = new mysqli("127.0.0.1", "user", "password", "your_database");
if ($con->connect_errno) {
    die("Failed to connect to MySQL: (".$con->connect_errno.") ".$con->connect_error);
}
print_r($con);
?>

编辑(回复评论): 如果添加print_r($con);,您应该看到 mysqli 连接对象,如:

/*
mysqli Object
(
    [affected_rows] => 0
    [client_info] => mysqlnd 5.0.10 - 20111026 - $Id: b0b3b15c693b7f6aeb3aa66b646fee339f175e39 $
    [client_version] => 50010
    [connect_errno] => 0
    [connect_error] => 
    [errno] => 0
    [error] => 
    [error_list] => Array
        (
        )

    [field_count] => 0
    [host_info] => 127.0.0.1 via TCP/IP
    [info] => 
    [insert_id] => 0
    [server_info] => 5.5.25a
    [server_version] => 50525
    [stat] => Uptime: 10  Threads: 1  Questions: 1  Slow queries: 0  Opens: 33  Flush tables: 1  Open tables: 26  Queries per second avg: 0.100
    [sqlstate] => 00000
    [protocol_version] => 10
    [thread_id] => 1
    [warning_count] => 0
)
*/

其他方法已经过时并且很快(谢天谢地)被弃用。

于 2012-08-21T09:50:38.873 回答
0

您应该在 else 块中连接到您的数据库;

   $con = mysql_connect("localhost", "username" , "password");
    if (!$con)
    {
       die('Could not connect: ');
    }
    else    // if connection is made, then select the DB
     { 
       mysql_select_db("ManagersDatabase", $con);
     }
于 2012-08-21T09:38:06.307 回答
0

Quote from your question:

if (!$con)
{
   die('Could not connect: ');
}
else 
 { echo "connection failed....";}

... you do notice it doesn't make much sense? it is saying if (something) { //could not connect } else { //connection failed } so your code thinks it is always failing.

What you should have is something like

$con = mysql_connect("localhost", "username" , "password");
if ($con === false)
{
   die('Could not connect: ' . mysql_error());
}
mysql_select_db("ManagersDatabase", $con);

So it would actually print out anything.

Also, you should have either display_errors enabled to get the errors on screen or error_log in use so you would get the errors logged to somewhere.

In addition, please check you actually have the mysql extension enabled and in use, and mysql_connect() function available. phpinfo(); will tell what extensions you have, and function_exists() can tell you have the function available.

So, things to do:

  1. Fix your code logic like listed above
  2. Enable error printing or logging
  3. Check that you have the mysql extension enabled
  4. Check that mysql_connect() function actually is callable

Edit. as the missing mysql extension seems to be the problem ('call to undefined function mysql_connect'):

First of all, you need to have access to your PHP configuration. If this is your local installation or server, you should have that - if you are using someone elses hosting, they are the people who should change these things.

Given some assumptions, such as

  • you are using your local apache server
  • you are not using XAMPP or any other *AMP products, but vanilla installations

this is what you should do:

  • Find your php.ini configuration file or create one based on example files in the PHP folder if you don't have one
  • Find the lines below (.dll for windows, .so for linux, don't know about others)

;extension=php_mysql.dll
;extension=php_mysqli.dll
;extension=php_pdo_mysql.dll

  • Strip out the comment (the first ';') from the one you want to use. You are using the first one in your example, the others are for using other abstractions described in some of the other postings here, so if you want to go with the code you have, you'd uncomment the first one.
  • Enable "display_startup_errors" also if you want to see any configuration problems immediately
  • Check that "extension_dir" setting is correct, and is pointing to the folder where you have the above .dll extensions
  • Restart your apache server
于 2012-08-21T09:45:08.310 回答
-1

这是您的项目connect.php的模板文件。你可以使用它。

<?php
// Connecting, selecting database
  $link = mysql_connect('localhost', 'root', '') or die('Could not connect: ' . mysql_error());
//echo 'Connected successfully';
  mysql_select_db('procost') or die('Could not select database');
?>
于 2016-10-23T13:52:09.647 回答