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
Related
This question already has answers here:
Warning: mysqli_select_db() expects exactly 2 parameters, 1 given
(2 answers)
Closed 1 year ago.
I am using an Ajax Search form which upon connecting to the database, will show search terms upon typing.
But I am getting database connection issue on my site.
This is the code I have in my connection file (db.inc):
<?php
$username = "xxxx";
$password = "xxxx";
$hostname = "xxxx";
$database = "xxxx";
mysqli_connect($hostname, $username, $password) or die(mysql_error());
mysqli_select_db($database) or die(mysqli_error());
?>
But on the frontend, I am getting errors as below:
Warning: mysqli_select_db() expects exactly 2 parameters, 1 given in xxxx/db.inc.php on line 9
Warning: mysqli_error() expects exactly 1 parameter, 0 given in xxxx/db.inc.php on line 9
Can anyone please check what I am doing wrong? I have pasted the complete db.inc file above.
Thank you in advance for helping.
As the error is saying, mysqli_select_db() expects 2 parameters.
The first one is a link returned by mysqli_connect() and the second parameter should be the database name.
The second error means that you have to add the link to the connection as a parameter to mysqli_error.
So you'll have to write it like this:
$link = mysqli_connect($hostname, $username, $password);
if (!$link) {
die('Connection error: ' . mysqli_connect_error());
}
mysqli_select_db($link, $database));
$username = "xxxx";
$password = "xxxx";
$hostname = "xxxx";
$database = "xxxx";
$conn=mysqli_connect($hostname, $username, $password) or die(mysql_error());
mysqli_select_db($conn, $database) or die(mysqli_error());
Here, the second parameter is missing. So, the correct syntax will be
mysqli_select_db($link, $database) or die(mysqli_error());
You are using Procedural Style and in this style, two parameters will be passed in mysqli_select_db().So, the correct syntax will be
bool mysqli_select_db ( mysqli $link , string $dbname )
link
Procedural style only: A link identifier returned by mysqli_connect() or
mysqli_init()
dbname
The database name.
For more details, you can find all the details https://dev.mysql.com/doc/apis-php/en/apis-php-mysqli.select-db.html
You can also add the database name in the single line which will be
mysqli_connect($hostname, $username, $password, $database);
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
( 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.
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()
I have a joint table query but keep getting errors as below:
I have also recreated the database again but still getting same error. Checked my connection to mysql and works fine - is there anything wrong in my joint table query?
$select_quote=mysql_query("SELECT authors.name, authors.id, authors.img, authors.slug, quotes.author_id, quotes.title, quotes.id, quotes.meta_keys, quotes.meta_description, quotes.slug, quotes.content
from quotes, authors
WHERE quotes.author_id = authors.id
ORDER BY RAND() LIMIT 1 ");
while ($row=mysql_fetch_array($select_quote)) {
$author_id = $row['authors.id'];
$author_name = $row['authors.name'];
$author_slug = $row['authors.slug'];
echo" $author_slug";
}
This is my connection
ob_start();
error_reporting(E_ALL);
ini_set( 'display_errors','1');
$user_name = "root";
$password = "root";
$database = "quotes";
$server = "localhost";
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);
if ($db_found) {
echo "Working";
mysql_close($db_handle);
}
else {
print "Database NOT Found ";
}
These are the errors I am getting
Warning: mysql_query(): A link to the server could not be established
in /home/user/public_html/index.php on line 6
Warning: mysql_fetch_array() expects parameter 1 to be resource,
boolean given in /home/user/public_html/index.php on line 11
Could you please advice me on this?
Thanks
(To close this question)
You're closing your DB with mysql_close($db_handle); (you're telling it, if found then close DB connection) remove it or place it after successful query.
Plus, instead of $author_id = $row['authors.id']; do $author_id = $row['id']; and do the same for the rest.
Disclaimer: You should also consider switching to mysqli_ or PDO. mysql_ are deprecated and will be removed from future releases.