当我安装xampp并运行我的php文件时,我发现了这个错误:
Connection Error: SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it.
这是我的联系:
<?php
$server="localhost";
$host="127.0.0.1";
$uname="root";
$prefix="";
$pass="admin";
$dbname="sample";
try{
$conn=new PDO("mysql:host=$server;dbname=$dbname","{$prefix}{$uname}",$pass);
// echo $conn;
}catch(PDOException $e){
echo "Connection Error: ".$e->getMessage();
die();
}
?>
然后我想我忘了在xampp控制器中启动mysql。然后我打开xampp控制器然后我在xampp控制台中找到了这个红色:
11:40:29 AM [mysql] MySQL Service detected with wrong path
11:40:29 AM [mysql] Change XAMPP MySQL and Control Panel settings or
11:40:29 AM [mysql] Uninstall/disable the other service manually first
11:40:29 AM [mysql] Found Path: "C:\Program Files\MySQL\MySQL Server 5.1\bin\mysqld" --defaults-file="C:\Program Files\MySQL\MySQL Server 5.1\my.ini" MySQL
11:40:29 AM [mysql] Expected Path: c:\xampp\mysql\bin\mysqld.exe --defaults-file=c:\xampp\mysql\bin\my.ini mysql
然后我搜索解决这个错误。几乎说我卸载了Mysql然后我尝试卸载它但错误来了:
11:40:45 AM [mysql] Uninstalling service...
11:40:45 AM [mysql] MySQL Service detected with wrong path
11:40:45 AM [mysql] Change XAMPP MySQL and Control Panel settings or
11:40:45 AM [mysql] Uninstall/disable the other service manually first
11:40:45 AM [mysql] Found Path: "C:\Program Files\MySQL\MySQL Server 5.1\bin\mysqld" --defaults-file="C:\Program Files\MySQL\MySQL Server 5.1\my.ini" MySQL
11:40:45 AM [mysql] Expected Path: c:\xampp\mysql\bin\mysqld.exe --defaults-file=c:\xampp\mysql\bin\my.ini mysql
11:40:45 AM [mysql] Service was NOT (un)installed!
实际上我在安装xampp之前安装了mysql5.1,因为我在java中做项目。我是php新手,我不知道该怎么做。请帮帮我们。我真的需要帮助。提前谢谢:&#39;)
答案 0 :(得分:0)
根据您的上一个声明,您分别安装了mysql而不是xampp,因此mysql路径不同。你得到了同样的信息。
从cmd提示符执行以下命令并检查您是否能够连接mysql -
"C:\Program Files\MySQL\MySQL Server 5.1\bin\mysqladmin.exe" -uroot -p status
如果mysql服务没有运行,那么你需要按照以下方式启动它 -
步骤1:以管理员身份连接cmd提示
Step2:执行以下命令 -
net start mysql
<强>更新强>
只是检查你的连接,保存在C:\ xampp \ htdocs下面的test.php文件下面,或者根据你在htdocs文件夹中的位置。
如果您的情况发生了变化,只需将your_db更改为实际的db和user / pass。
并从您的浏览器执行以下网址:http://localhost/test.php
注意:确保apache和mysql应该启动。我们已经检查过Mysql已经运行了。
<?php
$server = "localhost";
$user = "root";
$pass = "admin";
// Create connection
$conn = new mysqli($server, $user, $pass);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT first_name,last_name FROM your_db.user_t limit 1;";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "fname: " . $row["first_name"]. " - LName: " . $row["last_name"]. "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
答案 1 :(得分:0)
connect.php已解决。但这是我的问题。我将connect.php包含在login.php
中<?php session_start();
include "./support/connect.php";
$idNumber = mysql_real_escape_string(htmlspecialchars($_REQUEST['idnumber']));
//$pass = md5($_REQUEST['password']);
$pass = $_REQUEST['password'];
$sqlLogin = "select * from user_t where id_number='" . $idNumber . "' and password='" . $pass . "'";
$resultLogin = $conn->prepare($sqlLogin);
$resultLogin->execute();
$count = $resultLogin->rowCount();
if($count != 0){
$rowLogIn=$resultLogin->fetch(PDO::FETCH_ASSOC);
$_SESSION['firstname'] = $rowLogIn['first_name'];
$_SESSION['middle'] = $rowLogIn['middle_name'];
$_SESSION['lastname'] = $rowLogIn['last_name'];
$_SESSION['userlevel'] = $rowLogIn['account_type'];
$_SESSION['contact'] = $rowLogIn['contact_number'];
$_SESSION['idnumber'] = $idNumber;
$status = $rowLogIn['status'];
$userLevel = $rowLogIn['account_type'];
if(strtolower($status) == "deactivated")
header("location:index2.php?invalid=You are Deactivated! Please ask your admin to activate your account.");
switch (strtolower($userLevel)) {
case 'admin':
# code...
break;
case 'teacher':
# code...
break;
case 'student':
header("location:home.php");
break;
default:
# code...
break;
}
}else{
header("location:index2.php?invalid=Invalid Username or Password.");
}
?>
这是错误: 警告:include(./ support / connect.php):无法打开流:第3行的C:\ xampp \ htdocs \ proj1 \ pages \ loginqueue.php中没有此类文件或目录
Warning: include(): Failed opening './support/connect.php' for inclusion (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\proj1\pages\loginqueue.php on line 3
Warning: mysql_real_escape_string(): No connection could be made because the target machine actively refused it. in C:\xampp\htdocs\proj1\pages\loginqueue.php on line 5
Warning: mysql_real_escape_string(): A link to the server could not be established in C:\xampp\htdocs\proj1\pages\loginqueue.php on line 5
Notice: Undefined variable: conn in C:\xampp\htdocs\proj1\pages\loginqueue.php on line 10
Fatal error: Call to a member function prepare() on a non-object in C:\xampp\htdocs\proj1\pages\loginqueue.php on line 10