douqingnao9246 2013-03-31 16:58
浏览 97
已采纳

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

?>

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

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

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:

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

Thoughts anyone? Thanks in advance!

Edit: really appreciate the help everyone, thanks a lot!

  • 写回答

3条回答 默认 最新

  • dongmei9020 2013-03-31 17:21
    关注

    ok. Here is your code.

    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); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?