Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

PHP socket warnings

Posted on 2007-07-23
11
Medium Priority
?
358 Views
Last Modified: 2009-07-29
I'm getting these warnings on my server, however it works perfectly on my local machine with no warnings :(

Warning: mysql_query() [function.mysql-query]: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) in /mounted-storage/home43a/sub004/sc31952-BMDX/www/perfiles/functions.php on line 201
Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /mounted-storage/home43a/sub004/sc31952-BMDX/www/perfiles/functions.php on line 201

Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in /mounted-storage/home43a/sub004/sc31952-BMDX/www/perfiles/functions.php on line 202

Warning: mysql_query() [function.mysql-query]: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) in /mounted-storage/home43a/sub004/sc31952-BMDX/www/perfiles/functions.php on line 201

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /mounted-storage/home43a/sub004/sc31952-BMDX/www/perfiles/functions.php on line 201

Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in /mounted-storage/home43a/sub004/sc31952-BMDX/www/perfiles/functions.php on line 202

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /mounted-storage/home43a/sub004/sc31952-BMDX/www/perfiles/functions.php:201) in /mounted-storage/home43a/sub004/sc31952-BMDX/www/perfiles/viewVote.php on line 6

*************

This is the lin 202 of my functions.php file:

function getSetting($val)
{
      $sql="select * from settings where setting='$val'";
      $res=mysql_query($sql); //line 201
      while($obj=mysql_fetch_object($res)) //line 202
      {      
            return $obj->settingvalue;
      }
}
***************
viewvote.php

include("./inc/mysql.php"); //line 2
include("functions.php"); //line 3...

$db=new mysql();
session_start(); //line 6
$id=$_POST['photosid'];
0
Comment
Question by:axtur
  • 6
  • 5
11 Comments
 
LVL 4

Expert Comment

by:cg_medistox
ID: 19546640
It looks as though your connection to the server is not active?

When you create a new instance of the mysql class does your connection get opened and stay open? If you make the connection only valid for the function that opens it then once it falls out of scope there is the chance that it will close the connection.
0
 

Author Comment

by:axtur
ID: 19547149
This is the code of the mysql class, I don't find what can be going wrong :(

class mysql
{      
      var $dbhost="localhost";
      var $dbuser="root";
      var $dbpw="pass";
      var $dbname="dbname";
      var $rs;

      ////////////// init class ///////////////////
       function mysql($dbhost="", $dbuser="", $dbpw="",$dbname="")
             {
                  $this->dbhost = $dbhost ? $dbhost: $this->dbhost;
                  $this->dbuser = $dbuser ? $dbuser: $this->dbuser;
                  $this->dbpw   = $dbpw   ? $dbpw:       $this->dbpw ;
                  $this->dbname = $dbname ? $dbname:      $this->dbname;
                  $this->connect();             /////connect server
                  $this->select_db();        ///// select database
            }
      function connect($dbhost="",$dbuser="",$dbpw="")
      {      
            $dbhost      =      $dbhost      ?      $dbhost      :      $this->dbhost;
            $dbuser      =      $dbuser      ?      $dbuser      :      $this->dbuser;
            $dbpw      =      $dbpw      ?      $dbpw      :      $this->dbpw ;
                  if(!$link=@mysql_pconnect($dbhost, $dbuser, $dbpw))
                  {
                        $this->halt('Can not connect to server');
                  }
                  return $link;
      }

      function select_db($dbname="")
      {      
            $dbname=$dbname?$dbname:$this->dbname;
            return mysql_select_db($dbname);
      }
      
      function query($sql)
      {
            
            $query = mysql_query($sql);
            $this->rs=$query;            
            if(!$query)      $this->halt('MySQL Query Error');            
            return $query;
      }
      
      function fetch_object($rs="")
      {      
            $rs=$rs?$rs:$this->rs;
            return @mysql_fetch_object($rs);
            
      }
      
      function fetch_array($rs="",$result_type = MYSQL_ASSOC)
      {      
            $rs=$rs?$rs:$this->rs;
            return @mysql_fetch_array($rs, $result_type);
      }
      
      function num_rows($rs="")
      {      
            $rs=$rs?$rs:$this->rs;
            return  mysql_num_rows($rs);
            
      }
      
      function insert_id()
      {      
            return  mysql_insert_id();
      }
            
      function errno()
      {
            return mysql_errno();
      }

      function error()
      {
            return mysql_error();
      }
      
      function affected_rows()
      {
            return @mysql_affected_rows();
      }
      
      function halt($message ="")
      {      
          echo mysql_error(); echo mysql_errno ();
            echo $message;
            exit();
      }      

      function free_result($rs)
      {
             mysql_free_result($rs);
      }
      
      function close($link)
      {
            return mysql_close($link);
      }      
}
0
 
LVL 4

Expert Comment

by:cg_medistox
ID: 19547766
Here is a copy of my db connection class

<?php

/*  
      Database class to which all sites are now derived
      Code developed and maintained by Pure Pages
      Class written by Chris Lomax
      id: ver 0.1
      Revision date 3rd May 2007
*/


class db_connection
      {
            var $debug;
            var $username       = '';
            var $password       = '';
            var $database       = '';
            var $server       = '';
            var $dblink;
            var $db;
            var $query;
            var $queryarr;
            var $no_of_rows;
            var $strSQL;
            var $currentfield;

            function open_connection()
                  {
            
                        $this->dblink = mysql_connect($this->server,$this->username,$this->password);
            
                        if ( $this->dblink )
                              {
                                    $this->db = mysql_select_db($this->database);
                              }
                              
                        return $this->dblink;
                  }
                  
            //Simple Select      
            function pureselect()
                  {
                        $this->query = mysql_query($this->strSQL, $this->dblink);
                        if ( $this->query )
                              {
                                    return true;
                              } else {
                                    return false;
                              }
                  }
            
            //Simple Array
            function purearray()
                  {
                        $this->queryarr = mysql_fetch_array($this->query);
                        return $this->queryarr;
                  }
            
            //Close the connection
            function db_connection_close()
                  {
                        if ( mysql_close($this->dblink)) {
                              return true;
                        } else {
                              return false;      
                        }            
                  }
                  
            //Simple return of rows
            function pure_no_rows()
                  {
                        if ( $this->query )
                        {
                              $this->no_of_rows = mysql_num_rows( $this->query );
                              return $this->no_of_rows;
                        } else {
                              return false;
                        }
                  }
            function add_error($error)
                  {
                        $this->errorstring .= $error . "<br>\n";
                  }
            function show_errors()
                  {
                        return $this->errorstring;
                  }
            function get_link()
                  {
                        return $this->dblink;
                  }

      }

?>

As you can see, i keep the connection open by defining a local variable for the link, then i pass this link to the query for use when i make a query. At the current your script will terminate the link because it goes out of scope
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:axtur
ID: 19550242
So what should I change to make it work???

I tried with a local variable but it still keeps returning the warking :(
0
 
LVL 4

Expert Comment

by:cg_medistox
ID: 19551517
Add a variable
$link;

In the connection set the link variable to the mysql_connect

In the query set the second parameter of the function to $this->link

That way you force the query to use the connection to the database
0
 

Author Comment

by:axtur
ID: 19551648
I've changed it and keeps returning the same warnings.

class mysql
{      
      var $dbhost="localhost";
      var $dbuser="root";
      var $dbpw="pass";
      var $dbname="user";
      var $rs;
      var $link;

      ////////////// init class ///////////////////
       function mysql($dbhost="", $dbuser="", $dbpw="",$dbname="")
             {
                  $this->dbhost = $dbhost ? $dbhost: $this->dbhost;
                  $this->dbuser = $dbuser ? $dbuser: $this->dbuser;
                  $this->dbpw   = $dbpw   ? $dbpw:       $this->dbpw ;
                  $this->dbname = $dbname ? $dbname:      $this->dbname;
                  $this->connect();             /////connect server
                  $this->select_db();        ///// select database
            }
      function connect($dbhost="",$dbuser="",$dbpw="")
      {      
            $dbhost      =      $dbhost      ?      $dbhost      :      $this->dbhost;
            $dbuser      =      $dbuser      ?      $dbuser      :      $this->dbuser;
            $dbpw      =      $dbpw      ?      $dbpw      :      $this->dbpw ;
            $this->link =mysql_pconnect($dbhost, $dbuser, $dbpw);
                  if(!$this->link)
                  {
                        $this->halt('Can not connect to server');
                  }
                  return $this->link;
      }

      function select_db($dbname="")
      {      
            $dbname=$dbname?$dbname:$this->dbname;
            return mysql_select_db($dbname);
      }
      
      function query($sql)
      {
            
            $query = mysql_query($sql,$this->link);
            $this->rs=$query;            
            if(!$query)      $this->halt('MySQL Query Error');            
            return $query;
      }
0
 
LVL 4

Expert Comment

by:cg_medistox
ID: 19552008
Whilst doing some research into the problem i can across this article.

It may not be the php itself but the actual socket it is connecting through
http://www.tech-recipes.com/mysql_tips762.html

I take it by your posts that you are not on shared hosting but rather a dedicated or VS server?
0
 

Author Comment

by:axtur
ID: 19552303
I'm on a shared server (servage.net), the strange thing is that these warnings are displayed when the script is executed on the server, however there are no warnings at all on my local machine with the same script.

Despite the warnings, the script works.

I forgot to say that I only get this error in this page on the server, that is displayed on a new window (a _blank window), ther are no warnings at all on the main application.
0
 
LVL 4

Accepted Solution

by:
cg_medistox earned 1500 total points
ID: 19554288
I never noticed before from you original post, why are you using a mysql_query on the page when you have a class that performs queries?

Is this to get the settings for the server? If so have you already made a connection to the server by this point not in the class that this query can use to get the settings?
0
 

Author Comment

by:axtur
ID: 19569875
Finally I decided to use the default mysql functions, the code was developed by a programmer, and now i'm making some changes, so i really don't see the point for using a class for the queries when you can use the default functions.
0
 
LVL 4

Expert Comment

by:cg_medistox
ID: 19577543
Classes are designed to make your life easier, i use classes for just about everything and i believe that have real life models of a situation stored in a class makes life just a little bit simpler.

You should not have accepted my answers if i did not solve your problem!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question