News:

Tekforums.net - The improved home of Tekforums! :D

Main Menu

php issue on new server

Started by neXus, August 12, 2006, 18:46:10 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

neXus

Ok, the following has worked fine on one host but does not now and the other code bit works

Could you tell me why this is not working please with the error.


/* connect to the Mysql Server */
function makeConnection() {

/* Databse access information */
require("dbacess.inc.php");

$sqlconnect = mysql_connect($dbhost, $dbuser, $dbpass);
if (!$sqlconnect) {
echo (failed connection to the server);
}
else {
return $sqlconnect;
}
}

/* Get Connection the the required database */
function getConnection($dbname) {

$select = mysql_select_db($dbname, makeConnection());
if (!$select) {
echo(cant select database);
}
else {
return $select;
}
}

dbacess -> Has the values for dbname etc.

Very basic file i use for simple applications, non class.
Works fine on many hosts etc.

getConnection(databasename);
Thats used in the php pages using it.

Works fine bar new host where i get this error:

Warning: mysql_connect(): Cant connect to local MySQL server through socket /tmp/mysql.sock (2) in ......includes/misc.php on line 38
failed connection to the server


line 38 is the ->
$sqlconnect = mysql_connect($dbhost, $dbuser, $dbpass);


If yo do the old standard:


$hostname="";
$username="";
$password="";
$dbname="";

//getConnection(veng_booking);
mysql_connect($hostname,$username, $password) OR DIE ("Unable to connect to database! Please try again later.");
mysql_select_db($dbname);


In said page it works fine

The call of other files and includes are fine since basic other non database functions work ok.

Cant quite see what it is since it works on other servers and more or less is the same thing tbh

Mardoni

without seeing dbacess.inc.php I would guess that either:

a) the $dbhost, $dbuser, $dbpass variables are not set, or
b) their out of scope to the function.

On your old host did you have to do $_POST["varname"] to access Post variables or could you access them directly ?

If its the former then changing your connect routine to this should sort it out:


function makeConnection() {
 
global $dbhost, $dbuser, $dbpass;

  /* Databse access information */
  require("dbacess.inc.php");
 
  $sqlconnect = mysql_connect($dbhost, $dbuser, $dbpass);


neXus

/* **** File Name: dbaccess.inc **** */
/* **** Version: 1 **** */
/*
**** File Information:

// Notes: need to include error file and make error functions

Databse access

****
*/
/* **** Author: Liam Dilley **** */

$dbhost = "";
$dbname = "";
$dbuser = "";
$dbpass = "";
?>


The values are correct since they work if you put them in an actual page

Did not have to use $_POST no

Mardoni

Youve definately used this code before and seen it work ?

The only thing I can think of, without knocking up a test harness, is that the return value from mysql_connect (the resource identifier) is not being passed into mysql_select_db correctly. It needs to be a pointer and not a value.

I cannot remember whether its &$Var or just &var...anyway try this:


$select = mysql_select_db($dbname, &makeConnection());

neXus

Quote from: NimrodYouve definately used this code before and seen it work ?

The only thing I can think of, without knocking up a test harness, is that the return value from mysql_connect (the resource identifier) is not being passed into mysql_select_db correctly. It needs to be a pointer and not a value.

I cannot remember whether its &$Var or just &var...anyway try this:


$select = mysql_select_db($dbname, &makeConnection());

Its worked fine for ages

Although class now www.m3t.co.uk first run that, and the class version is more or less the same tbh anyway, there, first gen of my OO approach.

Basically i got a small booking thing used in this basic set up, straight ported it to the new host I am on and does not work, really odd.

Ill give your suggestion a go

EDIT:

Dont work, really odd, i just made a function just with the normal sql connection as it would be if i put it in the page itself

$hostname="";
$username="";
$password="";
$dbname="";
mysql_connect($hostname,$username, $password) OR DIE ("Unable to connect to database! Please try again later.");
mysql_select_db($dbname);

Just in a function, same error
Only works if i put it internally


in the file works on other hosts as ai said, tried just include no change.

Really odd.

neXus

that little gallery program i showed in another thread just used this scipt basic while I was learning php image manipulation, same thing

I think it my be an sql directory thing..
http://www.linuxforum.com/forums/index.php?showtopic=3846

neXus

emailed host

QuoteThank you for your response. Currently our shared hosting accounts have disabled socket connections

Mardoni

Erm, you wanna ask them how youre supposed to connect to MySQL then.

I didnt realise there was a non-socket / non-TCP/IP based option for MySQL !

Mardoni

Tbh m8, Im totally dumbfouned by their comment.

AFAIK MySQL only has socket based connection libraries. For them to say that socket based connections are disabled is the same as saying that you cannot connect to MySQL on their hosting :o (Now this might not be true, it is possible that there is some other way to communicate with a MySQL server, I just don;t know it...yet ;)) This is obviously not the case as you have proved that moving the connection code into a procedural PHP script allows the connection to work.

The only rational explanation is that there is some strange PHP configuration in play on that server causing your object based implementation to break. As to what this could be I have no idea :(

My suggestion to move forward would be to create a single script that connects, selects and outputs some data to the browser. Then take the script and break each major part into a seperate function. Perhaps end up with functions for "connect", "dbselect", "dataselect" and "output". See if that all works. Then put your DB stuff into a class (still in the same single file) and try again. Finally, move the DB class out into an include file.
Hopefully at some point the basic script will stop working and the solution to that problem will solve the bigger issue !

FWIW, I recently took a backup of a very simple site I developed onto a new hosting companys server for compatibility testing. I immediately hit a problem that I am yet to resolve where exception handing causes the PHP scripts to error !!! If I rem out all try { } catch { } blokes in the scripts, the site works fine (without exception handling) !! Crazyness !

Mardoni

ffs, I think Im being a tit :(

Quote from: php.orgNote:  Whenever you specify "localhost" or "localhost:port" as server, the MySQL client library will override this and try to connect to a local socket (named pipe on Windows). If you want to use TCP/IP, use "127.0.0.1" instead of "localhost". If the MySQL client library tries to connect to the wrong local socket, you should set the correct path as  mysql.default_host  string   in your PHP configuration and leave the server field blank.

The important bit there is that if you use a "named" connection, the PHP mySQL library attempts to connect over a "local socket" (file on linux / pipe on Windows).

You need to force your connection over TCP/IP to get around the socket/pipe restriction. So basically, use 127.0.0.1 instead of localhost.

That should do it !

I hope ;)

neXus

Tried this m8, got:

Warning: mysql_connect(): Cant connect to MySQL server on 127.0.0.1 (111) in

Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in

Warning: mysql_query(): Cant connect to local MySQL server through socket /tmp/mysql.sock (2)

I will pm you something odd with private data to you, the way in first thred is working on one place.