我的代码似乎有问题,在执行时,我会收到“未定义的变量:第60行的mysqli_connect”和“对第60行中的非对象的成员函数查询()的调用”。
我能在如何将其纳入我的代码的正确方向上迈出一步吗?
<!-- 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')";
?>发布于 2018-04-19 04:44:37
我已经更新了你的代码,请现在试一试。$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')";
?>发布于 2018-04-19 04:25:12
尝试以下代码连接到db
<?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";
?>https://stackoverflow.com/questions/49912645
复制相似问题