PHP无法执行查询寄存器。\u new
我的笔记本电脑上运行着PHP无法执行查询寄存器。\u new,php,mysql,Php,Mysql,我的笔记本电脑上运行着wamp服务器,phpmyadmin正在运行,我正在尝试创建一个允许人们注册的网站。但是当我转到register\u new.php文件时,我得到以下错误: Warning: mysqli::mysqli(): (HY000/1045): Access denied for user 'user'@'localhost' (using password: YES) in C:\wamp\www\db_fns.php on line 4 Call Stack # Time
wamp
服务器,phpmyadmin
正在运行,我正在尝试创建一个允许人们注册的网站。但是当我转到register\u new.php
文件时,我得到以下错误:
Warning: mysqli::mysqli(): (HY000/1045): Access denied for user 'user'@'localhost' (using password: YES) in C:\wamp\www\db_fns.php on line 4
Call Stack
# Time Memory Function Location
1 0.0150 138784 {main}( ) ...\register_new.php:0
2 0.0191 172744 register( ) ...\register_new.php:40
3 0.0191 172760 db_connect( ) ...\user_auth_fns.php:10
4 0.0191 173720 mysqli ( ) ...\db_fns.php:4
Warning: mysqli_query(): Couldn't fetch mysqli in C:\wamp\www\user_auth_fns.php on line 13
Call Stack
# Time Memory Function Location
1 0.0150 138784 {main}( ) ...\register_new.php:0
2 0.0191 172744 register( ) ...\register_new.php:40
3 0.0860 173952 mysqli_query ( ) ...\user_auth_fns.php:13
我得到无法执行查询作为警告
在我的register\u new.php
文件中,我有:
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
// include function files for this application
require_once('require_fns.php');
//create short variable names
$email=$_POST['email'];
$username=$_POST['username'];
$passwd=$_POST['passwd'];
$passwd2=$_POST['passwd2'];
// start session which may be needed later
// start it now because it must go before headers
session_start();
try {
// check forms filled in
if (!filled_out($_POST)) {
throw new Exception('You have not filled the form out correctly. Please go back and try again.');
}
// email address not valid
if (!valid_email($email)) {
throw new Exception('That is not a valid email address. Please go back and try again.');
}
// passwords not the same
if ($passwd != $passwd2) {
throw new Exception('The passwords you entered do not match. Please go back and try again.');
}
// check password length is ok
// ok if username truncates, but passwords will get
// munged if they are too long.
if (!preg_match('/^(?=.*\d)(?=.*[A-Za-z])[0-9A-Za-z]{6,12}$/', $passwd)) {
throw new Exception('Your password must be between 6 and 12 characters inclusive. Please go back and try again.');
}
// attempt to register
// this function can also throw an exception
register($username, $email, $passwd);
// register session variable
$_SESSION['valid_user'] = $username;
// provide link to members page
do_html_header('Registration successful');
echo "Welcome " . $_POST["username"];
echo 'Your registration was successful.';
do_html_url('member.php', 'Go to members page');
// end page
do_html_footer();
}
catch (Exception $e) {
do_html_header('Warning:');
echo $e->getMessage();
do_html_footer();
exit;
}
?>
另外,我必须连接到数据库的代码是
<?php
function db_connect() {
$result = new mysqli('localhost','user','password');
if (!$result) {
throw new Exception('Could not connect to database server');
} else {
return $result;
}
}
?>
EDIT2
它仍然不工作问题在于您的服务器登录凭据。您提供了错误的用户名和密码,因此无法连接到数据库
默认用户名和登录名通常为:
Username : 'root'
Password : '' // Empty string.
在db_connect()函数中更改登录凭据
<?php
function db_connect() {
$result = new mysqli('localhost','root','');
if (!$result) {
throw new Exception('Could not connect to database server');
} else {
return $result;
}
}
?>
register()函数的后代码too@Satyregister()
函数在那里,它是寄存器($username,$email,$passwd)代码>line@smithster这不是寄存器函数的代码,该行只是调用函数。我们需要函数register()@AlanTan的实际代码。我会将其编辑到问题中。共享您的连接代码,DB连接不正确。
Username : 'root'
Password : '' // Empty string.
<?php
function db_connect() {
$result = new mysqli('localhost','root','');
if (!$result) {
throw new Exception('Could not connect to database server');
} else {
return $result;
}
}
?>