MySQL query runs okay on test machine, but fails on ISP server - php

I am testing with MySQL on my home machine using an Apache server under XAMPP with the MySQL settings: Server: localhost via TCP/IP, version 5.5.16.
I have a query: SELECT * FROM project WHERE refno = $refno;
This works perfectly on my test machine. When I try the same thing on my ISP server, which has the following settings: Server: localhost via UNIX socket, version 5.0.92
I get the message:
"supplied argument is not a valid MySQL result resource"
Is the problem the difference in the settings? Is there anything I can do to get it working?

I would recommend you first make absolutely sure you have a valid database connection. Look for mysql_connect in your script, and make sure it is being passed the proper parameters. Look for the code examples on the man page to see how to add die() calls to catch any problems. Using die() is not a practice I would recommend on production code, though. You would usually want to catch and log errors, instead of having the script die.
Once you have made sure your connection returns a ressource, if it still chokes on you make sure you are sending the proper parameter (database name) to mysql_select_db.
Once these forementioned two functions play nice, your query should execute no problem.
Hope that helps, good-luck.

Related

PHP - How to get data from database which is in another pc?

A quick question. I have two laptops, installed with WAMP, both using MYSQL.
I tried ping each other -> success
Configure the phpmyadmin -> require and allow ip addresses ->success
View phpmyadmin of another pc typing 192.168.0.*->succes
But when i tried to putting php code on laptop A with '192.168.0.*' as host , it came out an error, saying server not responding, run time error...
Try giving the following line in the mysql configuration file. And make sure the privilege is there for the user.
bind-address=YOUR-SERVER-IP

PHPUnit database connection error

Very simple-to-explain problem here (at least after struggling with it and simplifying it almost up to the absurd).
I do a connection via the OCI driver inside a file called whatever.Test.php. Just that, make a simple query, and exit. I then call the file from the browser, and the data taken from the database is properly displayed. Next step: I run PHPUnit over the same file, with the following result:
PHP Warning: oci_connect(): ORA-12154: TNS:could not resolve the connect identifier specified in ...
Any ideas?
The final solution was a little bit obvious once I came up with it. Somehow the server could figure out what the server I was trying to connect with was, via the tnsnames.ora file somewhere, I guess. However, since the script execution doesn't trigger all the server processes this information was not reachable any more from there. So what I needed to do is to provide all the data in the oci_connect() $connection_string parameter, using the format [//]host_name[:port][/service_name][:server_type][/instance_name] (check documentation). Previously I was only giving the host_name part.
(Thanks anyway for your reply, user*).

PHP SQL Server Connection

I am using SQL Server as my backend for program.when i am try to connect with database which not shows any type of errors,but it is not working.Which is not getting connection.
My code is like
mssql_connect('servername', 'db_user', 'db_password') or die('Error');
The funny thing is which is not connecting and also which is not show message Error.
How can we enable SQL Server configuration using php program (what is the script for that.not manually). How could we know the actual status of SQL Server in server?
My application is working properly in local;the problem is about public hosting.
turn on error_reporting in your php.ini. it might be some other section of code that is causing the issue, since you say that it doesn't go to die part. try checking first if it even goes to the mysql_connect() part. if possible post a part of your code around the mysql_connect.
I would bet it is localhost, I have, nor do I ever wish to, see the database sitting on the same server as the website. Check in your hosting control panel for the proper connection string.

Confusing PDO-only problem : Can't connect through socket/Access denied/Can't connect to server (shared host)

So the problem changed from what it was, i'll leave the original question below to prevent bad reviews on answers like I had after someone editing his question I answered :
So I am working on a (really lame) shared hosting which has PDO installed, but it doesn't work.
With default parameters
<?php
try {
$dbh = new PDO('mysql:host=localhost;dbname=THE_DB_NAME', 'THE_USER', 'THE_PASSWORD');
echo 'Connected to database';
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
it throws this message :
SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
With a simple mysql_connect, it works.
And the socket path seems correct (both phpinfo and this query :
show variables like 'socket';
confirm.
Localhost redirects to 10.103.0.14 (this data comes from mysql_get_host_info() and in phpMyAdmin)
In the PDO, if i replace localhost by 127.0.0.1 i will get
SQLSTATE[HY000] [2003] Can't connect to MySQL server on '127.0.0.1' (111)
And if i replace localhost by 10.103.0.14 :
Access denied for user 'USER_NAME'#'10.103.0.14' (using password: YES
Both IP adress (127.0.0.1 and 10.103.0.14) work with mysql_connect.
So apparently the problem comes from the PDO connection.
Does somebody knows where this could come from, or/and any way to fix it ?
Some server datas :
The PHP Version : 5.2.10
You can see the server's phpinfo : http://web.lerelaisinternet.com/abcd.php?v=5
No command line possible.
(i know it should be the tech suport's job, but they're reaaaaaly slow)
Thanks
Previous question :
How to find the mysql.sock on a shared host (tricky way needed...)
So today's problem is : The PDO connection doesn't work on a shared host, and it's supposed to (it's installed on the server).
Just a basic PDO connection :
<?php
try {
$dbh = new PDO('mysql:host=localhost;dbname=THE_DB_NAME', 'THE_USER', 'THE_PASSWORD');
echo 'Connected to database';
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
throws this message :
SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
A regular mysql connection :
mysql_connect("localhost", "THE_USER", "THE_PWD") or die(mysql_error());
mysql_select_db("24DLJLRR1") or die(mysql_error());;
echo 'Connected to database <br/>';
works fine.
So apparently it cannot find the .sock.
I think specifying the correct address should work, i tried some "classic" mysql path that I found on internet, without success.
The phpinfo says it is at this adress (/var/lib/mysql/mysql.sock)
(The PHP Version is 5.2.10)
You can see the server's phpinfo : http://web.lerelaisinternet.com/abcd.php?v=5
So i am trying to figure out where the hell it is !!!
I tried to look in the phpMyAdmin interface, but i couldn't find the info, plus it seems that phpMyAdmin connects to a different server (it has a different IP adress, and trying to connect to it with php gives a "Wrong password" error). The mysql_connect also connects to this adress, i think it redirects to a different server with some internal password/login.
Well if you have any idea of how to obtain this info (the provider's technical support is "fixing the problem"... it's been 1 month...).
Also maybe the problem comes from somewhere else, but the same stuff works on other shared hosts...
The need of PDO is because I use the Symfony framework with Doctrine for this website, and the Doctrine plugin needs PDO... I don't want to redo the website from scratch !
Thanks for your help !
This was already marked as answered, but not really solved (without changing databases).
So, just in case someone like me also experiences this problem...
The easiest way to fix this is to first get the socket path (either by looking in the php.ini file or by using: phpmyadmin or the console (or construct it in mysql or mysqli)
...to run the following query (anything but PDO):
show variables like 'socket'; //as mentioned by symcbean
THEN, in the PDO connection string, change it to use the socket instead of a hostname:
$dbc = new
PDO("mysql:unix_socket=/var/run/mysqld/mysqld.sock;dbname=$DBName",
$User, $Password, array(PDO::ATTR_PERSISTENT => true)); // using
persistent connections
This worked for me.
FWIW, I had this issue and changed my host from 'localhost' to '127.0.0.1'.
I have no clue why localhost wasn't working, but that did the trick.
Odd thing is, we have tons of servers and it works on almost every one using 'localhost'
Is your server running with SeLinux enabled (enforcing)? If it is, try running as root:
# setsebool -P httpd_can_network_connect on
Can you try 127.0.0.1 as the server name instead of localhost?
IIRC, with some mySQL drivers / adapters, this decides whether the socket is used for establishing the connection or not.
Using the connection which works, run the query:
show variables like 'socket';
(this behaves just like a select statement)...and you'll get the path of the running socket.
Then check the file permissions.
I had the problem that production version worked just fine and a test version wasn't able to connect PDO :/
both versions was located at same servers, test in a sub directory.
The fix was replacing in DSN the localhost for ip.
'mysql:host=localhost;dbname=db'
became
'mysql:host=127.0.0.1;dbname=db'
try:
exec('`which mysql_config` --socket');
this should show you the configured socket.
I found the reason for the strange behaviour. If bind-address is different to 127.0.0.1 or 0.0.0.0 (all addresses) PDO can't connect to 127.0.0.1.
For what it's worth, I found this page after having the exact same issue. I am on a server running Apache & PHP only - MySQL is installed on another machine. I tried both the DNS name of the server and its IP and confirmed I could ping it. A PHP app on the same machine is talking to the database fine, using old syntax mysql_connect( ). But PDO from the CLI was throwing this error.
The solution for me was to check my DSN. Any typo in the DSN itself is ignored silently, and PDO assumes you mean localhost. My issue was I had "name=" instead of "dbname=" in the DSN.
The Issue In the Mysql configuration It you need to disable the option of skip-networking
in my.conf configuration file this should work fine
reference
http://www.wolfcms.org/forum/post7098.html#p7098
I just solved a similar issue. My guess is you probably replaced your mysql_connect() statement with the PDO equivalent. Don't forget you still have lots of other code dependent on that old connection statement. Try keeping the mysql_connect in place while writing in the PDO code.
What worked for me was specifying the port number like so:
mysql:hostname;port=3306;dbname=dbname;
This got it to work when connecting to a local database. Now I'm working on getting it to work with a remote db.
My problem may be different to the OP, but I thought it was worth posting. I did a software upgrade on a VM, then rebooted and got the OP's error message. It turned out to be an out-of-memory problem preventing mysql from starting. Deleting a few large files made the problem go away.
One year later, I found a solution for this issue : using a SQLite database. PDO worked fine, but not with MySQL
** EDIT ** as everyone is downvoting this: This solved my issue (I'm the OP). I was using Doctrine, so switching RDBMS was easy and quick. Also the website was some a home made CMS, with very few trafic, so SQLite was fine.
I know it's not a real "Answer" to the problem, but if someone is in the same context: a crappy shared hosting which you can't change with this weird PDO-MySQL bug AND is using doctrine. This IS a solution. I can delete this answer, but if I had thought of this at the time of the OP, I would have saved a lot of time.

pg_connect crashes my php script

I'm trying to run a PHP script which has pg_connect($connection_string) and it just crashes my PHP script. I'm running it off of xampp on my computer, here are some facts:
If I put exit("test"); immediately above the pg_connect statement, it successfully displays the word "test" and terminates the script, so I know that my installation of xampp is working.
Using phpinfo() I can see that the postgresql extension is indeed loaded.
I can connect to the database server from pgadmin, so it's not a firewall issue or anything like that.
If I remove this exit statement, the pg_connect statement just hangs. There is no warning displayed or logged, and it never even gets past the function call. I even have:
$db_crm = pg_connect($connection_str);
if (!$db_crm) die("connection failed");
And "connection failed" is never even displayed. My browser just shows "this page cannot be displayed",after timing out.
What in the world could be causing this?
It's doubtful that the call is crashing PHP. More likely is that for some reason, the call is hanging for some reason and PHP's max execution time is being exceeded. You can try extending the time limit before making the pg_connect() call to see if it eventually comes back with something.
Check the Apache error logs
Check the php error logs
Make sure you have logging enabled in your Postgres config file.
In your config, set
log_min_error_statement (DEBUG5)
to grab everything possible.
Check the postgres error logs
Here it is guys:
I have no reason why, but adding sslmode=disable to the end of my connection string made it work. What reason would it have to crash? I am on a windows machine and phpinfo() says OpenSSL is enabled..
This sounds really stupid, but is your server running under SSL? I've had problems where a server will try to authenticate to ssl and hang indefinitely, trying to connect to a non-existent port.
sslmode=disable did the trick for me. To disable ssl in postgres-config ( ssl = false ) also worked.
For me, the Apache logs revealed that PHP was not finding the pg_connect() function, even though php-postgresql was installed. Restarting Apache fixed this, i.e.
sudo service httpd restart

Categories