mysqli_query无法识别数据库?

时间:2013-03-31 16:58:29

标签: php mysql mysqli

我已经问过我正在研究的这个代码的问题,而不是同样的问题。无论哪种方式抱歉转发!

所以我遇到了代码问题,如下所示:

<?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不显示分页符):

  

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

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

  

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

有人想到吗?提前谢谢!

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

3 个答案:

答案 0 :(得分:1)

mysqli_query() $tablemysqli_query()$insert,而mysqli_query() $select$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']. " "; -

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

修改 此外,这一行将失败 -

{{1}}

因为您必须使用mysqli_fetch_array()

从查询中获取数组
{{1}}

答案 1 :(得分:0)

  

表'db_1.info'不存在

意味着,db info中不存在表db_1,因此请确保是这种情况。

答案 2 :(得分:0)

确定。这是你的代码。

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