• 7

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

PDO not working with Port

I am trying to connect to an external database using PDO.

$dbh = new PDO('mysql:host=hotsname;port=3309;dbname=dbname', 'root', 'root');

However this is not working on one particular server I have.

I thought maybe only a certain host was allowed, but I checked the mysql rules and also tried this code on a server I have personally and the connection worked.

So I know the code works and that their is no block on my side and that the firewall is accepting requests to port 3309 and passing it to the correct server on port 3306.

So the issue is solely on one server. As a test I thought I'd open port 3306 to test the code without specifying a port.

$dbh = new PDO('mysql:host=hotsname;dbname=dbname', 'root', 'root');

This worked straight away.

So my question is, why when adding the port does this cause it not to be able to connect to the database. It is only this particular server that has this issue.

Many Thanks for your time.


The error given is simply "Cannot connect to mysql server on xxx.xxx.xxx.xxx".

I have gotten slightly further now. The code is now working providing I have a firewall rule to allow incoming on 3306 to the server using 3306. So even though I have specified a port, it seems to ignore this and force it to go out of 3306.

      • 2
    • What does it do instead of working (what error does it produce)? Does the connecting user have permission to connect remotely from the connecting host? Is MySQL listening for TCP connections (skip-networking not enabled in my.cnf)
      • 1
    • @MichaelBerkowski I have updated my question. Also skip-networking is disabled. The connecting user has permissions as proven by me using the same code on other servers successfully as well as checking the mysql table and ensuring the allowed host is wildcard %. It certainly seems as though the server I am having trouble with just ignores the port completely and only uses 3306. It is a hosted server with cpanel.
      • 1
    • @TheHumbleRat Ah, try to debug it with other clients then too like mysql -hhostname -P3309 -uroot -proot Beyond that you may need to talk with the host's support. Your PDO is correct the way you have it.
      • 2
    • @MichaelBerkowski great suggestion. Have tried this via ssh and not working either. So definitely seems I need to talk to the host. Hoping this would not be the case, but that's life. Many thanks for all your help, definitely appreciated.

You probably using localhost as a hostname. Switching it into instead should solve the port ignorance problem.

  • 14
Reply Report
      • 1
    • This solved my problem (proxy connections through ssh). Any insight on why localhost ignores port while behaves 'properly'?
      • 1
    • @sea MySQL ends up using the unix socket instead of TCP. On the command line I've seen the same issue. There you can solve it by forcing TCP protocol with parameter: --protocol=TCP

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions