Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 442
  • Last Modified:

php mysql

I have a php script which is being used to install a database on my windows server in mysql. The script creates the database and populates it. When I run the install.php file I keep getting the following error message:

Database connect failed (2005): Unknown MYSQL server host 'localhost:3307' (11004)

I have successfully installed other php  / mysql databases using this exact same server name (localhost:3307) on the same server
I have also successfully used the install.php script to install the same database on another computer where Mysql is running on a default port (3306) where the address being used is only "localhost"

I have tried using an IP address and netbios name, all with the same error message
I presume it is a port issue ?

Any ideas ?
0
doctorbill
Asked:
doctorbill
  • 9
  • 8
1 Solution
 
gr8gonzoConsultantCommented:
It's hard to say for sure without seeing the code, but I'm going to take a guess and say that on the problem server, the mysqli extension might be being used. If so, the mysqli constructor has a separate parameter for the port number.

The old mysql extension allowed the port number to be in the host name parameter after the colon.
0
 
doctorbillAuthor Commented:
mysqli is being used
how do I get around this issue
0
 
gr8gonzoConsultantCommented:
Well, if your current constructor looks like:

$mysqli = new mysqli('localhost:3307', 'user', 'password', 'db');

Then you would just change it to:

$mysqli = new mysqli('localhost', 'user', 'password', 'db', 3307);

If you're not specifying the DB name in the constructor, just pass an empty string, like:

$mysqli = new mysqli('localhost', 'user', 'password', '', 3307);
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!

 
doctorbillAuthor Commented:
would these details be in the install.php file. If so I will attach it
0
 
gr8gonzoConsultantCommented:
They might be. Code can be anywhere, so it's hard to say for sure.
0
 
doctorbillAuthor Commented:
I have attached the current install file - hope it helps
install.php
0
 
gr8gonzoConsultantCommented:
At the second function before the end of the line, you have this:


function connect_db($hostname, $username, $passwd, $database = false)
{
      $dbh = new mysqli($hostname, $username, $passwd);

      if(mysqli_connect_errno()) {
            soft_error("Database connect failed (" . mysqli_connect_errno()
                        . "): " . mysqli_connect_error());
      }

      if($database)
            $dbh->select_db($database);
      $dbh->query("SET NAMES 'utf8'");

      return $dbh;
}


Just change it to:


function connect_db($hostname, $username, $passwd, $database = false, $port = 3307)
{
      $dbh = new mysqli($hostname, $username, $passwd,"",$port);

      if(mysqli_connect_errno()) {
            soft_error("Database connect failed (" . mysqli_connect_errno()
                        . "): " . mysqli_connect_error());
      }

      if($database)
            $dbh->select_db($database);
      $dbh->query("SET NAMES 'utf8'");

      return $dbh;
}
0
 
gr8gonzoConsultantCommented:
Actually, there's probably a better way - one second.
0
 
gr8gonzoConsultantCommented:
function connect_db($hostname, $username, $passwd, $database = false)
{
      // Detect port in hostname
      $port = 3306;
      if(preg_match("/:(\d+)\$/",$hostname,$matches))
      {
        $port = $matches[1];
        $hostname = str_replace(":{$port}","",$hostname);
      }

      $dbh = new mysqli($hostname, $username, $passwd,"",$port);

      if(mysqli_connect_errno()) {
            soft_error("Database connect failed (" . mysqli_connect_errno()
                        . "): " . mysqli_connect_error());
      }

      if($database)
            $dbh->select_db($database);
      $dbh->query("SET NAMES 'utf8'");

      return $dbh;
}
0
 
gr8gonzoConsultantCommented:
Use that function instead. It'll let you still specify the host as "localhost:3307" and it should work a little better inside that particular application (which wasn't coded very well).
0
 
doctorbillAuthor Commented:
two problems:
1. there was a checkbox on the page which allowed me to create the database
"create the database (don't check if it already exists)
This is no longer there

2. I get the following:

Software Error

Message:

Error creating events table.
No database selected
SQL query: CREATE TABLE `phpc_events` (
`eid` int(11) unsigned NOT NULL auto_increment,
`cid` int(11) unsigned NOT NULL,
`owner` int(11) unsigned NOT NULL default 0,
`subject` varchar(255) collate utf8_unicode_ci NOT NULL,
`description` text collate utf8_unicode_ci NOT NULL,
`readonly` tinyint(1) NOT NULL default 0,
`catid` int(11) unsigned default NULL,
PRIMARY KEY  (`eid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
Backtrace

C:\Inetpub\wwwroot\phpcalendar\install\install.php:578 - soft_error
C:\Inetpub\wwwroot\phpcalendar\install\install.php:416 - db_error
C:\Inetpub\wwwroot\phpcalendar\install\install.php:385 - create_tables
C:\Inetpub\wwwroot\phpcalendar\install\install.php:71 - install_base

Do I need to create the database first?
0
 
gr8gonzoConsultantCommented:
There's nothing in that code change that should affect the visibility of a checkbox. Are you sure you're not dealing with a different / updated script?

It looks like there should be a config file where you specify the database name as SQL_DATABASE. I would go ahead and create the database and fill in that value in the config file.
0
 
doctorbillAuthor Commented:
I will check
0
 
nanharbisonCommented:
Are you hosting this yourself, or are you on a webhost?
0
 
doctorbillAuthor Commented:
The second code you posted worked - the database was created and it allowed me to add an admin user to it
All tables are created and correct
There is still one issue though:

when I try to access the phpcalendar index file (http://localhost/phpcalendar/index.php) I get the following issue:
Database connect failed (2005): Unknown MySQL server host "localhost:3307" (11004)

If I delete the :3307 from the config file I get:
Database connect failed (1045): Access denied for user "root@localhost" (using password: yes)
0
 
gr8gonzoConsultantCommented:
There may be another section of code that you'll have to change. Search the rest of the phpcalendar code for "new mysqli" (without the quotes). Chances are that you just need to copy that same function into a different functions library.
0
 
doctorbillAuthor Commented:
You are correct - I found another config file that needed changing

For your info:
The following line needed changing from:
$this->dbh = new mysqli(SQL_HOST, SQL_USER, SQL_PASSWD);

To:
$this->dbh = new mysqli(SQL_HOST, SQL_USER, SQL_PASSWD,"",3307);

Thanks very much for your help on this - without your script the database and admin user would not have been created.
All pages are working correctly
0
 
doctorbillAuthor Commented:
Very fast and accurate response
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 9
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now