<?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.
Related
I have db.php with the following code.
<?php
$dbHost = "localhost";
$dbUser = "root";
$dbPass = "password";
$dbName = "test";
$databaseLink = new mysqli ($dbHost, $dbUser, $dbPass);
if($databaseLink)
{
mysqli_select_db($databaseLink,$dbName);
}
?>
which I usually import on to other php page like this
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/core/include/db.php";
?>
and works fine. I can start querying using $databaseLink. But there is one page where its not working. But if I explicitly define the connection like this $databaseLink= mysqli_connect("localhost", "root", "password", "test"); it works. There are other php files in the same directory which has no issues.
I have tried
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/core/include/db.php";
global($databaseLink);
?>
But that does not seem to work too. I have looked it up online for examples but can find any help.
you forget to require your db.php file
require_once($path);
You can simply use include at the top of your php code to link in another php page.
include '/core/include/db.php';
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)
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.
I'm getting the error: No database selected
Here is the code:
<?php
$host = "localhost";
$user = "root";
$password = "";
$database_name = "Student";
mysql_connect($host, $user, $password);
mysql_select_db($database_name);
?>
This code is for php-connect.php class and for the form:
<!DOCTYPE html>
<html>
<body>
<?php
//load database connection
include("php-connect.php");
$id = "1";
$name = "Elena";
$city = "Lahore";
//Command to insert into table
$query = "INSERT INTO studentdata (id,name,city) VALUES ('$id','$name','$city')";
//run the query to insert the person.
$result = mysql_query($query) OR die(mysql_error());
//let them know the person has been added.
echo "Data successfully inserted into the database table ... ";
?>
</body>
</html>
This is the code for the form.. I've tried a lot of things to fix this error but it does not work. Is there any problem with my database?
Try this...
<?php
$host = "localhost";
$user = "root";
$password = "";
$database_name = "Student";
$mLink = mysql_connect($host, $user, $password) or die(mysql_error());
mysql_select_db($database_name , $mLink);
another file
<?php
require 'php-connect.php';
//...... etc..
but, read this first:
This extension is deprecated as of PHP 5.5.0, and will be removed in
the future. 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_select_db()
PDO::__construct() (part of dsn)
http://br.php.net/mysql_select_db
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!