Cannot connect to mySQL DB [duplicate] - php

This question already has an answer here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 2 years ago.
Our lovely domain host switched us to a new server, but didn't bother to mention that we need to upgrade all mySQL lines. :-(
We have double checked, but cannot figure out why we can connect to the DB with test code, but not with the live code. The connection works, below, but fails at the "mysqli_select_db" line below.
Can anyone help, please.
}
}if (!(mysqli_connect(CONFIG_SQL_HOST, CONFIG_SQL_USERNAME, CONFIG_SQL_PASSWORD, CONFIG_SQL_DATABASE))) {
exit('mySQL username/password incorrect.');
;
}
if (!(mysqli_select_db(CONFIG_SQL_DATABASE))) {
exit('mySQL Database incorrect.');
;*
}

First try to know the reason.
$conn = new mysqli(CONFIG_SQL_HOST, CONFIG_SQL_USERNAME, CONFIG_SQL_PASSWORD, CONFIG_SQL_DATABASE)
if ($conn->connect_errno) {echo "MySQLError " . $conn->connect_errno . " " . $mysqli->connect_error;exit(0);
}

Related

I have been trying to connect to my local server but i keep getting errors [duplicate]

This question already has an answer here:
Should we ever check for mysqli_connect() errors manually?
(1 answer)
Closed 2 years ago.
I keep getting this error"
Warning: mysqli_error() expects parameter 1 to be mysqli, boolean given in C:\xampp\htdocs\php-contact-form\index.php on line 8
this is the connection i used:
$conn = mysqli_connect("localhost","root","Guyle4u2021","newDolce") or die("Connection Error: " . mysqli_error($conn));
By the looks of it, you need to remove the "or die("Connection Error: " . mysqli_error($conn))" and see what happens.
Also, id recommend switching to PDO because it's more consistent and is more widely used by the community, therefore additional support.

MYSQLi queries returning blank [duplicate]

This question already has answers here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 5 years ago.
So I had my website working but the hoster forced me to migrate, so I had to backup all I had and change it to the new host. The code was working but since I changed I cant get this code working. The connection to the database seems fine.
The code had to be changed from mysql to mysqli
All I get from mysqli_num_rows or mysqli_query is blank.
I checked the encoding and its UTF-8.
The query works fine on phpmyadmin
Since I only have ftp access to the publish folder Im not sure what options i have to find the problem.
$link = mysqli_connect($hostname,$username, $password) OR DIE ("Unable to connect to database! Please try again later." . mysqli_connect_error());
mysqli_set_charset($link, "utf8");
mysqli_select_db($dbname, $link);
$result = mysqli_query($link, "SELECT * FROM $usertable ORDER BY Date DESC");
//echo "result:" . $result; returns blank
echo "results:" . mysqli_num_rows($result); returns blank

Replacing mysql_real_escape_string(): [duplicate]

This question already has answers here:
How to change mysql to mysqli?
(12 answers)
Closed 2 years ago.
As mysql_real_escape_string is now deprecated, I have to change one function on the site that is using it. For the life of me, I can't figure out proper mysqli or pdo code to use. Maybe someone can guide me at the right direction. This is how it currently looks.
if (isset($_GET['btnSearch']) && !empty($_GET['txtSearch'])) {
$txtSearch = trim(mysql_real_escape_string($_GET['txtSearch']));
if (preg_match("/^(?i)BAW[0-9]+/", $txtSearch)) {
$pilot->pilot_num = strtoupper($txtSearch);
} else {
$pilot->name = $txtSearch;
}
}
Thank you all.
To replace mysql_real_escape_string with mysqli_real_escape_string you need to have an already opened connection to your DB like this:
$DBH = new mysqli($dbhost, $dbusername, $dbpasswd, $database_name);
then you can replace
mysql_real_escape_string($_GET['txtSearch'])
with
$DBH->real_escape_string($_GET['txtSearch'])
As it appears, I already have open connection and framework handles the query. All that needed is removal of
mysql_real_escape_string

Deprecated: mysql_connect() Error Code [duplicate]

This question already has an answer here:
The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead [duplicate]
(1 answer)
Closed 8 years ago.
My host recently upgrade the PHP version and a certain part of my website now shows the following error:
Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in url/structure/here on line 49
That is referencing the below code:
function DBConnect() {
$this->connectCount ++;
//echo "$this->connectCount<br>";
if ($this->dbType == 'mysql') {
$dbConnect = mysql_connect($this->dbHost, $this->dbUser, $this->dbPasswd) or die ("MySql Connection Failed: " . mysql_error());
mysql_select_db($this->dbName, $dbConnect);
}
if ($this->dbType == 'postgresql') {
$dbConnect = pg_connect("host=$this->dbHost port=$this->dbPort dbname=$this->dbName user=$this->dbUser password=$this->dbPasswd") or die ("PostgreSQL Connection Failed: " . pg_errormessage($dbConnect));
//$dbConnect = pg_pconnect("host=$this->dbHost port=$this->dbPort dbname=$this->dbName user=$this->dbUser password=$this->dbPasswd") or die ("PostgreSQL Connection Failed: " . pg_errormessage($dbConnect));
}
return $dbConnect;
}
I'm aware the fact that this is because the current way my site connects to MYSQL is now outdated in the new version of PHP but does anyone know how I would update the above code to make this work?
The easier way is to use mysqli_connect(). The syntax is very similar to what you would had with mysql_connect(),which means the changes in your code will be minor and easy to make.
Pdo would be the safest, but if you are trying to get you site back on quickly, the mysqli_* commands will achieve that.
Google (or check on stackoverflow) mysql vs mysqli. You ll find plenty of examples.
Hope this helps.
Good luck
--
Sorry after re-reading i see you asked what needs to be change on your source code. Afraid i cannot help right now as i am responding from a mobile phone :(

How do I fix Function mysql_list_tables() is deprecated? [duplicate]

This question already has answers here:
what is the alternate function mysql_list_tables() in php 5
(5 answers)
Closed 9 years ago.
I'm trying to add a voting system for my website, and I get this error:
Deprecated: Function mysql_list_tables() is deprecated in /home/yokoscap/public_html/vote/classes/vote.class.php on line 11
Access denied for user 'yokoscap'#'localhost' (using password: NO)
I'm new to PHP and MySQL, so if you could dumb it down/help me fix it it'd be great.
Here's the code paste, I thought it was too big to go in here so I uploaded to pastebin.
Did you check the manual? There is a complete example on a replacement for it:
<?php
$dbname = 'mysql_dbname';
if (!mysql_connect('mysql_host', 'mysql_user', 'mysql_password')) {
echo 'Could not connect to mysql';
exit;
}
$sql = "SHOW TABLES FROM $dbname";
$result = mysql_query($sql);
if (!$result) {
echo "DB Error, could not list tables\n";
echo 'MySQL Error: ' . mysql_error();
exit;
}
while ($row = mysql_fetch_row($result)) {
echo "Table: {$row[0]}\n";
}
mysql_free_result($result);
use this sql query
show tables from dbname
hope this will help
The mysql_list_tables() function has been deprecated since PHP 4.3.7 (!) because it's entirely too specific.
To get a list of tables in the current database, run a SHOW TABLES query. It's a perfectly normal query; you don't need a special function just to do that.
I says here in the manual
This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include:
According to mysql_list_tables ...changelog secton
This function became deprecated from version 4.3.7
Use SHOW TABLES

Categories