简体   繁体   English

无法访问数据库(MAMP)

[英]Can't access the Database (MAMP)

When I press my submit button, I get an error saying : 当我按下提交按钮时,出现错误消息:

ErrorTable 'form2.demo' doesn't exist 错误表'form2.demo'不存在

form2 is my database name which is created in phpmyadmin. form2是我在phpmyadmin中创建的数据库名称。

I am a new MAC user. 我是MAC新用户。

Below is my php code. 下面是我的PHP代码。

define('DB_NAME','form2');
define('DB_USER','root');
define('DB_PASSWORD','root');
define('DB_HOST','localhost:8888');

My ports are : 我的港口是:

Apache  : 8888
Msql : 8889

Full code is as below: 完整代码如下:

<?php
define('DB_NAME','form2');
define('DB_USER','root');
define('DB_PASSWORD','root');
define('DB_HOST','localhost:8889');


$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

if(!$link){
  die('Could not connect :' . mysql_error());
}

$db_selected = mysql_select_db(DB_NAME, $link);

if(!$db_selected){
  die('Can\'t use' . DB_NAME . ':' . mysql_error());
}

$value = $_POST['input1'];

$sql = "INSERT INTO demo (input1) VALUES ('$value')";

if(!mysql_query($sql)){
  die('Error' . mysql_error());
 }
 mysql_close();
 ?>

Your problem have nothing to do with Mac. 您的问题与Mac无关。 You have to show us more code. 您必须向我们展示更多代码。 How the code in your form looks like? 表单中的代码看起来如何? How you are sending stuff to database? 您如何将资料发送到数据库?

Let's assume that everything in code is set up properly. 假设代码中的所有内容均已正确设置。 The main thing you have to check is if you actually have demo table in your database. 您需要检查的主要事情是数据库中是否确实有演示表。 Go to phpmyadmin and check it, if there is no table create it. 转到phpmyadmin并检查它,如果没有表,请创建它。 You can use phpmyadmin to do that or do it via SQL query like that one 您可以使用phpmyadmin来做到这一点,也可以通过类似这样的SQL查询来做到这一点

CREATE TABLE IF NOT EXISTS `form2`.`demo` ( `id` INT NOT NULL AUTO_INCREMENT , PRIMARY KEY (`id`));

Of course code above will create table with only ID column so you have to adjust it to your needs 当然,上面的代码将创建仅具有ID列的表,因此您必须根据需要进行调整

 DEFINE('DB_USERNAME', 'root');
 DEFINE('DB_PASSWORD', '');
 DEFINE('DB_HOST', 'localhost');
 DEFINE('DB_DATABASE', 'customer');

 $mysqli = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE);


 if (mysqli_connect_error()) {
  die('Connect Error ('.mysqli_connect_errno().') '.mysqli_connect_error());
 }

 echo 'Connected successfully.';


 $mysqli->close();

Try the msqli library instead 尝试使用msqli库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM