-1

When I install xampp and run my php file, i found this error :

Connection Error: SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it. 

this is my connection :

<?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();
}
?>

Then i think i forgot to start the mysql in xampp controller. Then I open xampp controller then I found this red in xampp console:

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

Then I search to solve this error. almost said that I uninstall the Mysql then I try to uninstall it but error comes :

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!

Actually I had installed mysql5.1 before I install xampp, because I doing projects in java. I am new from php, and I don't know what to do. Please help me guys. I really need help. Thank you in advance :')

mrAsA
  • 17
  • 1
  • 7
  • may be the issue is with port see this:http://stackoverflow.com/questions/25609491/php-sqlstatehy000-2002-no-connection-could-be-made-because-the-target-machi – Suchit kumar Nov 05 '15 at 05:39
  • the port was resolved now. thank you. but still the xampp error are still there. I can't run MySQL. please help. – mrAsA Nov 05 '15 at 07:32
  • see this https://www.drupal.org/node/1249662 . i don't know the proper solution so i can really help because i'm a linux user. please do some search. – Suchit kumar Nov 05 '15 at 07:38

2 Answers2

0

As per your last statement, you installed mysql separately than xampp so mysql path is different. Same message you are getting.

Execute below command from cmd prompt and check if you are able to connect mysql-

"C:\Program Files\MySQL\MySQL Server 5.1\bin\mysqladmin.exe" -uroot -p status

if mysql service is not running, so you need to start it as per below-

Step1 : connect cmd prompt as administrator

Step2 : execute below command-

net start mysql

Update:

Just to check your connection save below test.php file in C:\xampp\htdocs or as per your location in htdocs folder.

Just change your_db as your actual db and user/pass if change in your case.

and execute following url from your browser : http://localhost/test.php

Note: Make sure apache and mysql should be start. Mysql already running as we have checked.

<?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);


?>
Zafar Malik
  • 6,734
  • 2
  • 19
  • 30
  • status : Uptime: 65491 Threads: 13 Questions: 18538 Slow queries: 0 Opens: 70 Flush tables: 1 Open tables: 0 Queries per second avg: 0.283 – mrAsA Nov 05 '15 at 08:08
  • step 2 resuslt : The requested service has already been started. – mrAsA Nov 05 '15 at 08:08
  • but when I look in xampp controller still : 4:07:15 PM [mysql] Attempting to start MySQL service... – mrAsA Nov 05 '15 at 08:09
  • actually mysql server is running outside than xampp, so in xampp it will show as not running. In your case mysql is already running so you can use mysql.. – Zafar Malik Nov 05 '15 at 09:15
  • and when I run, still : No connection could be made because the target machine actively refused it – mrAsA Nov 05 '15 at 09:19
  • execute following command from cmd prompt and let me know if you are able to connect mysql ""C:\Program Files\MySQL\MySQL Server 5.1\bin\mysql.exe" -h localhost -uroot -p" – Zafar Malik Nov 05 '15 at 09:29
  • I am able to connect to mysql> – mrAsA Nov 05 '15 at 10:00
0

the connect.php resolved. But this is my problem. I include the connect.php to 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.");        
} 
?>

and this is the errors: Warning: include(./support/connect.php): failed to open stream: No such file or directory in C:\xampp\htdocs\proj1\pages\loginqueue.php on line 3

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
mrAsA
  • 17
  • 1
  • 7