简体   繁体   English

与mysql连线时发生错误?

[英]Error with connection to mysql?

I have this in side my two php files. 我这边有两个PHP文件。

<?php

include 'dbh.php';

$first = $_POST['first']
 $last = $_POST['last']
 $uid = $_POST['uid']
 $pwd = $_POST['pwd']

 echo $first;
 echo $last;
 echo $uid;
 echo $pwd;

?>

and

<?php

$conn = mysql_connect("localhost", "root", "", "login");

if (!$conn) {
die("Connection faile:".mysql_connect_error());
}

?>

and my html is down there. 和我的HTML就在那儿。 What could be problem in my connection to mysql? 我与mysql的连接可能有问题吗? Can anyone help me? 谁能帮我? I don't have much experience with mysql but i think it should be ok. 我对mysql没有太多经验,但我认为应该没问题。

 <!DOCTYPE html> <html> <head> <title></title> </head> <body> <form action="http://localhost/signup.php" method="POST"> <input type="text" name="first" placeholder="name"><br> <input type="text" name="last" placeholder="lastname"><br> <input type="text" name="uid" placeholder="username"><br> <input type="pasword" name="pwd" placeholder="pasword"><br> <button type="submit">sign up</button> </form> </body> </html> 

First change this database connection, and don't use mysql beacause is deprecated use mysqli instead. 首先更改此数据库连接,并且不使用mysql,因为不建议使用mysqli。

<?php
// host, user, password, database name
$conn = mysqli_connect("localhost", "root", "", "login");

// if u use mysql then ur connection need to connect first then select database
// $conn = mysql_connect("localhost", "root", "");
// $db = mysql_select_db("database name");

if (!$conn) {
   die("Connection faile: " . mysqli_connect_error());
}

?>

use mysqi because it is more secured and you only need to adjust mostly on parameters but better if you start using it, mysql is deprecated already so try to avoid using mysql functions and start using mysqli.. connect using this 使用mysqi是因为它更加安全,您只需要在参数上进行大部分调整即可,但是如果您开始使用它,则更好,因为mysql已经过时了,因此请避免使用mysql函数并开始使用mysqli。

$db_host = '192.168.1.9'; //sample
$db_user = 'dbuser'; //sample usename
$db_pass = 'dbpass'; //sample password
$db_name = 'databasename';

$db = mysqli_connect($db_host, $db_user, $db_pass, $db_name);

if(mysqli_connect_errno($db))
die('Failed to connect to MySQL: ' . mysqli_connect_error());

hope this helps. 希望这可以帮助。

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

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