【问题标题】:php/SQL returns NULLphp/SQL 返回 NULL
【发布时间】:2015-09-04 03:04:00
【问题描述】:

我会第一个承认我是个白痴,但是这个查询为什么返回 NULL 有什么原因吗?与数据库的连接工作得很好,我可以发布到它。

<?php
$servername = "server";
$username = "user";
$password = "pass";
$dbname = "db-name";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT * FROM mytablename";
$results = mysqli_query($sql,$conn);

if ($results !== false) {
    var_dump($results);
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

mysqli_close($conn);
?>

【问题讨论】:

  • tablename 是什么? TABLE?
  • 您没有得到任何结果或无法连接到数据库?什么是回归null
  • 表格是关键字。要转义它,请使用反引号或重命名您的表格。选择 * 从TABLE
  • mixed mysqli_query ( mysqli $link , string $query [, int $resultmode = MYSQLI_STORE_RESULT ] )
  • 那么您需要在这篇文章中添加更多信息。你从哪里得到空值?您真的要查询名为 table 的表吗?

标签: php mysql sql mariadb


【解决方案1】:

mysqli_query 需要第一个参数是链接标识符,第二个参数是查询。试试 -

$results = mysqli_query($conn, $sql);

DOCS

更新

$results 是资源,而不是表数据。如果你需要数据,那么你需要使用

$fetchData = mysqli_fetch_object($result)

【讨论】:

  • 谢谢 - 至少现在发生了一些新的事情:它返回以下内容: object(mysqli_result)#2 (5) { ["current_field"]=> int(0) ["field_count"]= > int(4) ["lengths"]=> NULL ["num_rows"]=> int(4) ["type"]=> int(0) }
  • $sql = "SELECT * FROM SMFB"; $results = mysqli_query($conn, $sql); if ($results !== false) { mysqli_fetch_object($result) 喜欢这个?它现在什么都不返回。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-03
  • 2023-03-26
相关资源
最近更新 更多