mysqli_select_db wrong database - php

I'm trying to move a web page from one sever to another which I'm not the author (some kind of custom cms).
My problem with it is that I prints only the message Wrong database. I did check the login data with login through phpmyadmin and they work.
On the old server there was a version of php 5.4 and on the new one I have the 7.0 version
<?php
$DB->host = 'localhost';
$DB->name = 'db-name';
$DB->pass = 'pass';
$DB->login = 'db-user';
$DB->prefix = '';
$System->files = '';
$System->root = '';
$System->lang['pl'] = 'Polski';
$System->lang['de'] = 'Deutsch';
$System->lang['en'] = 'English';
//$System->lang['ru'] = 'русский';
$System->component->articles = true;
$System->component->references = true;
$System->component->contakt = true;
$link = mysqli_connect($DB->host,$DB->login,$DB->pass) or die ("Can't connect: " . mysqli_error());
mysqli_select_db($DB->name, $link) or die ("Wrong database: " . mysqli_error());
mysqli_query("SET NAMES 'utf8'");
unset($link,$DB->name,$DB->login,$DB->pass);
?>

When using procedural style mysqli_select_db the first parameter is the link identifier then comes the database name
mysqli_select_db( $link, $DB->name );
See the documentation.
Worth mentioning you have also other issues in your code: see Barmar's comments.
I suggest you check the manual for every mysqli function you're using; you should be able to fix things easily.

Related

Open DB Connection within PHP Function

I wrote an addon module for our WHMCS billing system a long time ago that we recently realized was causing some issues. Essentially each module's PHP file is loaded regardless if it is actually used or not, where this is how their "hook" system is setup.
When I wrote the module, I included my "db_config.php" file at the top in the global space, which I now realize is causing this database to load every page and is apparently being written to when it shouldn't be. As this is the case, I would like to open the Database connection at the top of the function and close it at the end of the function.
I've never seen this done before nor can I find much information on it. The contents of my db_config.php appear as follows and I am wondering if I can just include_once() inside of the function?
<?php
// Connection's Parameters
$hostname = "xxx.xxx.xxx.xxx";
$database = "database";
$username = "username";
$password = "password";
// Connection
$tca_conn = mysql_connect($hostname, $username, $password);
if(!$tca_conn)
{
die('Cannot Establish Connection to Database : ' . mysql_error());
}
$tca_db = mysql_select_db($database, $tca_conn);
if (!$tca_db)
{
die ('Cannot Select Database : ' . mysql_error());
}
?>
Try this one.It might work for you.
$tca_db = mysql_select_db($database);
instead of
$tca_db = mysql_select_db($database, $tca_conn);

php local connection mysql database

I try to make a simple IOS app that can connect to mysql database and read one table. But my php code does't work and really have no idea why, it's seems correct to me. The database is in a raspberry phpmyadmin server and the server works great.
I will put my code here and please tell me what's wrong.
<?php
$host = "192.168.2.193";
$db = "produtos";
$user = "root";
$pass = "1234";
$connection = mysql_connect($host, $user, $pass);
if(!$connection)
{
die("Database server connection failed.");
}
else
{
//attempt to select the database
$dbconnect = mysql_select_db($db, $connection);
//check to see if we could select the database
if(!dbconnect)
{
die("Unable to connect to the specified database!");
}
else
{
$query = "SELECT * FROM produtos";
$resultset = mysql_query($query, $connection);
$records = array();
//loop throught all our records and add them to our array
while ($r = mysql_fetch_assoc($resultset))
{
$records[] = $r;
}
echo json_ecode($records);
echo $resultset;
}
}
?>
Based on the question:
use mysqli_connect rather than mysql_connect because mysql_connect is deprecated and will not work someday. Also what is the the error you are getting? change your die() statement to something more helpful die(mysqli_error($connection));
Based on your comment:
That error would suggest that you either A) don't have the right IP address or B) there is a network issue between your host server and the SQL server, is this code running on the same server that is hosting the SQL database? if so then you can probably just use localhost for your $host

Trying to connect database MySQL from PHP (Not Successful)

I'm following a tutorial on the net on PHP and MySQL.
I'm using Linux. I'm trying to establish a connection to a database but it is not working.
I have my database:
create database test;
create table user(name text, pass text);
insert into user values('john', '123');
and then my php:
<?php
$_host = "localhost";
$_dbuser = "root"
$_dbpass = "";
$_dbname = "test";
#mysql_connect("$_host", "$_dbuser", "$_dbpass") or die("could not connect");
#mysql_select_db("$_dbname") or die("no database");
echo "connection stablished";
?>
And the output of my file is just a blank tab on the browser.
What should I do to solve this? What am I doing wrong?
Thank you in advance. I'm very new to web programming.
$_host = "localhost";
$_dbuser = "root"
$_dbpass = "";
$_dbname = "test";
$link = mysqli_connect($_host, $_dbuser, $_dbpass, $_dbname);
if (!$link) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
echo "connection stablished";
Now...
Use mysqli_* functions as mysql_* are deprecated.
You will need $link variable later for queries.
As you're newbie read this: How can I prevent SQL injection in PHP?
Prepared statements FTW! Remember!

WebMatrix PHP mySQL

I am using WebMatrix Beta 3 which has support for php 5.2 and 5.3 I am able to run php pages but when I am trying to connect to mySql DB its not working.
Can anyone please suggest me the right way of doing it.
The connection code is written in a file called dbinfo.php which resides under config folder
<?php
$hostname = '127.0.0.1';
$username = 'root';
$password = 'password';
$database = 'test';
$link = mysql_connect($hostname, $username, $password)
or die("Could not connect : " . mysql_error());
mysql_select_db($database) or die("Could not select database");
//Below function added to allow customized unescaping.
function mysql_unescape($sRet_VAL=""){
$sRet_VAL = str_replace('\"','"',$sRet_VAL);
$sRet_VAL = str_replace("\'","'",$sRet_VAL);
return $sRet_VAL;
}
?>
and I am using this file as follows
<?php
require_once( $_SERVER['DOCUMENT_ROOT'] . '/config/dbinfo.php');
?>
<?php
$query = "SELECT * from temp";
$result = mysql_query($query)
or die("Error: " . mysql_error());
?>
it worked seems like webmatrix do not recognize I replaced it wilt <>php echo $varData ?> and it worked.

Database code is not connecting in chat program using php

<?php
// MySQL database connection file
$SERVER = "127.0.0.1"; // MySQL SERVER
$USER = "root"; // MySQL USER
$PASSWORD = "admin"; // MySQL PASSWORD
$link = #mysql_connect($SERVER,$USER,$PASSWORD);
$db = mysql_select_db("website");
?>
This is a database connecting code for chat program but it not connecting,Can any one pls help me to correct this code?
Drop the # infront of mysql_connect, it's used to suppress error which you don't want.
Also you need to check the return value of mysql_connect which is there in $link and make sure that it is not false before you proceed and to a DB select. Calling the function mysql_error when an error occurs gives you the reason for the error.
$link = mysql_connect($SERVER,$USER,$PASSWORD);
if (!$link) {
die('Could not connect: ' . mysql_error());
}

Categories