This question already has answers here:
Warning: mysqli_select_db() expects exactly 2 parameters, 1 given
(2 answers)
Closed 5 years ago.
I have code that works with php 5.6 but not with php 7.0. It's very short, so I thought I'd be able to modify it without a problem, but I was wrong. The original script is below, followed by my attempt to use mysqli. Could some kind person please show me what I need to do to get it right?
OLD CODE
<?php
$db_host = "localhost";
$db_name = "database_name";
$db_user = "user_name";
$db_pass = "pass";
$link = mysql_connect($db_host, $db_user, $db_pass) or die("Could not connect to database as ".$db_user."#".$db_host."!");
mysql_select_db($db_name) or die("Could not select database ".$db_name);
?>
NEW CODE
<?php
$db_host = "localhost";
$db_name = "database_name";
$db_user = "user_name";
$db_pass = "pass";
$link = new mysqli_connect($db_host,$db_user,$db_pass) or die('Unable to establish a DB connection');
mysqli_select_db($db_name) or die("Could not select database ".$db_name);
?>
Add $link in mysqli_select_db, refer here for more information
mysqli_select_db($link, $db_name)
Related
This question already has answers here:
mysql: connection refused when trying to connect to localhost using remote IP
(3 answers)
Closed 2 years ago.
I looked around but i honestly have no idea what im doing, i had like 4 error when i started but i got it down to one. Ive tried to changed the username but that didnt really help.
<?php
$mysql_username = "root";
$mysql_password = "";
$db_name = "bvruggg";
$server_name = "192.168.104.1";
$conn = mysqli_connect($server_name, $mysql_username, $mysql_password, $db_name);
?>
im using xampp to host this and i made a database and a table to the best i could following the guide I found
If you are using XAMPP, try changing your server_name value to localhost:
<?php
$mysql_username = "root";
$mysql_password = "";
$db_name = "bvruggg";
$server_name = "localhost";
$conn = mysqli_connect($server_name, $mysql_username, $mysql_password, $db_name);
?>
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Hi I'm new to PHP and i coded a php page and created a database. I tried to connect db but it's not connecting.
<?php
$uname = "root";
$pwd = "";
$hostn = "localhost";
//connection to the database
$mysqlconn = mysqli_connect($hostn, $uname, $pwd)
or die("Unable to connect to MySQL");
//select a database to work with
$dbselect = mysqli_select_db("dataforuse",$mysqlconn)
or die("Could not select dataforuse");
mysql_close($mysqlconn);
?>
Hi Try to debug and echo bugs in connection with mysqli_error() function.
$uname = "root";
$pwd = "";
$hostn = "localhost";
//connection to the database
$mysqlconn = mysqli_connect($hostn, $uname, $pwd) or die(mysqli_connect_error());
or
$mysqlconn = mysqli_connect($hostn, $uname, $pwd , $database) or die(mysqli_connect_error());
You are mixing mysql and mysqli
You have to replace
/connection to the database
$mysqlconn = mysqli_connect($hostn, $uname, $pwd)
or die("Unable to connect to MySQL");
//select a database to work with
$dbselect = mysqli_select_db("dataforuse",$mysqlconn)
or die("Could not select dataforuse");
With this code
$mysqlconn = mysqli_connect($hostn, $uname, $pwd,"dataforuse")
or die("Unable to connect to MySQL");
connection variable then database name ..try this
$dbselect = mysqli_select_db($mysqlconn,"dataforuse");
and mysql close should be like this
mysqli_close($mysqlconn);
dont mix mysql with mysqli
You should write
<?php
$uname = "root";
$pwd = "";
$hostn = "localhost";
$dbname = "dataforuse";
$conn = new mysqli($hostn, $uname, $pwd, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
This will solve Your Problem
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.
I have always used the following for MySQL database connection for procedural coding not OO running on PHP 4 and now I am moving some old websites to PHP 5.4:
<?php
global $db_user;
global $db_pass;
global $db_host;
global $db_name;
$db_user = 'USERNAME';
$db_pass = 'PASSWORD';
$db_host = 'HOST';
$db_name = 'DBNAME';
function do_query ( $sql )
{
global $db_user;
global $db_pass;
global $db_host;
global $db_name;
mysql_pconnect( $db_host, $db_user, $db_pass )or die("Cant connect to $db_host: ");
mysql_select_db( $db_name )or die("Cant select $db_name: ");
$res = mysql_query( $sql )or die("You messed up in your sql Using <b>$sql</b>\n\r" . mysql_error());
return $res;
}
?>
How can I change this to work with PHP 5.4 as all that is required for my website is to change this file as everything else now is up to date and the current way I connect with MySQLi never output errors the same way?
Thanks in advance.
UPDATE:
I tried the following:
<?php
$db_user = 'USERNAME';
$db_pass = 'PASSWORD';
$db_host = 'HOST';
$db_name = 'DBNAME';
function do_query ( $sql )
{
mysqli_connect( $db_host, $db_user, $db_pass )or die("Cant connect to $db_host: ");
mysqli_select_db( $db_name )or die("Cant select $db_name: ");
$res = mysqli_query( $sql )or die("You messed up in your sql Using <b>$sql</b>\n\r" . mysqli_error());
return $res;
}
?>
And use a mysqli_close(); at the end but it still doesn't work.
I also tried using it procedurally with:
$link = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db');
if (!$link) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
But I can't seem to make that work as a function.
I have done loads of tests with different versions but I didn't want to over fill the page with information most people already know so I just put up the original code I have used in the past as I have so many other variations I have tried. I was not just trying to pass the buck, just trying to explain what exactly I was trying to convert to MySQLi. Sorry if people think this is pointless but I am stuck. Thanks.
Three flaws in one post.
First, using a function to create a connection and then query is a bad idea.
Second, you're using a persistent connection. So even when the page is done the connection remains. You'll exhaust your pool of database connections like that.
Third, you're using mysql_ functions, which are deprecated.
So here's the preferred way to do things now, using mysqli and an object approach. I don't recommend $GLOBALS normally but it sounds like you're working with legacy code
$mysqli = new mysqli($db_host, $db_user, $db_pass, $db_name);
function do_query($sql) {
$mysqli = $GLOBALS['mysqli'];
$res = $mysqli->query($sql)or die("You messed up in your sql Using <b>$sql</b>\n\r" . mysqli->error);
return $res;
}
Im trying to move my database connection script to an external (and therefore more secure) file. However, it isnt working.
Here my PHP page (with include link)
<?php
include 'block/datalogin.php';
..etc
And heres block/datalogin.php
$dbhost = "localhost";
$dbuser = "************";
$dbpass = "***********";
$dbname = "************";
#mysql_connect($dbhost, $dbuser, $dbpass) or die("unable to
connect to database."
);
mysql_select_db($dbname) or die ("Unable to select");
Im sure the paths and login info are correct.
Any suggestions?
Take out the "#" symbol in front of mysql_connect.
First off, Don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.
<?php
$dbhost = "localhost";
$dbuser = "************";
$dbpass = "***********";
$dbname = "************";
$conn=mysql_connect($dbhost, $dbuser, $dbpass) or die("unable to connect to database.");
mysql_select_db($dbname) or die ("Unable to select");
?>
Then use the $conn variable in your query's
mysql_query("SELECT * from ....",$conn);