未选择数据库错误消息 [英] No database selected error message

查看:65
本文介绍了未选择数据库错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将所有使用PHP MySQL的查询更改为MySQLi.

I am changing all my queries that are using PHP MySQL to MySQLi.

我用连接设置制作了一个名为db.php的文件.

I have made a file called db.php with the connection settings.

文件包括

<?php
$db = new mysqli('localhost','mysqlusername','mysqlpassword');
echo "<h1>Success database connection</h1>";
if($db->connect_errno > 0)
{
die('No connection [' . $db->connect_error . ']');
}
?>

我在文件中包含以下内容:

I include the file with:

require_once "/location/db.php";    

在那之后我使用:

 if($db->connect_error)
 {
   echo "Not connected, error: ".$db->connect_error;
 }  
 else
 {
   echo "Connected.";
 }

它回显为Connected,所以我认为我的连接很好.

It echo's Connected so I assume my connection is good.

我有3个PHP变量,我想在数据库表Code中插入

I have 3 PHP variables which I want to insert in my database table Code

我首先回显变量,以确保它们具有内容.

I first echo the variables so I am sure they have content.

在我确认连接正常(返回已连接")并回显要执行查询的变量的内容之后:

After I validated my connection is alright (returned Connected) and echoing the content of the variables I want to do the query with:

$sql = "INSERT INTO 'Code' (`Name`, `Code`, `Admin`)
VALUES ('$name', '$code', '$admin')"; 
echo $sql;//show query
// Performs the $sql query on the server to insert the values
if ($db->query($sql) === TRUE)
{
    echo 'User Created.';
}
else 
{
    echo 'Errorcreating : '. $db->error;
}

我收到消息错误创建:未选择数据库

I get the message Errorcreating : No database selected

我有echo $ sql向我显示查询.

I have the echo $sql to show me the query.

如果我直接在SQL中复制查询,它的工作就应该如此.

If I copy the query directly in SQL it works like it should.

这是我第一次使用MySQLi,所以我可能犯了一个非常愚蠢的错误,但我找不到它.

This is my first time on MySQLi so it's possible I made a very dumb mistake but I can't find it.

推荐答案

打开连接时,您可以将数据库名称作为第四个参数传递:

When opening the connection you can pass the database name as a 4th parameter:

$db = new mysqli('localhost','mysqlusername','mysqlpassword','database');

此外,您的转义字符是错误的.不要在表名周围使用单引号,而应使用反引号运算符

Also, your escape character is wrong. Don't use single quotes around table names, use backtick operator instead

$sql = "INSERT INTO `Code` (`Name`, `Code`, `Admin`)VALUES ('$name', '$code', '$admin')"; 

这篇关于未选择数据库错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆