MySQL PHP adding 3306 without my input - php

I have a back end cron script that checks MySQl regurlarly for an if-then condition and then runs a Curl action. Anyway, the script has been running fine for over a year now when suddenly a problem occured and it does not make sense to me
here is the truncated code for the script
<?php
//assign values to variables
$servername = "localhost:3306";
$username = "username";
$password = "password";
$database = "database";
$con = mysqli_connect($servername, $username, $password, $database);
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$results = "SELECT NewUserID FROM queue ORDER BY ID";
$queryresults = $con->query($results);
if($con->error) {
printf("Errormessage: %s\n", $con->error);
die();
}
I would see this error:
Warning: mysqli_connect(): (HY000/2002): Failed to parse address "localhost:3306:3306" in /home/foo/bar/BackEnd.php on line 11
Failed to connect to MySQL: Failed to parse address "localhost:3306:3306"
Fatal error: Uncaught Error: Call to a member function query() on boolean in /home/foo/bar/BackEnd.php:19
Stack trace:
#0 {main}
thrown in /home/foo/bar/BackEnd.php on line 19
The solution was simple, I change "localhost:3306" to "localhost" and everything worked again, since it seemed that ":3306" was being added to what was already there. This solution seems like a problem waiting to happen but im not sure what went wrong in the first place. Any ideas?

You can't specify the port in the host parameter.
Here is the full function definition:
mysqli_connect($host, $username, $password, $dbname, $port, $socket);
You should just add the port at the end of your call. Like this:
$con = mysqli_connect($servername, $username, $password, $database, 3306);

Related

Unknown database error when connecting sql database to server (localhost)

What should I do to solve this error?
Error:
Fatal error: Uncaught mysqli_sql_exception: Unknown database 'testdb' in D:\xampp\htdocs\dashboard\project\connection.php:6 Stack trace: #0 D:\xampp\htdocs\dashboard\project\connection.php(6): mysqli->__construct('localhost', 'root', '#Geoinformatici...', 'testdb') #1 {main} thrown in D:\xampp\htdocs\dashboard\project\connection.php on line 6
Code in Bracket IDE
<?PHP
$server = 'localhost';
$username = 'root';
$password = '#Geoinformaticionno1';
$database = 'testdb';
$connection = new mysqli($server, $username, $password, $database) or die("not connected");
echo "connected"
?>
Let me know if any other thing is required to see to solve this error.
You just have to specify the server port in these brackets.
<?PHP
$server = 'localhost';
$username = 'root';
$password = '#Geoinformaticionno1';
$database = 'testdb';
$connection = new mysqli($server, $username, $password, $database, 3307) or die("not
connected");
echo "connected"
?>
our local system or localhost has defualt user is root & password is `` that means blank.
if, you using add password from your end, for accessing phpmyadmin, then you can add your setted password. But, default, value user is: root & password is blank.

Dreamhost php sql error 'could not find driver'

I have made numerous attempts, most returning the odd error of 'could not find driver' when using PDO. I've confirmed multiple times with customer support that my credentials are correct. I've gone over the code looking for bugs, and found none. I built some simple tests, two examples below, one returning the error of 'Call to undefined function mysqli_connect()', the other returning the aforementioned ''could not find driver'.
I've been reading and googling and have yet to find a solution which is able to provide a working connection. Hoping for guidance on how to proceed
test 1: produces 'driver not found' error
$host = 'notmyHOST';
$db = 'notmyDATABASE';
$password = 'notmyPASSWORD';
$user = 'notmyUSERNAME';
$dsn = "mysqli:host=$host;dbname=$db;charset=UTF8";
try {
$pdo = new PDO($dsn, $user, $password);
if ($pdo) {echo "Connected to the $db database successfully!";}
} catch (PDOException $e) {
echo $e->getMessage();
}
//test 2: produces 'driver not found' error
//This test is a straight line-for-line copy of the example connect code
//provided by Dreamhosts knowledge-base
$hostname = "mysql.example.com"; //hostname created when creating the database
$username = "yourusername"; //username specified for database
$password = "yourpassword"; //password specified for database access
$database = "databasename"; //database name specified at creation
$link = mysqli_connect($hostname, $username, $password, $database);
if (mysqli_connect_errno()) {
die("Connect failed: %s\n" + mysqli_connect_error());
exit();
}
/*
test 3: produces 'Fatal error: Uncaught Error: Class "mysqli" not found in
/home/*****/*****/a___connectTEST4.php:25 Stack trace: #0 {main} thrown in
/home/*****/*****/a___connectTEST4.php on line 25' error
*/
$hostname = 'myHOST';
$database = 'myDATABASE';
$password = 'myPASSWORDS';
$username = 'myUSERNAME';
// Create connection
$conn = new mysqli($hostname, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
To recap, I'mooking for advice/solution/how to proceed to resolve connection error in any of the tests provided. In all tests I checked all credentials multiple times, they are correct; Haven't found a solution which maps to my problem on Stack Overflow, but I've looked at similar issues without success.

How to fix the error: "Connection failed: Access denied for user 'username'#'servername' (using password: NO)"

I am trying to implement a login system on my website and therefore I need to connect a php file to a mySQL database. One problem, I keep getting this error:
Connection failed: Access denied for user 'username'#'servername' (using password: NO)
Does anyone know how to fix this error? Or can anyone tell me what I'm doing wrong?
I know this question has been asked like a million times already, but none of those answers have worked for me. They all are solutions for a local server and database, where mine is at a webhost so i think this works different.
Here's my code:
<?php
$servername = "servername";
$dBUsername = "db username";
$dBPassword = "db passwordd";
$dBName = "db name";
$dBPort = "db port";
$conn = new mysqli($servername, $dBUsername, $dPassword, $dBName, $dBPort);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
The variable name you are using in the password parameter slot is not the same as the one you defined above ($dBPassword vs $dPassword):
$dBPassword = "db passwordd";
// ...
$conn = new mysqli($servername, $dBUsername, $dPassword, $dBName, $dBPort);
In PHP the undefined variable "$dPassword" will be treated as a NULL which is likely why you're getting that "using password: NO" in the returned error.

Cant connect to DB. Won't Stop Loading

I cant figure out why i am not connecting to my db. It seems like it is finding the host but having authentication problems or something. I am using xampp and the db provided with it. My connect code is as follows
<?php
$servername = 'localhost:1234';
$username = 'protech';
$password = '';
$dbname = 'protech';
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}else{
echo "Connected successfully";
}
mysqli_close($conn);
exit;
?>
if you need any information I am by my computer and will respond quick.
I get these error messages after waiting a while.
Warning: mysqli::__construct(): MySQL server has gone away in C:\xampp\htdocs\protech_connect.php on line 9
Warning: mysqli::__construct(): Error while reading greeting packet. PID=7764 in C:\xampp\htdocs\protech_connect.php on line 9
Warning: mysqli::__construct(): (HY000/2006): MySQL server has gone away in C:\xampp\htdocs\protech_connect.php on line 9
Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\protech_connect.php on line 9
The standard port for mySQL is 3306 - so if you are genuinely using 1234 then append that as a new parameter to the db connection constructor.
$dbhost = 'localhost';
$dbuser = 'protech';
$dbpwd = 'xxx';
$dbname = 'protech';
$dbport = 1234;
$conn = new mysqli( $dbhost, $dbuser, $dbpwd, $dbname, $dbport );
if( $conn->connect_error ) exit( 'Bad foo: '.$conn->connect_error );
else { /* do exciting things */ }
$conn->close();
The mysqli_connect() function doesn't accept a port in the host parameter. You need to add the port number in another parameter like so:
$conn = mysqli_connect($servername, $username, $password, $dbname, $serverport);
Function documentation: mysqli::__construct()
If you get following error or problem then follow above solution.
Error:
Warning: mysqli_connect(): MySQL server has gone away
Warning: mysqli_connect(): Error while reading greeting packet. PID=3528
Warning: mysqli_connect(): (HY000/2006): MySQL server has gone away
Solution 1:
use 127.0.0.1 instead of localhost. Don't use any port number after 127.0.0.1.
Solution 2:
Check your host file located at
C:\Windows\System32\drivers\etc
and remove any localhost or 127.0.0.1 entry in file and save. You can use any HostEditor program to edit this file.

Fatal error: Call to a member function set_charset()

I am using Cpanel to develop my application. It gives the error
Fatal error: Call to a member function set_charset() on a non-object in /home/restaur /public_html/restaurant/includes/connect_database.php on line 3
when I access to "www.yyyyy.com/filename/"
And here is my connect_database script
<?php
include($_SERVER['DOCUMENT_ROOT'].'/YOUR_FOLDER/variables/variables.php');
$connect->set_charset('utf8');
?>
my close_database script
<?php
include($_SERVER['DOCUMENT_ROOT'].'/YOUR_FOLDER/variables/variables.php');
$connect->close();
?>
my variable script
<?php
// database configuration
$host ="db_host_name";
$user ="db_username";
$pass ="db_pasword";
$database = "database_name";
$connect = new mysqli($host, $user, $pass,$database) or die("Error : ".mysql_error());?>
it should be like
$host ="db_host_name";
$user ="db_username";
$pass ="db_pasword";
$database = "database_name";
// variables.php
$connect = new mysqli($host, $user, $pass, $database);
/* check connection */
if ($connect->connect_errno) {
printf("Connect failed: %s\n", $connect->connect_error);
exit();
}
Now in connect_database.php
// Make sure your included path is correct
include($_SERVER['DOCUMENT_ROOT'].'/YOUR_FOLDER/variables/variables.php');
global $connect;
$connect->set_charset('utf8');
this will help you to know more in depth global
I was having this exact issue, i was having encoding problems but adding
$connect->set_charset('utf8');
(my variable was actually $conn->set_charset('utf8');) just after
$conn = new mysqli($servername, $username, $password, $dbname);
and it worked beautifully.

Categories