Page 1 of 1

Suggestions about not being able to connect to mysql

PostPosted: 23. February 2014 21:42
by szmontsaroff
When running the CD Collection demo, it fails with the following message

CD Collection (Example for PHP+MySQL+PDF Class)
A very simple CD programm.

CD list as PDF document.


Warning: mysql_connect(): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. in E:\DevTools\xampp\htdocs\xampp\cds.php on line 78
Could not connect to database!
Is MySQL running or did you change the password?


Looking at phpmyadmin, it reports mysql as inactive.

I have verified that it is running, and have made no changes to the default XAMPP confirguration.

This occures even if my AV system is off.

I am running Windows 7 and the lates XAMPP release.

Any suggestions would be welcome.

Re: Suggestions about not being able to connect to mysql

PostPosted: 05. March 2014 19:01
by jake0
I had the same issue with XAMPP for Windows 1.8.3-3. If you look at the source for htdocs/xampp/cds.php it shows the file is using the now deprecated mysql_connect instead of mysqli_connect. Further, the password is hard-coded as "". If you would like to use the CD Collection demo, then you can hardcode your password in the mysql_connect lines (there are at least two lines that need to be changed).

I rewrote the mysql_connect functionality to use mysqli functions. Save the original cds.php as cds_old.php and then copy and paste the source provided below into cds.php. If you saved the source provided blow as a separate file (cds2.php) some of the database functions will not work correctly due to callbacks to cds.php.

cds.php:
_____________________________________

Code: Select all
<?php include("langsettings.php"); ?>

<?php
if (urlencode(@$_REQUEST['action']) == "getpdf") {
      $link = mysqli_connect("localhost","root","","cdcol") or die("Error " . mysqli_error($link));
      $query = "SELECT titel, interpret, jahr FROM cds" or die("Error in the query..." . mysqli_error($link));
      $result = mysqli_query($link, $query);
      if (!$result) {
         printf("Error: %s\n", mysqli_error($link));
         exit();
      }
        include ('fpdf/fpdf.php');
        $pdf = new FPDF();
        $pdf->AddPage();

        $pdf->SetFont('Helvetica', '', 14);
        $pdf->Write(5, 'CD Collection');
        $pdf->Ln();

        $pdf->SetFontSize(10);
        $pdf->Write(5, '© 2002/2003 Kai Seidler, oswald@apachefriends.org, GPL');
        $pdf->Ln();

        $pdf->Ln(5);


        $pdf->SetFont('Helvetica', 'B', 10);
        $pdf->Cell(40 ,7, $TEXT['cds-attrib1'], 1);
        $pdf->Cell(100 ,7, $TEXT['cds-attrib2'], 1);
        $pdf->Cell(20 ,7, $TEXT['cds-attrib3'], 1);
        $pdf->Ln();

        $pdf->SetFont('Helvetica', '', 10);

        //$result=mysql_query("SELECT titel,interpret,jahr FROM cds ORDER BY interpret");

      while($row = mysqli_fetch_array($result)) {
            $pdf->Cell(40, 7, $row['interpret'], 1);
            $pdf->Cell(100, 7, $row['titel'], 1);
            $pdf->Cell(20, 7, $row['jahr'], 1);
            $pdf->Ln();
      }


        $pdf->Output();
        exit;
    }
?>

<html>
<head>
<title>apachefriends.org cd collection</title>
<link href="xampp.css" rel="stylesheet" type="text/css">
</head>

<body>

&nbsp;<p>
<h1><?php print $TEXT['cds-head']; ?></h1>

<?php print $TEXT['cds-text1']; ?><p>
<?php print $TEXT['cds-text2']; ?><p>

<?php

//    Copyright (C) 2002/2003 Kai Seidler, oswald@apachefriends.org
//
//    This program is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program; if not, write to the Free Software
//    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.


?>

<h2><?php print $TEXT['cds-head1']; ?></h2>

<table border=0 cellpadding=0 cellspacing=0>
<tr bgcolor=#f87820>
<td><img src=img/blank.gif width=10 height=25></td>
<td class=tabhead><img src=img/blank.gif width=200 height=6><br><b><?php print $TEXT['cds-attrib1']; ?></b></td>
<td class=tabhead><img src=img/blank.gif width=200 height=6><br><b><?php print $TEXT['cds-attrib2']; ?></b></td>
<td class=tabhead><img src=img/blank.gif width=50 height=6><br><b><?php print $TEXT['cds-attrib3']; ?></b></td>
<td class=tabhead><img src=img/blank.gif width=50 height=6><br><b><?php print $TEXT['cds-attrib4']; ?></b></td>
<td><img src=img/blank.gif width=10 height=25></td>
</tr>


<?php
$link = mysqli_connect("localhost","root","","cdcol") or die("Error " . mysqli_error($link));
   if(@$_REQUEST['interpret']!="")
   {
      $titel=mysqli_real_escape_string($link, $_REQUEST['titel']);
      $interpret=mysqli_real_escape_string($link, $_REQUEST['interpret']);
      $jahr=intval($_REQUEST['jahr']);
      if($jahr=="")$jahr="NULL";
      mysqli_query($link, "INSERT INTO cds (titel,interpret,jahr) VALUES('$titel','$interpret',$jahr);");
   }

   if(@$_REQUEST['action']=="del")
   {
      mysqli_query($link, "DELETE FROM cds WHERE id=".round($_REQUEST['id']));
   }

   $result=mysqli_query($link, "SELECT id,titel,interpret,jahr FROM cds ORDER BY interpret;");
   
   $i=0;
   while( $row=mysqli_fetch_array($result) )
   {
      if($i>0)
      {
         echo "<tr valign=bottom>";
         echo "<td bgcolor=#ffffff background='img/strichel.gif' colspan=6><img src=img/blank.gif width=1 height=1></td>";
         echo "</tr>";
      }
      echo "<tr valign=center>";
      echo "<td class=tabval><img src=img/blank.gif width=10 height=20></td>";
      echo "<td class=tabval><b>".htmlspecialchars($row['interpret'])."</b></td>";
      echo "<td class=tabval>".htmlspecialchars($row['titel'])."&nbsp;</td>";
      echo "<td class=tabval>".htmlspecialchars($row['jahr'])."&nbsp;</td>";

      echo "<td class=tabval><a onclick=\"return confirm('".$TEXT['cds-sure']."');\" href=cds.php?action=del&id=".$row['id']."><span class=red>[".$TEXT['cds-button1']."]</span></a></td>";
      echo "<td class=tabval></td>";
      echo "</tr>";
      $i++;

   }

   echo "<tr valign=bottom>";
        echo "<td bgcolor=#fb7922 colspan=6><img src=img/blank.gif width=1 height=8></td>";
        echo "</tr>";


?>

</table>

<h2><?php print $TEXT['cds-head2']; ?></h2>

<form action=cds.php method=get>
<table border=0 cellpadding=0 cellspacing=0>
<tr><td><?php print $TEXT['cds-attrib1']; ?>:</td><td><input type=text size=30 name=interpret></td></tr>
<tr><td><?php print $TEXT['cds-attrib2']; ?>:</td><td> <input type=text size=30 name=titel></td></tr>
<tr><td><?php print $TEXT['cds-attrib3']; ?>:</td><td> <input type=text size=5 name=jahr></td></tr>
<tr><td></td><td><input type=submit border=0 value="<?php print $TEXT['cds-button2']; ?>"></td></tr>
</table>
</form>
<?php include("showcode.php"); ?>

</body>
</html>

Re: Suggestions about not being able to connect to mysql

PostPosted: 05. March 2014 19:29
by jake0
Addressing MySQL showing as deactivated on the Status page, simply change the htdocs/xampp/mysql.php file using the code below. This issue is also due to the use of the now deprecated mysql_connect. In this case, simply adding an 'i' to mysqli_connect is all that is required. Note that the "pma" MySQL user does not have a password by default. You can change the password by either using the MySQL commands or by using phpmyadmin (localhost/phpmyadmin/). You would then need to change the password in this file as well (htdocs/xampp/mysql.php). This file (and the above cds.php) could be improved by requiring a connection file instead of hardcoding passwords in multiple files.

General Syntax: mysqli_connect("localhost", "[USER]", "[PASSWORD]", "[DATABASE]")

htdocs/xampp/mysql.php

Code: Select all
<?php
   if (@mysqli_connect("localhost", "root", "", "cdcol")) {
      echo "OK";
   } else {
      echo "NOK";
   }
?>