无法从 PHP 访问数据库值
Cannot acces the database value from PHP
我已经从 php 连接到 mySql 数据库。我想从中获取两列的详细信息。当我尝试访问它们时,它输出 null。
当我在 phpmyAdmin 数据库工具中 运行 时,相同的查询字符串工作正常。
下面是我的代码:
$servername = "mysql";
$username = "$$$$$$$$$";
$password = "%%%%%%%%%%";
$databasename = 'blog';
// Create connection
$conn = new mysqli($servername, $username, $password, $databasename);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else {
echo "Connected successfully";
}
$select_sql = "Select site_url from sites where id LIKE '%website1Modal%'";
$result = $conn->query($select_sql);
if ($result->num_rows > 0) {
echo "in if loop";
echo $result[0];
}
以上代码输出:"Connected successfully" 和 "in if loop".
有人能告诉我我犯了什么愚蠢的错误吗?
由于 $result[0]
是一个数组,您不能使用 echo
显示它,而是使用 print_r
:
echo '<pre>';
print_r($result[0]);
echo '</pre>';
如果你想显示 site_url
var 你需要使用 :
echo $result[0]['site_url'];
if
不会让您陷入循环。 if
是一个控制结构。使用 while
进入循环并将 fetch
放入其中,以便可以访问结果。
试一试:
$servername = "mysql";
$username = "$$$$$$$$$";
$password = "%%%%%%%%%%";
$databasename = 'blog';
// Create connection
$conn = new mysqli($servername, $username, $password, $databasename);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else {
echo "Connected successfully";
}
$select_sql = "Select site_url from sites where id LIKE '%website1Modal%'";
$result = $conn->query($select_sql);
if ($result->num_rows > 0) {
echo "in if control";
while($row = $result->fetch_array()) { //this is optional; if only one row the while can be removed. e.g. $row = $result->fetch_array();
echo 'in while loop';
echo $row['site_url'];
} //if removing while remove this as well
}
您可以在此处阅读有关 if
和 while
控件的更多信息。
http://php.net/manual/en/control-structures.if.php
http://php.net/manual/en/control-structures.while.php
这是 query 功能的手动条目。
重要的一点
For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object.
并且因为您有 mysqli_result object
,所以您需要将其发送到 fetch,后者将
Returns an array of strings that corresponds to the fetched row or NULL if there are no more rows in resultset.
您需要使用 Mysqli_Result::fetch_assoc()
获取结果的每一行
while($row = $result->fetch_assoc())
{
print_r($row);
}
将打印结果集的每一行。
您可以访问循环内每个 属性 结果行;
$row['col_name'];
其中 col_name
是列的名称。
// put your db-connection in a different file, preferably placing it outside your webroot
include('../some/folder/dbconn.php');
$select_sql = "Select site_url from sites where id LIKE '%website1Modal%'";
$result = $conn->query($select_sql);
if ($result->num_rows() > 0) {
// fetch the result
$result = $conn->fetch();
// echo the result
echo $result['site_url'];
}
// close db connection
$conn = NULL;
我已经从 php 连接到 mySql 数据库。我想从中获取两列的详细信息。当我尝试访问它们时,它输出 null。 当我在 phpmyAdmin 数据库工具中 运行 时,相同的查询字符串工作正常。
下面是我的代码:
$servername = "mysql";
$username = "$$$$$$$$$";
$password = "%%%%%%%%%%";
$databasename = 'blog';
// Create connection
$conn = new mysqli($servername, $username, $password, $databasename);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else {
echo "Connected successfully";
}
$select_sql = "Select site_url from sites where id LIKE '%website1Modal%'";
$result = $conn->query($select_sql);
if ($result->num_rows > 0) {
echo "in if loop";
echo $result[0];
}
以上代码输出:"Connected successfully" 和 "in if loop".
有人能告诉我我犯了什么愚蠢的错误吗?
由于 $result[0]
是一个数组,您不能使用 echo
显示它,而是使用 print_r
:
echo '<pre>';
print_r($result[0]);
echo '</pre>';
如果你想显示 site_url
var 你需要使用 :
echo $result[0]['site_url'];
if
不会让您陷入循环。 if
是一个控制结构。使用 while
进入循环并将 fetch
放入其中,以便可以访问结果。
试一试:
$servername = "mysql";
$username = "$$$$$$$$$";
$password = "%%%%%%%%%%";
$databasename = 'blog';
// Create connection
$conn = new mysqli($servername, $username, $password, $databasename);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else {
echo "Connected successfully";
}
$select_sql = "Select site_url from sites where id LIKE '%website1Modal%'";
$result = $conn->query($select_sql);
if ($result->num_rows > 0) {
echo "in if control";
while($row = $result->fetch_array()) { //this is optional; if only one row the while can be removed. e.g. $row = $result->fetch_array();
echo 'in while loop';
echo $row['site_url'];
} //if removing while remove this as well
}
您可以在此处阅读有关 if
和 while
控件的更多信息。
http://php.net/manual/en/control-structures.if.php
http://php.net/manual/en/control-structures.while.php
这是 query 功能的手动条目。
重要的一点
For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object.
并且因为您有 mysqli_result object
,所以您需要将其发送到 fetch,后者将
Returns an array of strings that corresponds to the fetched row or NULL if there are no more rows in resultset.
您需要使用 Mysqli_Result::fetch_assoc()
while($row = $result->fetch_assoc())
{
print_r($row);
}
将打印结果集的每一行。
您可以访问循环内每个 属性 结果行;
$row['col_name'];
其中 col_name
是列的名称。
// put your db-connection in a different file, preferably placing it outside your webroot
include('../some/folder/dbconn.php');
$select_sql = "Select site_url from sites where id LIKE '%website1Modal%'";
$result = $conn->query($select_sql);
if ($result->num_rows() > 0) {
// fetch the result
$result = $conn->fetch();
// echo the result
echo $result['site_url'];
}
// close db connection
$conn = NULL;