mysql_select_db() expects parameter 2 to be resource, object given in /home/u707958028/public_html/Couponn App/db1.php on line 7
This is my db1.php
<?php
$database = "blabla";
$server = "blabla";
$db_user = "blabla";
$db_pass = "blabla";
$link = mysqli_connect($server, $db_user, $db_pass);
mysqli_select_db($database,$link);
?>
You are using mysqli_select_db in a wrong way. The first parameter must be the link, and second one must be the name of your database. So:
mysqli_select_db($link, $database);
As an extention to Ahmad's answer, using mysqli_select_db is unnecessary as the mysqli_connect function allows you to define the database as an additional parameter.
<?php
mysqli_connect("server","username","password","database");
Please try this code..
`<?php`
$database = "blabla";
$server = "blabla";
$db_user = "blabla";
$db_pass = "blabla";
$link = mysqli_connect($server, $db_user, $db_pass);
mysqli_select_db($link,$database);
?>
in mysqli_select_db first parameter must be database resource link and second argument must be database name.
Related
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)
<?php
$phpContent = '<?php
session_start();
include("../conn.inc.php");
$id = $_SESSION["gameid"];
$select_content = mysql_query("select * from game_details where id=".$id);
$arr_content = mysql_fetch_array($select_content);
echo $arr_content["name"];exit;
?>
';
fwrite($phpFile, $phpContent);
fclose($phpFile);
?>
In this code, I am selecting the datas from the database of a particular value of id stored in $id. The code in $phpContent, I am writing it into a file,it shows this error,when I am opening that written file:
Warning: mysql_fetch_array() expects parameter 1 to be resource,
boolean
Can anyone say how to eliminate this error ?
conn.inc.php
<?php
error_reporting(E_ALL ^ E_DEPRECATED);
$db_host = "localhost";
$db_user = "root";
$db_name = "pixo";
$db_pwd = "mysql";
$connection=mysql_connect($db_host, $db_user, $db_pwd);
mysql_select_db($db_name);
?>
Your issues with mysql_select_db you have forgot to pass $connection as second parameter please change your code with below code.
<?php
error_reporting(E_ALL ^ E_DEPRECATED);
$db_host = "localhost";
$db_user = "root";
$db_name = "pixo";
$db_pwd = "mysql";
$connection=mysql_connect($db_host, $db_user, $db_pwd);
mysql_select_db($db_name,$connection);
?>
More About mysql_select_db
But my advice is now days mysql_select_db is not secure please go with MySQLi Functions More About Mysqli
Hope above code will helps you. you forgot pass $connection parameter.
It seems like your mysql requĂȘte is returning FALSE, var_dump your $select_content var to see what it is returning exactly.
Is there a way to connect to your database without having to always type:
$dbHost = "localhost";
$dbUser = "root";
$dbPass = "pass123";
$dbName = "LoginDatabase";
$db = new PDO("mysql:dbname=$dbName;host=$dbHost;port=3306", $dbUser, $dbPass);
Is it possible to have a file that always connects to it?
As suggested in comment,
You need to make a connection.php file and add your connection code into it.
connection.php
$dbHost = "localhost";
$dbUser = "root";
$dbPass = "*****";
$dbName = "LoginDatabase";
$db = new PDO("mysql:dbname=$dbName;host=$dbHost;port=3306", $dbUser, $dbPass);
In this file you have $db object.
in other PHP file, write
include 'connection.php';
In that file you can directly access $db object.
Suggestion: You should also check db connection error in your connection.php file. So if you have any connection error then you can fetch it.
instead of writing this line directly
$db = new PDO("mysql:dbname=$dbName;host=$dbHost;port=3306", $dbUser, $dbPass);
Use below code to check error also.
$dsn = "mysql:host=$dbHost;dbname=$dbName;charset=utf8";
$opt = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
);
$db = new PDO($dsn,$dbUser,$dbPass, $opt);
For more detail, Refer this PDO tag wiki.
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 have this mysqli_connect on a file named config.php :
$DbServer = 'localhost';
$DbUser = 'username';
$DbPassword = 'password';
$DbName = 'dbname';
$con = mysqli_connect($DbServer, $DbUser, $DbPassword, $DbName);
then I have another PHP file named sendingrequest.php :
<?php
session_start();
require_once ('config.php');
$Key = mysqli_real_escape_string ($con, $_GET["key"]);
...
?>
this sendingrequest.php always produce this error message :
Warning: mysqli_real_escape_string(): Couldn't fetch mysqli in /home/public_html/sendingrequest.php on line 4
I tried to modify the script by putting mysqli_connect() on the same file, not by require_once() anymore and surprisingly it works...
<?php
session_start();
//require_once ('config.php');
$DbServer = 'localhost';
$DbUser = 'username';
$DbPassword = 'password';
$DbName = 'dbname';
$con = mysqli_connect($DbServer, $DbUser, $DbPassword, $DbName);
$Key = mysqli_real_escape_string ($con, $_GET["key"]);
...
?>
what's wrong with my require_once? why mysqli_real_escape_string failed to using it? thanks!