Cron job returns an error message - php

I'm trying to connect to my database via cron job. However i keep receiving an error messages. After many frustrating hours im posting here for help.
My cron script php file:
<?php
define("HOST","localhost");
define("USERNAME","user_muser");
define("PASSWORD","*********");
define("DB_DATABASE","databasename");
$conn = mysqli_connect('HOST', 'USERNAME', 'PASSWORD','DB_DATABASE');
// Check connection
if (mysqli_connect_errno())
{
"Failed to connect to MySQL: " . mysqli_connect_error();
}
// Check if server is alive
if (mysqli_ping($conn))
{
"Connection is ok!";
}
else
{
"Error: ". mysqli_error($conn);
}
mysqli_close($conn);
?>
This is the error i received:
mysqli_connect(): (HY000/2005): Unknown MySQL server host 'HOST' (0)
mysqli_ping() expects parameter 1 to be mysqli
mysqli_error() expects parameter 1 to be mysqli
mysqli_close() expects parameter 1 to be mysqli
Any help? Thanks!

You're quoting constants, which makes them strings.
define("HOST","localhost");
define("USERNAME","user_muser");
define("PASSWORD","*********");
define("DB_DATABASE","databasename");
$conn = mysqli_connect(HOST, USERNAME, PASSWORD, DB_DATABASE);

You should write constants out of quotes as following. Otherwise they are usual strings.
$conn = mysqli_connect(HOST, USERNAME, PASSWORD,DB_DATABASE);

Related

Database Selection Failed Unknown database 'login' [duplicate]

This question already has answers here:
How can i solve this "Warning: mysqli_connect(): (HY000/1049): Unknown database" problem?
(6 answers)
Database exists but returns an error saying "Unknown Database"
(1 answer)
Closed 2 years ago.
I was trying to create a login using PHP and MySQL for the database, I did everything but when I try to run the site it gives me a connection error with the database even though I created the database and gave it the corresponding name (login)
This is the code
db_connect.php
<?php
$connection = mysqli_connect('localhost', 'root', '');
if (!$connection){
die("Database Connection Failed" . mysqli_error($connection));
}
$select_db = mysqli_select_db($connection, 'login');
if (!$select_db){
die("Database Selection Failed" . mysqli_error($connection));
}
authen_login.php
<?php
require('db_connect.php');
if (isset($_POST['user_id']) and isset($_POST['user_pass'])){
// Assigning POST values to variables.
$username = $_POST['user_id'];
$password = $_POST['user_pass'];
// CHECK FOR THE RECORD FROM TABLE
$query = "SELECT * FROM `user_login` WHERE username='$username' and Password='$password'";
$result = mysqli_query($connection, $query) or die(mysqli_error($connection));
$count = mysqli_num_rows($result);
if ($count == 1){
//echo "Login Credentials verified";
echo "<script type='text/javascript'>alert('Login Credentials verified')</script>";
}else{
echo "<script type='text/javascript'>alert('Invalid Login Credentials')</script>";
//echo "Invalid Login Credentials";
}
}
?>
And the error is this
Database Selection Failed Unknown database 'login'
(I add to the question a photo of the database that maybe the error is there)
You dont have a database called shoolbrk, your database appears to be called login.
Also you you should use mysqli_connect_error() to see connection errors and not mysqli_error($connection). Thats for all other errors other than connection errors.
You can also simplify the connection script as follows
<?php
$connection = mysqli_connect('localhost', 'root', '', 'login');
// database name ^^^^^
if (!$connection){
echo $connection ->connect_error;
}
A better solution would also be to not die() or show errors to the world, instead add mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); to the connection script.
Port number issue
See this answer https://stackoverflow.com/a/62831626/2310830 which describes how WAMPServer manages MySQL and mariaDB port number usage. And how it will allow you to switch the default to MySQL so it uses port 3306 which will allow you to write your code in a way that will run anywhere without having to change the port number. As most hosting companies will have either MySQL or mariaDB listening on 3306

Connect to XAMPP MySQL Database from Document Root on another Drive

would like to ask about configuration of XAMPP mySQL database.
I have set my xampp document root to drive D, and now i unable to connect to SQL database and always get error.
Fatal error: Uncaught Error: Call to undefined function mysql_connect() in D:\localserver\connection.php:3 Stack trace: #0 D:\localserver\login.php(4): include() #1 {main} thrown in D:\localserver\connection.php on line 3
the file that handle the connection look like this
<?php
$connect = mysql_connect("localhost","root","");
if(!$connect) {
die ('connection fail!!!');
} else {
print ('connection okay!!!');
}
$connectdb = mysql_select_db('admin_login');
if(!$connectdb) {
die ('connection fail!!!');
} else {
print ('connection okay!!!');
}
?>
The mysql_connect() function is from a library that is deprecated since a couple of years and has been removed in PHP 7.
Use mysqli_connect() or PDO.
UPDATE
You can pass the name of the database into mysqli_connect() and get rid of the extra mysqli_select_db(). If you want to user mysqli_select_db() in procedural style instead of object oriented it expects the link that is returned by mysqli_connect() as the first parameter and the database name as the second one like this:
$link = mysqli_connect("localhost", $user, $password);
$db = mysqli_select_db($link, $dbname);

cannot get a connection to MySQL in php

I have this simple code:
<?php
//Open the mySQL connection
$conn_string = "'localhost', 'Vale', 'test'";
$dbh = mysql_connect($conn_string);
//Check that a connection with the DB has been established
if (!$dbh)
{
die("Error in MySQL connection: " . mysql_error());
}
...
And I get the error: Error in MySQL connection: php_network_getaddresses: getaddrinfo failed: The requested name is valid, but no data of the requested type was found.
I cannot figure out what the problem is, I have been google-ing but all the suggestions have failed (tried 127.0.0.1 instead of localhost, 127.0.0.1:3306, etc.)
I have code that works with postgre, but I need to use mysql, and I am trying to modify it, but I cannot pass the first line and get a connection. Any suggestion, please? Thank you!
mysql_connect doesn't take a comma seperated string. It takes 3 individual strings.
Change it to this:
$dbh = mysql_connect($server, $mysql_user, $password);
If you absolutely have a comma separated string, and can't get around this, you can split the string like this:
$config = str_replace("'", '', $conn_string); // replace the quotes.
$config = preg_split('/,/', $conn_string); // split string on ,
if($config != $conn_string) { // make sure this returned an array
count($config) === 3 OR die("Invalid database configuration string");
$dbh = mysql_connect($config[0], $config[1], $config[2]);
if(FALSE === $dbh) {
die("Coult not connect: " . mysql_error());
}
}
You might want to consider MySQLi, instead of MySQL.
<?php
$dbh=mysqli_connect("localhost","Vale","Password","test"); /* Vale is your username? And the name of your database if test, right? And your Username's Password is blank? */
if(mysqli_connect_errno()){
echo "Error".mysqli_connect_error();
}
?>
As other users have pointed out mysql_connect expects the database, username, and password as separate arguments rather than a single string.
I think another highly important issue to point out is that this particular extension is deprecated.
Please see: http://uk1.php.net/function.mysql-connect
A better solution would be to use mysqli_connect: http://uk1.php.net/manual/en/function.mysqli-connect.php
$db = mysqli_connect( 'localhost', 'Vale', 'test', 'yourDatabaseName' );
mysql_connect requires three argument not single string
$dbh = mysql_connect('localhost', 'Vale', 'test');

PHP is not creating MySQL database

So I'm not sure as to why PHP is not creating a database. Let me show what my code looks like first, and then I will quote what warning/error it's giving me:
<?php
// Connection
$con = mysqli_connect("localhost", "root", "1monica1");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Creating a database
// The database is called alarm_db
$sql = "CREATE DATABASE alarm_db";
if (mysql_query($sql, $con))
{
echo "Database alarm_db created sucessfully";
}
else
{
echo "Error creating database: " . mysqli_error($con);
}
?>
The error and warning I keep getting is this:
Warning: mysql_query(): A link to the server could not be established in B:\DropBox\Dropbox\EbadlyAgha\Ebad\reminders.php on line 51
Error creating database:
Line 51 is where the if (mysql_query($sql, $con)) is. Also, it won't give me the mysqli_error($con). It is just blank.
My MySQL connection is fine, because I never see the "Failed to connect to MySQL" statement.
It should be in mysqli_* while you executing DB creating query.
if (mysqli_query($sql,$con)) {
echo "Database alarm_db created sucessfully";
}
Try to avoid mysql_* statements due to the entire ext/mysql PHP extension, which provides all functions named with the prefix mysql_*, is officially deprecated as of PHP v5.5.0 and will be removed in the future.
There are two other MySQL extensions that you can better Use: MySQLi and PDO_MySQL, either of which can be used instead of ext/mysql.
Your connection is being generated by the mysqli library:
$con = mysqli_connect
but you are trying to query it using the mysql library:
mysql_query($sql,$con)
You need to be consistent about which library you use. Stick to mysqli throughout (since mysql is deprecated).
Note that mysqli_query expects the connection before the SQL:
mysqli_query($con, $sql);
You can try this to create a database using PHP:
<?php
$con = mysql_connect('localhost', 'root', '');
$query = mysql_query('create database alarm_db') or die(mysql_error());
if($query)
{
echo 'Database is created';
}
else
{
echo 'Database is not created';
}
?>

PHP Connection Error; Not allowed to connect to MySQL Server

I am trying to make a registration page where it adds the information to a SQL Table. Here is my PHP code to do this... Can anyone tell me what the error with this is?
<?php
$con=mysqli_connect("Correct Website is here","Correct Username","Correct Pass","Correct DB");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$DBsql = mysql_query("SELECT username, email from Profiles");
$sql = mysql_fetch_array($DBsql);
if(!($_POST['username'] == $sql['username']) && ($_POST['email'] == $sql['email'])) {
if ($_POST['password'] == $_POST['password2']){
$sql="INSERT INTO Profiles (firstName, lastName, username, email, password, region, group, verified)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[username]','$_POST[email]','$_POST[password]','$_POST[region]','member','no')";
}else{
echo '<script type="text/javascript">'
, 'window.alert("The passwords do not match.")'
, '</script>';
}
} else {
echo '<script type="text/javascript">'
, 'window.alert("That username or email is already in use.")'
, '</script>';
}
mysqli_close($con);
?>
It's giving these errors:
Warning: mysqli_connect() [function.mysqli-connect]: (HY000/1130): Host '31.170.161.96' is not allowed to connect to this MySQL server in /home/a5349216/public_html/register.php on line 2
Failed to connect to MySQL: Host 'ip' is not allowed to connect to this MySQL server
Warning: mysql_query() [function.mysql-query]: Access denied for user 'username'#'localhost' (using password: NO) in /home/username/public_html/register.php on line 8
Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/username/public_html/register.php on line 8
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/username/public_html/register.php on line 9
Warning: mysqli_close() expects parameter 1 to be mysqli, boolean given in /home/username/public_html/register.php on line 27
If your mysql database is not on the same server(localhost) you need to give it access from a remote server for that mysql user/database for your server. See this article for some steps that might help you. It sounds like it might be remote.
https://support.rackspace.com/how-to/mysql-connect-to-your-database-remotely/
Create new php file, just test with below syntax. let me know your error.
$con=mysqli_connect("Correct Website is here","Correct Username","Correct Pass","Correct DB");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

Categories