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

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!