mysql查询不起作用

时间:2014-02-22 12:28:07

标签: php mysql ajax

单独为ajax提供代码,包括

   $connection = mysqli_connect($db_config['server'],$db_config['db_user'],$db_config['db_pass']);
if (!$connection)
    die('Could not connect to database: ' . mysql_error());
mysqli_select_db($connection, $db_config['db_name']);

$test = mysql_query("SELECT * FROM chat");
echo "test: <br>";
print_r($test);

但如果我打电话,它会返回nothig:

//db connect
$connection = mysqli_connect($db_config['server'],$db_config['db_user'],$db_config['db_pass']);
if (!$connection)
    die('Could not connect to database: ' . mysql_error());
mysqli_select_db($connection, $db_config['db_name']);

$test = mysql_query("SELECT * FROM chat");

print_r($connection);

返回:

mysqli对象([affected_rows] =&gt; -1 [client_info] =&gt; mysqlnd 5.0.11-dev - 20120503 - $ Id:40933630edef551dfaca71298a83fad8d03d62d4 $ [client_version] =&gt; 50011 [connect_errno] =&gt; 0 [connect_error ] =&gt; [errno] =&gt; 0 [错误] =&gt; [error_list] =&gt;数组()[field_count] =&gt; 0 [host_info] =&gt; localhost通过TCP / IP [info] =&gt; [insert_id] =&gt; 0 [server_info] =&gt; 5.6.13-log [server_version] =&gt; 50613 [stat] =&gt;正常运行时间:7999358主题:4个问题:656648624慢查询:9090打开:3348364齐平表: 1打开表:1999每秒查询数:82.087 [sqlstate] =&gt; 00000 [protocol_version] =&gt; 10 [thread_id] =&gt; 5759563 [warning_count] =&gt; 0)

3 个答案:

答案 0 :(得分:3)

你通过mysqli连接并使用了mysql。

改变

  $test = mysql_query("SELECT * FROM chat");

 $test = mysqli_query($connection,"SELECT * FROM chat");

然后你需要像那样提取你的查询

 $row= mysqli_fetch_array($test) ;
 echo $row['some_column'];

这是完整的代码:

 $connection =mysqli_connect($db_config['server'],$db_config['db_user'],$db_config['db_pass']);
 if (!$connection)
  die('Could not connect to database: ' . mysql_error());
    mysqli_select_db($connection, $db_config['db_name']);

  $test = mysqli_query("SELECT * FROM chat");
  $row= mysqli_fetch_array($test) ;
  echo $row['some_column']."<br />"; //you need to put the column you want to output.

答案 1 :(得分:2)

您使用的是mysqli_connect,但mysql_query。您无法混合mysqlimysql - 使用其中一个。大多数人会告诉您使用mysqli。我会说使用最适合你的方式。就我而言,那是mysql,因为我不必将连接引用传递给每一个电话......以及其他原因。

答案 2 :(得分:0)

当mysql被删除时,请尝试使用mysqli,pdo或其他任何内容。 mysqli的代码如下:

<?php
$mysqli = new mysqli("host", "username", "password", "database_name");

$test= $mysqli->query("SELECT * FROM chat") or die($mysqli->error);

if($test->num_rows){
            while($rows = $test->fetch_array(MYSQLI_ASSOC)){
                $data1= $rows['data1'];
                $data2= $rows['data2'];
            }
}
?>

$ test-&gt; num_rows检查结果数量,while循环用于获取返回的所有数据。