Edward68 Edward68 - 2 months ago 9
PHP Question

xxamp connect to database using php

I am using xammp to connect to localhost on my pc.I have the latest versions of php (PHP version: 5.5.38) and mysql. I am using port 8012 so when I access the website I am building I type the following url: localhost:8012/website/index.php. I can access the website without any issues.I created a config file to connect to the database. I created a new database. I added a new user with full privileges to the default users. When I try to connect to the database I get the following error:
Warning: mysql_connect(): MySQL server has gone away in C:\xampp\htdocs\website\config.php on line 2. I have tried just about everything to connect but I am not having any luck. Here is my config file:

<?php
$user='admin';
$pass='password';
$db ='website';

$db = new mysqli('localhost:8012',$user, $pass,$db) or die("Unable to connect");
echo"great work !!!";
?>


Can someone please tell me what I am doing wrong. Thanks. :)

Answer

Might be obvious, but if you're website is accessable on port 8012, your database is probably running on the default port of 3306.

Try adjusting your code to this and try again.

$db = new mysqli('localhost:3306',$user, $pass, $db) or die("Unable to connect");