tags:

views:

59

answers:

3

I have this PHP code:

<?php
        $username="root";
        $password="******";// censored out
        $database="bazadedate";
        mysql_connect("127.0.0.1",$username,$password); // i get unknown constant localhost if used instead of the loopback ip 
        @mysql_select_db($database) or die( "Unable to select database");
        $query="SELECT * FROM backup";
        $result=mysql_query($query);
        $num=mysql_numrows($result);

        $i=0;
        $raspuns="";
          while ($i < $num) {
            $data=mysql_result($result,$i,"data");
            $suma=mysql_result($result,$i,"suma");
            $cv=mysql_result($result,$i,"cv");
            $det=mysql_result($result,$i,"detaliu");

            $raspuns = $raspuns."#".$data."#".$suma."#".$cv."#".$det."@";

          $i++;
          }

             echo "<b> $raspuns </b>";

        mysql_close();
?>

And it should return a single string containing all data from the table. But it says "connection reset when loading page".

the log is :

[Tue Jun 15 16:20:31 2010] [notice] Parent: child process exited with status 255 -- Restarting.
[Tue Jun 15 16:20:31 2010] [notice] Apache/2.2.11 (Win32) PHP/5.3.0 configured -- resuming normal operations
[Tue Jun 15 16:20:31 2010] [notice] Server built: Dec 10 2008 00:10:06
[Tue Jun 15 16:20:31 2010] [notice] Parent: Created child process 2336
[Tue Jun 15 16:20:31 2010] [notice] Child 2336: Child process is running
[Tue Jun 15 16:20:31 2010] [notice] Child 2336: Acquired the start mutex.
[Tue Jun 15 16:20:31 2010] [notice] Child 2336: Starting 64 worker threads.
[Tue Jun 15 16:20:31 2010] [notice] Child 2336: Starting thread to listen on port 80.
[Tue Jun 15 16:20:35 2010] [notice] Parent: child process exited with status 255 -- Restarting.
[Tue Jun 15 16:20:35 2010] [notice] Apache/2.2.11 (Win32) PHP/5.3.0 configured -- resuming normal operations
[Tue Jun 15 16:20:35 2010] [notice] Server built: Dec 10 2008 00:10:06
[Tue Jun 15 16:20:35 2010] [notice] Parent: Created child process 1928
[Tue Jun 15 16:20:35 2010] [notice] Child 1928: Child process is running
[Tue Jun 15 16:20:35 2010] [notice] Child 1928: Acquired the start mutex.
[Tue Jun 15 16:20:35 2010] [notice] Child 1928: Starting 64 worker threads.
[Tue Jun 15 16:20:35 2010] [notice] Child 1928: Starting thread to listen on port 80.

Any idea why it outputs nothing?

+2  A: 
$num=mysql_numrows($result);

should be

$num=mysql_num_rows($result);

Thats atleast 1 issue.

You should also look into mysql_fetch_assoc...

# this will loop through each record
while($row = mysql_fetch_assoc($result)) {
    $data = $row['data'];
    $sum = $row['suma'];
    ....
}
Lizard
A: 

You need to enable error reporting to see what's going on... Edit php.ini, and set error_reporting=2147483647 and display_errors=1. Then run the script again to check for all errors...

ircmaxell
A: 

From the log snippet, something's killing the Apache child handling the request, which causes an immediate shutdown of the TCP connection, which your browser sees as a "Connection reset".

You may want to investigate using 'localhost' instead of '127.0.0.1' for your database connection. MySQL treats "localhost" differently than an IP address, and tries to connect via sockets, rather than a TCP connection - this is a speed optimization as local sockets are faster than TCP sockets. Your MySQL install may not be listening for TCP connections (skip_networking = 1).

As well, you should NEVER connect to your database as the root account, especially if this will be a public-facing site. The root account can access anything and do anything in MySQL, which is NOT what you want to expose to the world. Read up on the GRANT syntax and create a MySQL account with limited privileges and use that instead. Most likely your site would only need insert/update/select/delete.

Beyond that, change your query fetching logic to use what Lizard suggested above, and start doing some debugging - figure out which line is causing the error, and how many iterations it took to get there. You may be exhausting some kind of resource which kills Apache/PHP.

Marc B