从数据库 PHP MySQL 中的表中选择数据


selecting data from a table in database php mysql

尝试从数据库中的表中获取数据并将其存储到名为"about"的文本字段中

但是我不断收到错误:

警告:mysql_fetch_assoc() 期望参数 1 是资源,对象在

<?php
require("common.php");     
$query = $db->prepare("SELECT * FROM about");     
$result = $query or die(mysql_error()); // run the query    
$row = mysql_fetch_assoc($result); // fetch a result row      
echo $row['about'];    
?>

那是因为你没有执行查询。准备后使用 Execute()。你的代码将看起来像这样

<?php
require("common.php"); 
$query = $db->prepare("SELECT * FROM about");  
$query->execute();
$result = $query->fetch(PDO::FETCH_ASSOC);
print_r($result);//to check the elements of the array
echo $row['content'];    
?>

记得

PDO::prepare() - Prepares a statement for execution and returns a statement object
PDOStatement::execute() - Executes a prepared statement

你缺少mysql_query(); .这是为了执行您的 sql 查询。

$query = "SELECT * FROM about";     
$result = mysql_query($query) or die(mysql_error());