我正在尝试了解有关MySQL数据库和编写自定义PHP脚本的更多信息。
我以前查询过WordPress数据库,我很乐意添加自定义表,自定义查询等。但我以前从未创建过自己的数据库。
到目前为止,我有一个名为 simplecms
的数据库,其中有一个名为 core
的表,它有两列,core_name
和 core_value
。到目前为止,它有一行。
我所做的一切都是为了回显这一行的值。
到目前为止,我有以下代码:
[更新的代码]
<?php
// New Connection
$mysqli = new mysqli('localhost','root','root','simplecms');
// Check for errors
if( mysqli_connect_errno() ) {
echo mysqli_connect_error();
} else {
echo('connected to db...<br /><br />');
}
// Create Query
$query = "SELECT core_value FROM core WHERE core_name='url'";
// Execute Query
if( $result = $mysqli->query($query) ) {
// Cycle through results
while($row = $mysqli->fetch_object($result)){
echo $row->column;
}
// Free result set
$result->close();
} else {
printf("Error message: %s'n", $mysqli->error);
}
// Close connection
$mysqli->close();
?>
这将返回:Error message: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '''url''' at line 1
有什么想法我可能出错吗?
[更新]
我已经设法让它使用一些全新的代码!我不是 100% 为什么这有效,但这里只是以防万一:
<?php
// new Connection
$mysqli = new mysqli('localhost','root','root','simplecms');
// check for errors
if( mysqli_connect_errno() ) {
echo mysqli_connect_error();
} else {
echo('connected to db...<br /><br />');
}
// create a prepared statement
if($query = $mysqli->prepare("SELECT core_value FROM core WHERE core_name='url'")) {
// execute
$query -> execute();
// bind results
$query -> bind_result($result);
// fetch value
$query -> fetch();
// echo out results
echo $result;
// close the statement
$query -> close();
}
// close mysqli
$mysqli -> close();
?>
SELECT core_value FROM core WHERE core_name=url
从您所说的内容来看,没有 url 列,因此此查询不起作用。使用'url'
.
将来,您将能够通过检查文档$mysqli->error
示例来捕获此类问题。
我已经设法让它使用一些全新的代码!我不是 100% 为什么这有效,但这里只是以防万一:
<?php
// new Connection
$mysqli = new mysqli('localhost','root','root','simplecms');
// check for errors
if( mysqli_connect_errno() ) {
echo mysqli_connect_error();
} else {
echo('connected to db...<br /><br />');
}
// create a prepared statement
if($query = $mysqli->prepare("SELECT core_value FROM core WHERE core_name='url'")) {
// execute
$query -> execute();
// bind results
$query -> bind_result($result);
// fetch value
$query -> fetch();
// echo out results
echo $result;
// close the statement
$query -> close();
}
// close mysqli
$mysqli -> close();
?>