I keep getting this whenever I run the below code:
Fatal error: Call to undefined function mysql_connect() in C:\wamp\www\php-academy\113-connect-db.php
<?php
$conn_error ='could not connect';
$mysql_host = 'localhost';
$mysql_user = 'root';
$mysql_pass = '';
mysql_connect($mysql_host, $mysql_user, $mysql_pass) or die($conn_error);
$mysql_db = 'a_database';
mysql_select_db($mysql_db) or die($conn_error);
echo 'Connected!';
?>
That probably means you don't have the MySQL extension loaded. You can test whether the extension is loaded with extension_loaded("mysql");.
I suspect that this is because the extension is not loaded. I would check your php_info(); to find out.
Once you do your code should work, although I prefer to assign the mysql_connect() to a variable and then call that variable in mysql_select_db() as I have below:
$defHost = 'localhost';
$defUsername = 'username';
$defPassword = 'password';
$defDatabase = 'database_name';
$connect = mysql_connect($defHost, $defUsername, $defPassword) or die();
mysql_select_db($defDatabase, $connect) or die();
The reasons for the error may be the following:
Incorrect access details to server database are specified in configuration file.
There are no read permissions for configuration file. Provide read permissions for the file.
Related
Just moved my site to new hosting. On the new server mysql_connect() works fine if called without using classes or inside of the method but not from the _construct. When connecting from the _construct the following error displays:
Access denied for user 'root'#'localhost' (using password: NO)...my configuration defines the user not as root but as USERNAME and does provides a password.
PHP 5.6, MySQL 5.5, Linux
Class File:
function __construct(){
error_reporting(E_ERROR);
$dbhost = 'localhost';
$dbuser = 'USERNAME';
$dbpass = 'PASSWORD';
$dbname = 'DATABASENAME';
$this->urlAppPath = "http://www.myurl.com/CD/";
// connect to the database
$this->db = mysql_connect($dbhost, $dbuser, $dbpass) or die("Connection Error: " . $SQL);
mysql_select_db($dbname);
}
function validatePerson($personID) {
$SQL = "SELECT *,COUNT(*) AS total FROM zen_customers WHERE customers_classware_id = '$personID'";
$result = mysql_query( $SQL ) or die("Could not execute query 1 . ".$SQL." ".mysql_error()." & ". $dbhost ." ".$dbuser ." ".$dbpass." ".$dbname);
}
Call Method File:
require_once('./clsCart.php');
$myclass = new clsCart();
$result2 = $myclass->validatePerson($personID);
Note
If I define and call the connection within the file that I have been trying to call the method...that is instead of calling the method it works. If I define and call the connection within the method it works...but if I define and call the connection within the construct() it does not work...error "Access denied for user 'root'#'localhost' (using password: NO)". This seems to be specific to some servers...regardless of using PHP 5.6 or lower & MySQL 5.6 or lower.
So, if I use :
$dbhost = 'localhost';
$dbuser = 'USERNAME';
$dbpass = 'PASSWORD';
$dbname = 'DATABASENAME';
// connect to the database
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die("Connection Error: " . $SQL);
mysql_select_db($dbname);
Outside of the class or within the method then it works...but, I should (as I have in the past) be able to connect within the construct().
This seems to be related to a server issue. From the __contruct() only the Database default user information is being recognized. Everywhere else in the applications uses the User created for the database, but the construct will only connect when using the default database user on this server. Other servers are not having this issue, such that any database user with all privileges work.
You have to use mysqli_connect. Please never use mysql_connect it deprecated in PHP 5.5.0 and onwards. If still you face issue please let us know.
Following code for mysqli_connect :
<?php
$con = mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
Definition and Usage
The mysqli_connect() function opens a new connection to the MySQL server.
Syntax
mysqli_connect(host,username,password,dbname,port,socket);
Parameter Description
port Optional. Specifies the port number to attempt to connect to the MySQL server.
socket Optional. Specifies the socket or named pipe to be used.
my I am trying to make login registration page in php.
apache and mysql is up and running.
I am executing the following code
<?php
$db_host = "localhost:777";
$db_username = "root";
$db_pass = "";
$db_name = "login";
mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql");
mysql_select_db("$db_name") or die ("no database");
?>
I am using localhost:777 because port 80 is being used by skype.
phpMyAdmin is up and running.
The output i am getting is nothing but the above code only.
can anyone help me on this issue?
Use MySQLi or PDO not MySQL.
You're passing the variables as a string using " " in the mysql_connect function
The syntax is as follows: mysqli_connect(host,username,password,dbname,port,socket);
<?php
$db_host = "localhost";
$db_port = "777";
$db_username = "root";
$db_pass = "";
$db_name = "login";
$conn = mysqli_connect($db_host,$db_username,$db_pass,$db_name,$db_port);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
Try that and it should work. Please make sure you understand the code above and the error you made. Do not simply copy and paste please you will not learn from it.
Im having trouble with MySQLi.
Every time I run this code it returns an error on line 13(mysql_select_bd()).
I cant figure out where the problem is.
Code:
<?php
$conn_error = 'Could not connect';
$mysqli_host = 'localhost';
$mysqli_user = 'root';
$mysqli_password = '';
$mysql_db = 'a_database';
#$mysqli_conn = mysqli_connect($mysqli_host, $mysqli_user, $mysqli_password);
mysqli_select_db('a_database', $mysqli_conn);
?>
You have an incorrect usage of the function:
mysqli_select_db('a_database', $mysqli_conn);
The connection must come first before the database name in the arguments:
mysqli_select_db($mysqli_conn, 'a_database');
// ^ connection object, then database name
Alternatively, you could also do this:
$mysqli_conn = mysqli_connect($mysqli_host, $mysqli_user, $mysqli_password, $mysql_db);
Or the object oriented interface:
$mysqli_conn = new mysqli($mysqli_host, $mysqli_user, $mysqli_password, $mysql_db); // personal preference
Instead of doing that you can do something like this:
$conn = mysqli_connect('localhost', 'root', '', 'a_database');
I'm trying to use mysql_real_escape_string() to secure a log in form.
Using this code:
include_once 'access-shared.php';
include_once 'access-databaseconnect.php';
session_start();
$email = mysql_real_escape_string(isset($_POST['email'])) ? mysql_real_escape_string($_POST['email']) : $_SESSION['email'];
$password = mysql_real_escape_string(isset($_POST['password'])) ? mysql_real_escape_string($_POST['password']) : $_SESSION['password'];
Trouble is it throws up an error every time:
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'xxx'#'localhost' (using password: NO)
I can't get my head around it, the db user has all permissions and the username details are correct in the access-databaseconnect.php file. It works perfectly without the mysql_real_escape_string around the $_POST but obviously it leaves it open to mySQL injection.
Any help is most appreciated.
EDIT: Here is the contents of the access-databaseconnect.php file:
<?php
$dbhost = 'localhost';
$dbusername = 'xxxx';
$dbpassword = 'xxxx';
function dbConnect($db='') {
global $dbhost, $dbusername, $dbpassword;
$dbcnx = #mysql_connect($dbhost, $dbusername, $dbpassword)
or die('Cannot connect to Database: '.mysql_error());
if ($db!='' and !#mysql_select_db($db))
die('Cannot connect to Database: '.mysql_error());
return $dbcnx;
}
?>
In order to use mysql_real_escape_string(), you must have already established a connection via mysql_connect(). If that does not occur in access-databaseconnect.php, or the connection has not succeeded, you will not be able to call mysql_real_escape_string()
Update
You define the function dbConnect() in access-databaseconnect.php, but you never call it. Create your connection as
$dbcnx = dbConnect($dbname);
An additional note, but not the source of your problem... Do not call mysql_real_escape_string() around the result of your isset() calls. Though it is most likely harmless, it is unnecessary.
$email = mysql_real_escape_string(isset($_POST['email'])) ? mysql_real_escape_string($_POST['email']) : $_SESSION['email'];
// Should be
$email = isset($_POST['email']) ? mysql_real_escape_string($_POST['email']) : $_SESSION['email'];
<?php
$host = 'localhost';
$db='mysql';
$user = 'root';
$pass = 123;
$conn = new PDO("mysql:host=$host;dbname=$db",$user,$pass);
if($conn){
echo 'ok';
}else {
echo 'error';
}
?>
why this code can't connect the database? my php.ini file setting is right,
extension=php_pdo.dll
extension=php_pdo_mysql.dll
the database user is root, the password is 123. thank you
If you are at the beginning of a project and are unable to make PDO work, you might be able to have more luck with mySQLi.
http://php.net/manual/en/book.mysqli.php