remove mysqli_connect warning displaying in page - php

$mysqlServer = "***";
$mysqlDb = "***";
$mysqlUser = "***";
$mysqlPass = "***";
$conn = mysqli_connect($mysqlServer, $mysqlUser, $mysqlPass) or die("failed to connect to db");
mysqli_select_db($conn, $mysqlDb) or die("failed to connect select db");
i have this code, and its working without any problem. But if i try to input a wrong sql server or test it to perform an error. This will display:
Warning: mysqli_connect(): (HY000/2002): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
failed to connect select db
i don't want the warning to display if ever theres a problem in connecting the sql server. i just want my own error to display.

2 possible options:
set the error_reporing level to NOT to show warnings http://php.net/manual/en/function.error-reporting.php
put a # sign before mysqli_connect, this supresses the warning message

putting # sign before each function hide errors
$conn = #mysqli_connect($mysqlServer, $mysqlUser, $mysqlPass) or die("failed to connect to db");

Try this one:
$conn = mysqli_connect($mysqlServer, $mysqlUser, $mysqlPass, $mysqlDb);
Pass the DB name with the connect as fourth param.

Related

Build database if mysqli return error constant 'ER_BAD_DB_ERROR'

Hey guys I want to be able to run queries to build up my database if mysqli returns error constant of 'ER_BAD_DB_ERROR'/1049. Here is my code. If the error is caught I don't want mysqli to display the error but rather just built the database.
What happens it that the unknown database error is display, error is caught in the if() statement and the database is built.
I realize mysqli if displaying the error because of this:
$connection = mysqli_connect($db_server, $db_user, $db_pwd, $dbname);
How do I just build the database in place of displaying the error if that exact error that I want is caught.
$db_server = "localhost";
$db_user = "user";
$db_pwd = "";
$dbname = "name";
#connect to database
$connection = mysqli_connect($db_server, $db_user, $db_pwd, $dbname);
#try to catch database not exist error
if ('ER_BAD_DB_ERROR') {
# if true build database and connect again
build_db();
$connection = mysqli_connect($db_server, $db_user, $db_pwd, $dbname);
} else {
#close connection with other errors encounter
die("database connection failed". mysqli_connect_error());
}
I'm not sure if I'm following best practices here. So, I'm wholeheartedly open to suggestions and feedback. Thanks Guys.
You are looking for
mysqli::$connect_errno — Returns the error code from last connect call
Example:
$link = mysqli_connect('localhost', 'user', 'password', 'my_db');
// check if the connect call returned a database object
if (!$link) {
// check the particular error number why the connect failed
if (mysqli_connect_errno() === ER_BAD_DB_ERROR) {
// the database does not exist, so build it now
build_db();
} else {
// something else caused the connect to fail
}
}
This assumes the constant ER_BAD_DB_ERROR exists. I haven't checked if it does. If it doesn't exist, you will need to define it with a value corresponding to the correct error code returned by mysqli_connect_errno.
You can also use mysqli_connect_error() to get a textual representation of the error and compare that instead of just the error code.
Quoting the PHP Manual on where to find the error codes:
Client error message numbers are listed in the MySQL errmsg.h header file, server error message numbers are listed in mysqld_error.h. In the MySQL source distribution you can find a complete list of error messages and error numbers in the file Docs/mysqld_error.txt.
Note that this will still display a message about the missing database. You can either introduce a custom error handler for this particular error or just put an error suppression operator in front of the mysqli_connect call, e.g. change it to #mysqli_connect.

mySQL connection error using php codes

"Warning: mysql_connect(): A connection attempt failed because the connected party did not properly respond after a period of time or established connection failed because connected host has failed to respond. in C:\xampp\htdocs\appraisal\database.php on line 5
No database selected"
Why does it show most of the time? But when I refresh the page by entering F5, the error will be gone and will show the output correctly. This doesn't really affects the system but it is quite bothering when our users see the error.
I have the codes here.
<?php
$dbhost='localhost';
$dbuser='root';
$dbpass='';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db('db_appraisal');
?>
Hoping for your response. Thanks!
Try adding the $conn parameter to the mysql_select_db function call
<?php
$dbhost='localhost';
$dbuser='root';
$dbpass='';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db('db_appraisal', $conn);
?>
Link to The Manual
If you are writing new PHP code you should really not be using the mysql_* extension as it has been deprecated i.e. it will be removed from PHP sometime soon.
Use either the mysqli_* extension or the PDO one.

php warning when logging in website folder by cpanel

I am writing some php script in the cpanel. When I access the website folder
(e.g. www.bbbbbb.com/folder/)
It give the warning like that.
Warning: mysqli::mysqli() [mysqli.mysqli]: (HY000/2005): Unknown MySQL server host 'host07.portalwebhosting' (1) in /home/restaur/public_html/restaurant/variables/variables.php on line 7
Warning: mysqli::set_charset() [mysqli.set-charset]: Couldn't fetch mysqli in /home/restaur/public_html/restaurant/includes/connect_database.php on line 3
Warning: mysqli::stmt_init() [mysqli.stmt-init]: Couldn't fetch mysqli in /home/restaur/public_html/restaurant/includes/login_form.php on line 45
Fatal error: Call to a member function prepare() on a non-object in /home/restaur/public_html/restaurant/includes/login_form.php on line 46
And I use the default account admin to access, it gives me the same result.
And here is my php coding
<?php
// database configuration
$host ="host07.portalwebhosting.com";
$user ="user";
$pass ="password";
$database = "monkey";
$connect = new mysqli($host, $user, $pass,$database) or die("Error : ".mysql_error());
// access key to access API
$access_key = "12345";?>
check your connection string or may be you need to try with port
$connection = new mysqli("host", "user", "pwd", "db", "3306");
no need to change port 3306 is default
why you use die("Error : ".mysql_error()); use mysqli

Unable to access MySQL database

I'm trying to use MySQL with my first website, and using biz.nf to host it.
The website control panel gives me the following info for the MySQL database I have created, which I have stored in a login.php file
<?php
$db_hostname = (hostname);
$db_database = (db name);
$db_username = (username);
$db_password = '********'; //not showing here
$db_port = '3306';
?>
I then try and access it with the following code:
<?php
require_once 'login.php';
$db_server = mysql_connect($db_hostname, $db_port, $db_username, $db_password);
if (!$db_server) die("Unable to connect to MySQL: " . mysql_error());
?>
Which doesn't work, giving me the following error.
Warning: mysql_connect() [function.mysql-connect]: Access denied for user '3306'#'83.125.22.189' (using password: YES) in /srv/disk12/1570263/www/erfbattle.co.nf/index.html on line 18
Unable to connect to MySQL: Access denied for user '3306'#'83.125.22.189' (using password: YES)
I tried doing a port check on fdb4.biz.nf with port number 3306, and found that the port was closed. I'm kind of new to this, and trying to figure out what could be wrong here.
You have put your parameters in the wrong order. As you'll notice in your error, the port is being passed as the username. You should change the order of your parameters.
That being said, you shouldn't use mysql_ functions, as mentioned in the comments above. Use mysqli or PDO instead.
Your statement
$db_server = mysql_connect($db_hostname, $db_port, $db_username, $db_password);
should be
$db_server = mysql_connect($db_hostname.":".$db_port, $db_username, $db_password);
Port 3306 is the default port for MySql. Try the hostname with Localhost. And in the control panel assign the Username to the database. In Cpanel there will be a option to do this.

Can't select database table even though the code is right

I am trying to display a list of my vbulliten threads on a non-vbulliten portion of my site. However I can't select the vbulliten database:
<?php
$host = "localhost";
$user = "my username";
$pass = "my password";
$dbname = "tableprefix_forum";
mysql_connect($host, $user, $pass) or die ("Could not connect to database server.");
mysql_select_db($dbname) or die ("Could not select database.");
?>
I am substituting some things here in this example but all my credentials are correct including my db server username, password and forum db name. So what is the problem? Is it due to some internal security feature in vbulliten, does this system not allow you to connect to it's db if the page trying to connect to it is a non-vbulliten page?
Vbulletin has NO control over the permissions given by the server. But you do need to make sure that the user/pass you are using has been granted permission to access the database you are requesting.

Categories