使用PHP和MySQL创建数据库

时间:2013-08-27 17:25:21

标签: php mysql database

好的,我有这段代码:

    <?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpwd = 'ALLO';
$dbname = 'test';
$conn = mysqli_connect($dbhost, $dbuser, $dbpwd, $dbname);


if(! $conn )
{
  die("\nconn: $conn \nCould not connect: " . mysqli_error($conn));
}
echo "\n";







$createtab="CREATE TABLE People(FirstName CHAR(30),LastName CHAR(30),Age INT)";

// Execute query
if (mysqli_query($conn,$createtab))
  {
  echo "Table created successfully";
  }
else
  {
  echo "Error creating table: " . mysqli_error($conn);
  }

echo "\n";




mysqli_query($conn,"INSERT INTO People (FirstName, LastName, Age)
VALUES ('Peter', 'Griffin',35)");

mysqli_query($conn,"INSERT INTO People (FirstName, LastName, Age)
VALUES ('Glenn', 'Quagmire',33)");






mysqli_close($conn)
?>

当我运行php脚本时,运行正常。

但是,我在mysql workbench中找不到该表。

我多次刷新。

http://i.stack.imgur.com/uX1sQ.png http://i.stack.imgur.com/iEXqT.png

1 个答案:

答案 0 :(得分:0)

看看这是否会给你带来任何结果:

SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='People'

如果没有,请在任何表之前创建一个数据库,或者从截图中判断只需在代码中添加一个USE测试。

$createdb="USE test";

// Execute query
if (mysqli_query($conn,$createdb))
 {
  echo "Db selected";
 }
else
 {
  echo "Error selecting db: " . mysqli_error($conn);
 }