mysqli_query无法识别数据库? [英] mysqli_query not recognizing database?

查看:68
本文介绍了mysqli_query无法识别数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经对正在处理的这段代码提出了一个问题,只是没有遇到相同的问题.不管哪种方式,对转发都感到抱歉!

I've already asked a question on this code I'm working on, just not about the same problem. Either way sorry for the repost!

所以我在使用代码时遇到了麻烦,如下所示:

So I'm having trouble with the code, as follows:

<?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);

?>

此输出(blockquote不显示分页符):

This outputs (blockquote doesn't display page breaks):

连接成功,数据库选择成功 ==============================创建的表插入结果不起作用表'db_1.info'不存在

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);,因为我在一本书中看到了这种语法.但是,它给出的只是输出中的以下消息:

What does it mean by "Table 'db.info'" not existing? It clearly says that my info table was created... What I tried doing is inverting the variables in the $result query: $result = mysqli_query ($insert, $con);, because I had seen that syntax in a book. However all it gave was the following message in the output:

警告:mysqli_query()期望参数1为mysqli,给定字符串 在C:\ wamp ...

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

有没有人?预先感谢!

非常感谢大家的帮助,非常感谢!

really appreciate the help everyone, thanks a lot!

推荐答案

好.这是您的代码.

if(! $con )
{
  die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully<br />';
if (!$con) {trigger_error("Could not connect to MySQL: " . mysqli_connect_error()); }   
else { echo "Database successfully connected<br />===============================<br />"; }
$a = 1;
$b = 2234;
$table = mysqli_query($con,"CREATE TABLE IF NOT EXISTS info (`id` int(11) unsigned NOT NULL auto_increment,
`city` CHAR(40), 
`country` CHAR(40), PRIMARY KEY  (`id`) )ENGINE=MyISAM  DEFAULT CHARSET=utf8");
  if (!$table) {
die('Could not create table ' . mysqli_error($con));
}
else {
echo "Table created<br />";
}
$insert = mysqli_query ($con,"INSERT INTO info (city, country) VALUES ('$a', '$b')");
 if (!$insert) {
die('Could not insert ' . mysqli_error($con));
}
else {
echo "Inserted<br />";
}
$select =  mysqli_query ($con,"SELECT * FROM info");  
$res=mysqli_fetch_array($select);
 if (! $res) {
die('Result not working ' . mysqli_error($con));
}
else {
echo "Result working<br />";
}

echo "result: ".$res['city']. " ";
echo "result: ".$res['country']. " ";
mysqli_close($con); 

这篇关于mysqli_query无法识别数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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