简体   繁体   English

无法获取我的PHP文档以连接到我的xampp服务器

[英]Can't get my PHP document to connect to my xampp server

I am very new to php, and am sorry if this question turns out to be too vague, but if there is any other information that you need from me let me know. 我是php的新手,如果这个问题过于模糊,我们深感抱歉,但是如果您需要我提供其他任何信息,请告诉我。

Anyway, basically what my problem is I have some simple php code that should call die() if it can't connect to the xampp server I have set up, however, even if I put in invalid info for the server or user it prints Successful. 无论如何,基本上我的问题是我有一些简单的php代码,如果它无法连接到我设置的xampp服务器,则应该调用die(),即使我为打印的服务器或用户输入了无效的信息成功了 I really don't know where to go with this, so if anyone has any suggestions that would be great. 我真的不知道该去哪里,所以如果有人有什么建议会很棒。

<?php

$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';



if ( !mysql_connect($dbhost, $dbuser, $dbpass) )
{
    die(mysql_error());
} 
else
    echo 'Succesful';

?>

Ok for some reason it was just not working with the root user, so I created a new user who required a password and apparently that connected like it was supposed to. 好的,由于某种原因,它不能与root用户一起使用,所以我创建了一个新用户,该用户需要密码,并且显然应该按原样进行连接。 Thanks everyone for the answers. 谢谢大家的回答。

Your code is correct pistolpete333. 您的代码是正确的pistolpete333。 For your root user issue, that your not able to connect with root user you can check below file for your phpmyadmin configuration 对于您的root用户问题,您无法与root用户连接,可以在下面的文件中检查phpmyadmin配置

C:\wamp\apps\phpmyadmin3.5.1\config.inc.php.

In above file you can check your host, username and password of phpmyadmin. 在上面的文件中,您可以检查phpmyadmin的主机,用户名和密码。

This is working because you have specified valid username, host and password to mysql. 这是有效的,因为您为mysql指定了有效的用户名,主机和密码。 If you specify wrong password, you get mysql error. 如果指定了错误的密码,则会出现mysql错误。 This code works and connects to the mysql server though you have not selected any database. 尽管您尚未选择任何数据库,但是此代码可以工作并连接到mysql服务器。

Try this code instead 试试这个代码

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$link = mysql_connect($dbhost, $dbuser, $dbpass);
if (!$link) {
    die('Not connected : ' . mysql_error());
}

// make foo the current db
$db_selected = mysql_select_db('foo', $link); //foo is your database name
if (!$db_selected) {
    die ('Can\'t use foo : ' . mysql_error());
}
?> 

I have found that when trying to connect php to an xampp server there are usually only a few things that could cause the issue you are having. 我发现,当尝试将php连接到xampp服务器时,通常只有几件事会导致您遇到问题。

1. mySQL service is not running
2. no specific database is selected to try to access
3. user does not exist in phpmyadmin.

When you pick a user to connect php to mySQL with you should always make sure that a password is set, that is usually the thing people miss. 当您选择一个用户将php连接到mySQL时,应始终确保设置了密码,这通常是人们会错过的事情。 You can use root if you create another instance of it in the user list and require a password, or you can add a password to the root user already in the list, but the best option is to create a new custom user that only has access to the database you want to access with your php and make sure it has a password required. 如果您在用户列表中创建root的另一个实例并需要输入密码,则可以使用root;也可以向列表中已经存在的root用户添加密码,但是最好的选择是创建一个仅具有访问权限的新自定义用户。到您要使用php访问的数据库,并确保它具有所需的密码。 (make sure any user you use can connect with localhost; root can by default and I think most newly created users can as well). (确保您使用的任何用户都可以与localhost连接;默认情况下,root用户可以,并且我认为大多数新创建的用户也可以)。

Below is the php I use for websites I design that need php to access a DB. 以下是我用于设计网站的php,这些网站需要php才能访问数据库。

<?php
// This configures the variables for database access
$dbhost = 'localhost';
$dbuser = '????';
$dbpass = '????';
$dbname = '????';


// This will connect to the server and then the correct database
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
?>

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

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