0
votes

Warning: mysql_connect(): No connection could be made because the target machine actively refused it. in D:\xampp\htdocs\ex\ctop.php on line 22 Could not connect: No connection could be made because the target machine actively refused it.

Code

<html>

   <head>
      <title></title>
      <style type="text/css">
      body{background-image:url(img/11.jpg);
       background-size: 100% 100%;}
      table{font-size:20pt;
        color: Black;
      text-align: center;
font-weight:bold;}
th{color:       BC8F8F;}
      </style>
   </head>

   <body>
<?php
            $dbhost = 'localhost';
            $dbuser = 'corn';
            $dbpass = 'pword';

            $conn = mysql_connect($dbhost, $dbuser, $dbpass);

   if(! $conn ) {
      die('Could not connect: ' . mysql_error());
   }

   $sql = 'SELECT Distinct i.Title,i.Singer,i.Mname  FROM items i, vote v
                   where i.Title=v.Title
                   group by v.Title
                   order by sum(v.rating) Desc';
   mysql_select_db('test_db');
   $retval = mysql_query( $sql, $conn );

   if(! $retval ) {
      die('Could not get data: ' . mysql_error());
   }
    echo" <center>";
    echo "<h2>Based on ratings given by users!!</h2>";
    echo "<br/><br/><br/><br/><br/><br/>";
   echo "<table border=1>";
    echo" <tr>";

                    echo"<th>Title</th>";

                    echo"<th>Movie</th>";
                    echo"<th>Singer</th>";


               echo" </tr>";


   while($row = mysql_fetch_array($retval)) {

      echo "<tr>  <td>{$row['Title']} </td>".
           "<td>{$row['Mname']} </td>".
         "<td> {$row['Singer']}</td>  
           </tr>";
   }
    echo "</table>";
     echo "</center>";

   mysql_close($conn);
?>
</body>
</html>

I have already executed this php program. But sometimes I get this warning, so I tried reinstalling MySQL then I got output. Now again same warning I'm getting, how to completely overcome this problem?

2
mysql_* is deprecated try to use mysqli_*JYoThI
show your connection codeJYoThI
I'm getting same warning on using mysqli alsouser7822307
Have you checked your credentials for MySQL connection is right?, because mostly this error says you are accessing with wrong creadentials.Nirav Madariya
because mysql_* is deprecated, use of mysqli_* is suggested, it don't solve your problem.Nirav Madariya

2 Answers

0
votes

I had the same issue, and since everything looks just fine, the issue is in the server port. Try to specify the server port when you define your server $dbhost = 'localhost:(Server Port)';

-1
votes

I'm sure your MySQL server isn't running. So my suggestion is to recheck your server status is online or not. if not then debug why it's not online. Hope this will solve your problem.

Note: mysql_ driver for connecting with database is insecure and can cause you big security issue in your application like SQL injection so please avoid using mysql_ and use PDO this is a advanced driver for connecting with database