WAMP PHP Error: Can not connect to MySQL - php

I need help with problem that appear on my website while I tried to connect to mysql.
so, this is the error:
Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in C:\wamp\www\sites\mysite\core\connection.php on line 9
Call Stack
Time Memory Function Location
1 0.0007 163008 {main}( ) ..\index.php:0
2 0.0012 165376 include( 'C:\wamp\www\sites\mysite\core\connection.php') ..\index.php:1
3 0.0015 166400 mysql_connect ( ) ..\connection.php:9
( ! ) Warning: mysql_connect(): in C:\wamp\www\sites\mysite\core\connection.php on line 9
Call Stack
Time Memory Function Location
1 0.0007 163008 {main}( ) ..\index.php:0
2 0.0012 165376 include( 'C:\wamp\www\sites\mysite\core\connection.php') ..\index.php:1
3 0.0015 166400 mysql_connect ( ) ..\connection.php:9
MySQL Error: Accטs refusי pour l'utilisateur: 'root'#'#localhost' (mot de passe: OUI)
the php code to connect that I wrote, is:
<?php
session_start();
$dbhost = "localhost";
$dbname = "users";
$dbuser = "root";
$dbpass = "root";
mysql_connect($dbhost, $dbuser, $dbpass) or die("MySQL Error: " . mysql_error());
mysql_select_db($dbname) or die("MySQL Error: " . mysql_error());
?>

cause you are using deprecated extension so
Use mysqli or PDO (The mysql extension is deprecated)
try
mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);

By default, your MySQL credentials for WAMP are:
username: root
and no password.
$dbhost = "localhost";
$dbname = "users";
$dbuser = "root";
$dbpass = "";
And as other users mentioned, try to use mysqli or pdo as mysql is deprecated, but it's not your issue here.
Good luck!

If your using the default user of phpmyadmin and didn't set a new password than the password would be an empty string (no password), unless you set your password to root.
Your code is correct, although you should look into mysqli when you feel like you can handle it.

mysql_connect — Open a connection to a MySQL Server.
This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. 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:
mysqli_connect()
PDO::__construct()

Related

Registration system with php and mysql

im new to php and mysql (using wamp)
im getting the following error when i run my script, any idea what to fix?
Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given in C:\wamp64\www\web1\Register.php on line 79
Warning: mysqli_query() expects parameter 1 to be mysqli, string given in C:\wamp64\www\web1\Register.php on line 81
line 79 t0 83
79 mysqli_select_db($database_localhost, $localhost);
80 $query_Register = "SELECT * FROM mytable";
81 $Register = mysqli_query($query_Register, $localhost) or die(mysql_error());
82 $row_Register = mysql_fetch_assoc($Register);
83 $totalRows_Register = mysql_num_rows($Register);
Here is the database connection:
<?php
# FileName="Connection_php_mysqli.htm"
# Type="mysqli"
# HTTP="true"
$hostname_localhost = "localhost";
$database_localhost = "mydatabase";
$username_localhost = "root";
$password_localhost = "";
$localhost = mysqli_connect($hostname_localhost, $username_localhost, $password_localhost) or trigger_error(mysql_error(),E_USER_ERROR);
?>
As Andrew noted in the comment, the mysql extension was deprecated in PHP 5.5.0 and removed in PHP 7.0.0. You should be using mysqli.
Nonetheless mysql expects the second parameter to be a database resource. You create a resource like this:
$dbResource = mysql_connect('localhost', 'mysql_user', 'mysql_password');
then you would select the database with something like
mysql_select_db('your_database_name', $dbResource);
i have changed the position of the connection link and database name from
mysqli_select_db($database_localhost, $localhost);
to
mysqli_select_db($localhost, $database_localhost);
the first error disappeared, but the second error still persist
forgot to mention that (html code) registration form is not showing

having issues with php mysql connection [duplicate]

I wrote a simple PHP code to connect to the MySQL server as below
<?php
$username = "root";
$password = "Kepwd";
$hostname = "localhost:81";
//connection to the database
$dbhandle = mysqli_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
but this generates the following errors. I found some topics regarding this issue in google and Stack Overflow. but those don't help me.
( ! ) Warning: mysqli_connect(): MySQL server has gone away in C:\wamp\www\SSDConsultingNew\inc\test.php on line 8
Call Stack
# Time Memory Function Location
1 0.0014 240936 {main}( ) ..\test.php:0
2 0.0014 241528 mysqli_connect ( ) ..\test.php:8
( ! ) Warning: mysqli_connect(): Error while reading greeting packet. PID=10612 in C:\wamp\www\SSDConsultingNew\inc\test.php on line 8
Call Stack
# Time Memory Function Location
1 0.0014 240936 {main}( ) ..\test.php:0
2 0.0014 241528 mysqli_connect ( ) ..\test.php:8
( ! ) Warning: mysqli_connect(): (HY000/2006): MySQL server has gone away in C:\wamp\www\SSDConsultingNew\inc\test.php on line 8
Call Stack
# Time Memory Function Location
1 0.0014 240936 {main}( ) ..\test.php:0
2 0.0014 241528 mysqli_connect ( ) ..\test.php:8
Unable to connect to MySQL
The error is here:
$hostname = "localhost:81";
You are not connecting to MySQL, but to Apache server. If you didn't change MySQL port just use
$hostname = "localhost";
you forget to specify the database name after entering database name try again. The syntax should be like this
<?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();
}
?>

Deprecated: mysql_connect(): ERROR [duplicate]

This question already has answers here:
Deprecated: mysql_connect() [duplicate]
(12 answers)
Closed 6 years ago.
I don't know what I'm doing wrong but I receive this error:-
Deprecated: mysql_connect(): The mysql extension is deprecated and
will be removed in the future: use mysqli or PDO instead in
C:\wamp\www\includes\config.php on line 7
How can I fix this?
<?php
$mysql_hostname = "localhost";
$mysql_user = "bootstrapadmin";
$mysql_password = "";
$mysql_database = "bootstrap";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Mate you fucked something up!");
mysql_select_db($mysql_database, $bd) or die("Mate you fucked something up!");
?>
Now I'm running into the problem with my login page
Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\wamp\www\login.php on line 12
Call Stack
# Time Memory Function Location
1 0.0012 140512 {main}( ) ..\login.php:0
2 0.0079 149352 mysqli_query ( ) ..\login.php:12
( ! ) Warning: mysql_num_rows() expects parameter 1 to be resource, null given in C:\wamp\www\login.php on line 13
Call Stack
# Time Memory Function Location
1 0.0012 140512 {main}( ) ..\login.php:0
2 0.0960 149752 mysql_num_rows ( ) ..\login.php:13
<?php
session_start();
include("includes/config.php");
if ($_SERVER["REQUEST_METHOD"] == "POST") {
//username and password sent from form
$myusername = addslashes($_POST['username']);
$mypassword = md5(addslashes($_POST['password']));
$sql = "SELECT userid FROM tbl_users WHERE username='$myusername' and password='$mypassword'";
$result = mysqli_query($sql);
$count = mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if ($count == 1) {
//$session_register("myusername");
$_SESSION['login_admin'] = $myusername;
header("location: http://localhost/admin/");
}
}
?>
mysql_* functions are no longer supported. You can use mysqli_* functions instead. For eg:
<?php
$mysql_hostname = "localhost";
$mysql_user = "bootstrapadmin";
$mysql_password = "";
$mysql_database = "bootstrap";
$bd = mysqli_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Mate you messed something up!");
mysqli_select_db($mysql_database, $bd) or die("Mate you messed something up!");
?>
Have you searched for MySQLi or PDO?
The old mysql_* methods are deprecated and will be removed in the upcoming versions of PHP.
In my opinion PDO is the best way to go, but just converting to MySQLi should be sufficient. Just research a little and use one of these newer techniques.
For MySQLi basically just change the two last lines to:
$connection = mysqli_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Mate you fucked something up!");
mysqli_select_db($connection, $mysql_database) or die("Mate you fucked something up!");
(Added the "i" after "mysql" / also switched parameters on db-selection)
More information on MySQLi: http://www.w3schools.com/php/php_ref_mysqli.asp

mysql vs mysqli error when running php code in wamp stack

( mysql vs mysqli error (advait) )
Hi All, See code. This code runs on my localhost WAMP Win7 php ver 5.5.12 but it gives an error:
---
Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in C:\wamp\www\get_data02.php on line 9
Call Stack
# Time Memory Function Location
1 0.0010 135216 {main}( ) ..\get_data02.php:0
2 0.0010 135680 mysql_connect ( ) ..\get_data02.php:9
---
I tried replacing mysql with mysqli but that just gave more errors. How do I fix this? Thanks,
<?php
// 1. Enter Database details
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'iamdb_copy_04';
// 2. Create a database connection
$connection = mysql_connect($dbhost,$dbuser,$dbpass);
if (!$connection) {
die("Database connection failed: " . mysql_error());
}
// 3. Select a database to use
$db_select = mysql_select_db($dbname,$connection);
if (!$db_select) {
die("Database selection failed: " . mysql_error());
}
$query = mysql_query("SELECT * FROM users WHERE city = 'city' ");
while ($rows = mysql_fetch_array($query)) {
$f_name = $rows['first_name'];
$address_01 = $rows['address_01'];
$city = $rows['city'];
echo "$f_name<br>$address_01<br>$city<br><br>";
}
?>
There are two options to get out from this error.
Set display_errors off in php file using ini_set("display_errors",0) after start of php <?php.
Replace mysql with mysqli in mysql_connect, mysql_select_db,mysql_query, mysql_fetch_array.
Let me know if you still face any problem and also comment your error.

Warning: mysqli_connect(): MySQL server has gone away

I wrote a simple PHP code to connect to the MySQL server as below
<?php
$username = "root";
$password = "Kepwd";
$hostname = "localhost:81";
//connection to the database
$dbhandle = mysqli_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
but this generates the following errors. I found some topics regarding this issue in google and Stack Overflow. but those don't help me.
( ! ) Warning: mysqli_connect(): MySQL server has gone away in C:\wamp\www\SSDConsultingNew\inc\test.php on line 8
Call Stack
# Time Memory Function Location
1 0.0014 240936 {main}( ) ..\test.php:0
2 0.0014 241528 mysqli_connect ( ) ..\test.php:8
( ! ) Warning: mysqli_connect(): Error while reading greeting packet. PID=10612 in C:\wamp\www\SSDConsultingNew\inc\test.php on line 8
Call Stack
# Time Memory Function Location
1 0.0014 240936 {main}( ) ..\test.php:0
2 0.0014 241528 mysqli_connect ( ) ..\test.php:8
( ! ) Warning: mysqli_connect(): (HY000/2006): MySQL server has gone away in C:\wamp\www\SSDConsultingNew\inc\test.php on line 8
Call Stack
# Time Memory Function Location
1 0.0014 240936 {main}( ) ..\test.php:0
2 0.0014 241528 mysqli_connect ( ) ..\test.php:8
Unable to connect to MySQL
The error is here:
$hostname = "localhost:81";
You are not connecting to MySQL, but to Apache server. If you didn't change MySQL port just use
$hostname = "localhost";
you forget to specify the database name after entering database name try again. The syntax should be like this
<?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();
}
?>

Categories