PHP可以连接数据库但不能查询任何sql

时间:2015-08-06 02:57:25

标签: php mysql

我在CentOS 7上安装了php(5.4.16),php-mysql,php-fpm,nginx,MariaDB。现在我可以使用以下代码连接数据库。但它无法执行任何sql。我检查了/etc/php.d/pdo.ini,/etc/php.d/mysql.ini和/etc/php.d/pdo_mysql.ini,并且所有扩展都已启用。

$('.some-selector').yourAwesomeFunction();

PHP信息:

<?php
///print_r(phpinfo());exit;
    $dbhost = 'localhost';
    $dbuser = 'root';
    $dbpass = 'XXXX;
    $dbname = 'XXXX';
    $conn = mysql_connect($dbhost,$dbuser,$dbpass,$dbname);
    if ($conn) {
        echo "Connection Successfully\n";
    } else {
        echo "Connection failed\n";
    }

    $sql = 'SELECT * FROM `user` LIMIT 0, 10';

    try{

        if(mysql_query($sql,$conn)){
            echo "execute successfully";
        }else{
            echo "execute failed";
        }
    }catch(Exception $e){
        echo $e;
    }

?>

BTW:我可以用mysql cli

获得结果

1 个答案:

答案 0 :(得分:1)

<?php 
  error_reporting(E_ALL);
  ini_set('display_errors', 1);

  // error reporting initialized, for use in development/staging, not production

  // remember, this is a test. Do not use root otherwise
  $dbhost = "localhost";
  $dbuser = "root";
  $dbpass = "xxxx";
  $dbname = "xxxx";

  // note to self: we will be using mysqli_* functions

  // from OP's phpinfo:
  // MysqlI Support => enabled
  // Client API library version => 5.5.41-MariaDB

  $conn=mysqli_connect($dbhost,$dbuser,$dbpass,$dbname);

  // Check connection
  if (mysqli_connect_errno()) {
    echo "Connect Failed: " . mysqli_connect_error();
    exit();
  }

  // Perform a query, check for error
  $query="SELECT * FROM `user` LIMIT 0, 10";
  if (!mysqli_query($conn,$query)) {
    echo("Error description: " . mysqli_error($conn));
    exit();
  }
  echo "Successfully made it here !";

?>

旁注:只应在暂存时进行显示错误,而不是生产。