我是编程新手,并且不断出现。
这是我的connect.php
<?php
$username = "root";
$password = "";
$db = "app";
// Create connection
$conn = mysqli_connect("localhost", $username, $password, $db);
if (!@$conn) {
die ("Unable to Connect!");
}
else {
echo "Connected!";
}
?>
并且错误一直显示在此代码中(index.php) 错误说
致命错误:未捕获错误:在C:\ xampp \ htdocs \ nath \ index.php中调用字符串上的成员函数query():5堆栈跟踪:#0 {main}在C:\ xampp \ htdocs中抛出第5行的\ nath \ index.php
<?php
error_reporting(E_ALL);
require 'db/connect.php';
if ($update = $db->query("UPDATE people SET created = NOW()")) {
}
?>
答案 0 :(得分:0)
试试这个:
if ($update = $conn->query("UPDATE people SET created = NOW()")) {
echo "Data updated.";
}
$db = 'app';
您可以使用$db->query();
来解决错误原因。
因此,更正为$conn->query('your query');
答案 1 :(得分:-1)
<?php
error_reporting(E_ALL);
require 'db/connect.php';
if ($update = $conn->query("UPDATE people SET created = NOW()")) {
}
?>
$update = $conn->query
因为您的关联是$conn
而不是$db
答案 2 :(得分:-2)
error_reporting(E_ALL);
require 'db/connect.php';
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "UPDATE people SET created = NOW()";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
答案 3 :(得分:-2)
我建议您使用PDO而不是mysqli。所以你可以声明一个这样的类:
class DataBase
{
var $database_name = "your_database_name_here";
var $database = null;
public function __construct()
{
$user = "root";
$password = "";
try {
$this->database = new PDO('mysql:host=localhost;dbname=' . $this->database_name, $user, $password);
} catch (PDOException $e) {
exit("Fatal error: " . $e->getMessage());
}
}
public function get_database()
{
return $this->database;
}
}
然后你可以使用:
$database_object = new DataBase();
$database = $database_object->get_database();
最后,要进行查询,您可以执行以下操作:
$database->query("select * from foo");