>我似乎在代码上遇到了问题,执行时,我收到' 未定义的变量:在第 60 行mysqli_connect' 和'在第 60 行的非对象上调用成员函数 query(( "
我能否朝着正确的方向迈出一步,将其合并到我的代码中?
<!-- Retrieve records from database -->
<?php
$db=mysqli_connect(null,null,null,'connection')
or die("Can't connect to DB:" . mysqli_connect_error());
// Create database
// sql to create table
$sql = "CREATE TABLE state_t (
state_abbr char(2) PRIMARY KEY,
state_name char(20),
state_zone integer)"
if ($mysqli_connect->query($sql) === TRUE) {
echo "TABLE state_t created successfully";
} else {
echo "Error creating TABLE: " . $mysqli_connect>error;
}
$mysqli_connect->close();
// sql to create table
$sql = "CREATE TABLE tool_t (
tool_item_no char(10) PRIMARY KEY,
tool_name char(20),
tool_price numeric(6, 2),
tool_weight numeric(4, 1),
tool_picture char(30),
tool_description varchar);"
if ($mysqli_connect->query($sql) === TRUE) {
echo "TABLE state_t created successfully";
} else {
echo "Error creating TABLE: " . $mysqli_connect>error;
}
$sql = "INSERT INTO tool_t (tool_item_no, tool_name, tool_price, tool_weight, tool_picture, tool_description)
VALUES ('1', 'Chisel', '4.00', '100', '/tools/chisels.jpg', 'Beautiful Chisel')";
$sql = "INSERT INTO tool_t (tool_item_no, tool_name, tool_price, tool_weight, tool_picture, tool_description)
VALUES ('2', 'Tree Trimmers', '2.00', '50', '/tools/tree_trimmer.jpg', 'Cool Tree Trimmer')";
$sql = "INSERT INTO tool_t (tool_item_no, tool_name, tool_price, tool_weight, tool_picture, tool_description)
VALUES ('3', 'Spanners', '2.00', '320', '/tools/spanners.jpg', 'Awesome Spanner')";
?>
我已经更新了您的代码,请立即尝试。 $mysqli_connect
不是数据库连接的引用变量,因此它给出错误Call to a member function query() on a non-object in on line 60
。所以你应该数据库连接对象$conn
。
<!-- Retrieve records from database -->
<?php
// $db =mysqli_connect(null,null,null,'connection');
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// sql to create table
$sql = "CREATE TABLE state_t (
state_abbr char(2) PRIMARY KEY,
state_name char(20),
state_zone integer)"
if ($conn->query($sql) === TRUE) {
echo "TABLE state_t created successfully";
} else {
echo "Error creating TABLE: " . $conn->error;
}
$mysqli_connect->close();
// sql to create table
$sql = "CREATE TABLE tool_t (
tool_item_no char(10) PRIMARY KEY,
tool_name char(20),
tool_price numeric(6, 2),
tool_weight numeric(4, 1),
tool_picture char(30),
tool_description varchar);"
if ($mysqli_connect->query($sql) === TRUE) {
echo "TABLE state_t created successfully";
} else {
echo "Error creating TABLE: " . $mysqli_connect>error;
}
$sql = "INSERT INTO tool_t (tool_item_no, tool_name, tool_price, tool_weight, tool_picture, tool_description)
VALUES ('1', 'Chisel', '4.00', '100', '/tools/chisels.jpg', 'Beautiful Chisel')";
$sql = "INSERT INTO tool_t (tool_item_no, tool_name, tool_price, tool_weight, tool_picture, tool_description)
VALUES ('2', 'Tree Trimmers', '2.00', '50', '/tools/tree_trimmer.jpg', 'Cool Tree Trimmer')";
$sql = "INSERT INTO tool_t (tool_item_no, tool_name, tool_price, tool_weight, tool_picture, tool_description)
VALUES ('3', 'Spanners', '2.00', '320', '/tools/spanners.jpg', 'Awesome Spanner')";
?>
尝试以下代码连接到数据库
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>