gpt4 book ai didi

php - mysqli_query 无法识别数据库?

转载 作者:行者123 更新时间:2023-11-29 13:56:48 24 4
gpt4 key购买 nike

我已经就我正在处理的这段代码提出了一个问题,但不是同一个问题。不管怎样,抱歉重发!

所以我在代码方面遇到了问题,如下:

<?php
// Create connection

$host = "localhost";
$username="tudor";
$password="passw0rd";

$con=mysqli_connect($host, $username, $password);
if(! $con )
{
die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully<br />';


$db_1 = mysqli_select_db( $con, 'db_1' );
if (! $db_1) {
die('Could not select database: ' . mysqli_error());
}
else {
echo "Database successfully selected<br />===============================<br />";
}

//===================================



$a = 1;
$b = 2234;

$table = "CREATE TABLE info (id INT NOT NULL AUTO_INCREMENT, city CHAR(40), country CHAR(40))";
if (! $table) {
die('Could not create table ' . mysqli_error($con));
}
else {
echo "Table created<br />";
}

$insert = "INSERT INTO info (city, country) VALUES ($a, $b)";
if (! $insert) {
die('Could not insert ' . mysqli_error($con));
}
else {
echo "Inserted<br />";
}

$select = "SELECT * FROM info";


$result = mysqli_query ($con, $insert);
if (! $result) {
die('Result not working ' . mysqli_error($con));
}
else {
echo "Result working<br />";
}

echo "result: ".$result['city']. " ";


mysqli_close($con);

?>

此输出( block 引用不显示分页符):

Connected successfully Database successfully selected =============================== Table created Inserted Result not working Table 'db_1.info' doesn't exist

“表'db.info'”不存在是什么意思?它清楚地表明我的信息表已创建......我尝试做的是反转 $result 查询中的变量: $result = mysqli_query ($insert, $con);,因为我在一本书中看到过该语法。然而,它给出的只是输出中的以下消息:

Warning: mysqli_query() expects parameter 1 to be mysqli, string given in C:\wamp...

有人有什么想法吗?提前致谢!

编辑:非常感谢大家的帮助,非常感谢!

最佳答案

$insert 上执行 mysqli_query() 之前,您没有对 $table 执行 mysqli_query() ,并且您没有在 $select

上执行 mysqli_query()
$table = "CREATE TABLE info (id INT NOT NULL AUTO_INCREMENT, city CHAR(40), country CHAR(40))";
if (! $table)

$insert = "INSERT INTO info (city, country) VALUES ($a, $b)";
if (! $insert) {

$select = "SELECT * FROM info";

$result = mysqli_query ($con, $insert);
if (! $result)

尝试添加mysqli_query() -

$table_sql = "CREATE TABLE `info` (`id` INT NOT NULL AUTO_INCREMENT, `city` CHAR(40), `country` CHAR(40), PRIMARY KEY (`id`))";
$table = mysqli_query ($con, $table_sql);
if (! $table) {
die('Could not create table ' . mysqli_error($con));
}
else {
echo "Table created<br />";
}

$insert_sql = "INSERT INTO `info` (`city`, `country`) VALUES ('$a', '$b')";
$insert = mysqli_query ($con, $insert_sql);
if (! $insert) {
die('Could not insert ' . mysqli_error($con));
}
else {
echo "Inserted<br />";
}

$select = "SELECT * FROM `info`";

$result = mysqli_query ($con, $select);
if (! $result) {
die('Result not working ' . mysqli_error($con));
}
else {
echo "Result working<br />";
}

编辑另外,这条线将会失败 -

echo "result: ".$result['city']. " ";

因为您必须使用 mysqli_fetch_array() 从查询中获取数组

$results = mysqli_fetch_array($result);
echo "result: ".$results['city']. " ";

关于php - mysqli_query 无法识别数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15731995/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com