-1

我已经就我正在处理的代码提出了一个问题,只是不是同一个问题。不管怎样,很抱歉重新发布!

所以我的代码有问题,如下:

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

?>

这输出(块引用不显示分页符):

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

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

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

有人想吗?提前致谢!

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

4

3 回答 3

1

你没有在你的mysqli_query()on$table之前做一个mysqli_query()on $insert,你也没有做一个mysqli_query()on$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']. " ";

因为您必须使用从查询中获取数组mysqli_fetch_array()

$results = mysqli_fetch_array($result);
echo "result: ".$results['city']. " ";
于 2013-03-31T17:04:48.343 回答
0

表 'db_1.info' 不存在

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

于 2013-03-31T17:03:36.877 回答
-1

行。这是你的代码。

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); 
于 2013-03-31T17:21:56.993 回答