8 Replies - 1666 Views - Last Post: 18 July 2016 - 01:07 AM Rate Topic: -----

#1 tonyal   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 85
  • Joined: 23-September 14

PHP connecting to database problems.

Posted 17 July 2016 - 02:32 PM

Hello all, I have been trying to figure out for about 6 hours now why I can't connect to a database using PHP. I'm not sure if it's a PHP, database, or server problem but I'm desperate. I've tried the code various ways following different guides on the internet, and found out some of it was depreciated.
Before I post the code, here are a few things that may or may not help:

*I'm using the newest WAMP server - I had to change the apache port to 8080 to get the icon to turn green (80 is being used by Microsoft SQL Server apparently - I have to use it for another class and WAMP for this class).

*I can't get to local host by typing localhost - it has to be localhost:8080

*I turned off all the features for IIS

*I can load other pages that are in the www folder in WAMP that came with it (testmysql.php)

Here are some I've tried:
<?php
$con = mysqli_connect("localhost:8080","root",NULL,"test_database");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>



<?php
$conn = new mysqli('localhost:8080', 'root', NULL, 'test_database', 3306);
?>


I also tried 127.0.0.1 instead of local host

<?php
$db_host = "localhost:8080";
$db_username = "root";
$db_pass = "";
$db_name = "test_database";

@mysql_connect($db_host,$db_username,$db_pass) or die ("Could not connect you fool!");
@mysql_select_db(db_name) or die ("No database bro");

echo"Yay, it worked!";
?>



That's just a few. I've been at this all day and I think the neighbors might be worried about me due to the amount of cussing I'm sure they've heard. If you can help me I swear I will name my first-born after you. Thanks in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: PHP connecting to database problems.

#2 astonecipher   User is offline

  • Enterprise Software Architect
  • member icon

Reputation: 3150
  • View blog
  • Posts: 11,955
  • Joined: 03-December 12

Re: PHP connecting to database problems.

Posted 17 July 2016 - 02:48 PM

Using multiple servers on a single computer, you would need to specify the port. I have 5 different servers on mine, IIS, apache, and Zend in a few different configurations, that isn't the issue.


Your issue is, MySQL is not on port 8080, it is on 3306 by default. Your connection string should be similar to this,
$conn = new mysqli('localhost', 'root', '', 'test_database');


However you should also know if there are errors occurring. So,
try {
  $conn = new mysqli('localhost', 'root', '', 'test_database');
} catch ( Exception $e ) {
  echo "<p>{$e->getMessage()}</p>";
}


Was This Post Helpful? 1
  • +
  • -

#3 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: PHP connecting to database problems.

Posted 17 July 2016 - 02:57 PM

For the first version what is the error message? If there isn't an error message then I wonder how you know that it isn't connecting?
Was This Post Helpful? 0
  • +
  • -

#4 tonyal   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 85
  • Joined: 23-September 14

Re: PHP connecting to database problems.

Posted 17 July 2016 - 03:39 PM

View Postandrewsw, on 17 July 2016 - 02:57 PM, said:

For the first version what is the error message? If there isn't an error message then I wonder how you know that it isn't connecting?


I get three errors showing on the browser page:
Warning: mysqli_connect(): MySQL server has gone away in C:\wamp64\www\mysql_connect.php on line 2

Warning: mysqli_connect(): Error while reading greeting packet. PID=8532 in C:\wamp64\www\mysql_connect.php on line 2

Warning: mysqli_connect(): (HY000/2006): MySQL server has gone away in C:\wamp64\www\mysql_connect.php on line 2

View Postastonecipher, on 17 July 2016 - 02:48 PM, said:

Using multiple servers on a single computer, you would need to specify the port. I have 5 different servers on mine, IIS, apache, and Zend in a few different configurations, that isn't the issue.


Your issue is, MySQL is not on port 8080, it is on 3306 by default. Your connection string should be similar to this,
$conn = new mysqli('localhost', 'root', '', 'test_database');


However you should also know if there are errors occurring. So,
try {
  $conn = new mysqli('localhost', 'root', '', 'test_database');
} catch ( Exception $e ) {
  echo "<p>{$e->getMessage()}</p>";
}



Thank you, I will try that.
Was This Post Helpful? 0
  • +
  • -

#5 tonyal   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 85
  • Joined: 23-September 14

Re: PHP connecting to database problems.

Posted 17 July 2016 - 03:59 PM

View Postastonecipher, on 17 July 2016 - 02:48 PM, said:

Using multiple servers on a single computer, you would need to specify the port. I have 5 different servers on mine, IIS, apache, and Zend in a few different configurations, that isn't the issue.


Your issue is, MySQL is not on port 8080, it is on 3306 by default. Your connection string should be similar to this,
$conn = new mysqli('localhost', 'root', '', 'test_database');


However you should also know if there are errors occurring. So,
try {
  $conn = new mysqli('localhost', 'root', '', 'test_database');
} catch ( Exception $e ) {
  echo "<p>{$e->getMessage()}</p>";
}



Maybe I'm trying to get to the page wrong? I was typing the local host port and the php doc name in the browser?
When I copy and pasted your code and then did a browser search for http://localhost/mysql_connect.php I get an error saying that the page can't be found.
When I change it to http://localhost:808...sql_connect.php I get a blank page.
When I try http://localhost:330...sql_connect.php I get page saying native_password got packet out of order with a bunch of strange symbols and letters (it wouldn't let me paste it all here).

Thank you for the help. I'm new to using this and trying to figure it out by watching tutorials that are all different!
Was This Post Helpful? 0
  • +
  • -

#6 astonecipher   User is offline

  • Enterprise Software Architect
  • member icon

Reputation: 3150
  • View blog
  • Posts: 11,955
  • Joined: 03-December 12

Re: PHP connecting to database problems.

Posted 17 July 2016 - 04:12 PM

I don't know the page you are trying to connect to, so I don't know it there is content on that page. Try this for a reference.

try {
  $conn = new mysqli('localhost', 'root', '', 'test_database');
  echo "<p>Connected to database now.</p>";
} catch ( Exception $e ) {
  echo "<p>{$e->getMessage()}</p>";
}



I don't deal with OR like mysqli, and use PDO personally and professionally. But, the connection should still work for you.

And based on what you said, typing,

localhost:8080 into the url bar, should take you to the index page in your htdocs directory.
Was This Post Helpful? 1
  • +
  • -

#7 tonyal   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 85
  • Joined: 23-September 14

Re: PHP connecting to database problems.

Posted 17 July 2016 - 04:36 PM

View Postastonecipher, on 17 July 2016 - 04:12 PM, said:

I don't know the page you are trying to connect to, so I don't know it there is content on that page. Try this for a reference.

try {
  $conn = new mysqli('localhost', 'root', '', 'test_database');
  echo "<p>Connected to database now.</p>";
} catch ( Exception $e ) {
  echo "<p>{$e->getMessage()}</p>";
}



I don't deal with OR like mysqli, and use PDO personally and professionally. But, the connection should still work for you.

And based on what you said, typing,

localhost:8080 into the url bar, should take you to the index page in your htdocs directory.


Thank you, for the love of everything that is great - it's finally working! Man, I wish I could buy you a beer right now. :bananaman:
Was This Post Helpful? 0
  • +
  • -

#8 astonecipher   User is offline

  • Enterprise Software Architect
  • member icon

Reputation: 3150
  • View blog
  • Posts: 11,955
  • Joined: 03-December 12

Re: PHP connecting to database problems.

Posted 17 July 2016 - 04:43 PM

It's confusing and takes some getting use too, but it gets easier.
Was This Post Helpful? 2
  • +
  • -

#9 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4278
  • View blog
  • Posts: 13,563
  • Joined: 08-June 10

Re: PHP connecting to database problems.

Posted 18 July 2016 - 01:07 AM

View Postastonecipher, on 17 July 2016 - 11:48 PM, said:

However you should also know if there are errors occurring. So,
try {
  $conn = new mysqli('localhost', 'root', '', 'test_database');
} catch ( Exception $e ) {
  echo "<p>{$e->getMessage()}</p>";
}


unlike PDO, mysqli does not throw an exception on connection failure--unless you enable it beforehand.

This post has been edited by Dormilich: 18 July 2016 - 01:07 AM

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1