Using multiple database on my web page on same host - php

I just referenced this answer and what I preferred was very first solution, now the issue is he has given an information for mysql_() but am using mysqli_(), so using 4th parameter as true, I select the database when user logs in, the moment he logs in he gets redirected to respective page but it is showing that connection was actively refused. any Idea how I can use 2 database, 1 is my default engine database which I need to keep it on for running my framework and second database to run respective scripts according to the user logged in...
What am trying is this
<?php
$database_connect = mysqli_connect('localhost', 'root', '', 'engine');
if(!$database_connect) {
die ('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
}
if(isset($_SESSION['system_id'])) {
$system_database = mysqli_connect('localhost', 'root', '', $_SESSION['system_name'], true);
if(!$system_database) {
die ('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
}
}
?>
P.S I want a procedural way

You don't need any extra parameters, simply do it like this
<?php
$database_connect = mysqli_connect('localhost', 'root', '', 'engine');
if(!$database_connect) {
die ('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
}
if(isset($_SESSION['system_id'])) {
$system_database = mysqli_connect('localhost', 'root', '', $_SESSION['system_name']);
if(!$system_database) {
die ('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
}
}
//Now whatever query you write, just do it like this
$access = mysqli_query($database_connection_var, "/*Query Goes Here*/"); //Will get database name from $database_connection_var
$access2 = mysqli_query($database_connection_var2, "/*Query Goes Here*/"); //Will get database name from $database_connection_var2
?>

Related

mysqli_connect executing for every host input

<?php
/* die/exit operation*/
mysqli_connect('localhost','root','') or die ('The connection is lost');
echo 'connected';
?>
mysqli_connect or die functions working together fine in case of the both correct and incorrect host names.But no matter what username I am using,it is always showing 'connected'.Can anyone please tell me why it is happening?
I don't think your die statement will ever be reached.
mysql_connect is the alias for mysqli::connect and the object will be made.
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
To get a connection failure:
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error);
}

Why do I connect to database no matter what

Newish to php. I have been trying to query a database, and I keep getting the exception thrown that the query could not be completed. I checked to make sure I was connecting to the database, and everything looked fine, until I dug deeper. It appears that the my code tells me that I am connecting to the database regardless of what I put in for a password, username, or even if I do not have this data defined. I don't get it. Originally I had the following code in a function, but I put it no its own page to debug:
<?php
echo'this is working so far <br>';
/*$db = 'fake';
$host = 'localhost';
$password = 'wrong';
$user = 'root';
*/
$result = new mysqli($host, $user, $password, $db);
if(!$result){
echo 'did not connect to database';
throw new Exception('Could not connect to database');
}
else{
echo'connected to database';
return $result;
}
It always tells me I am connected to the database..
Because you are mixing Object oriented style with Procedural style To check database connection
Procedural style
<?php
$link = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db');
if (!$link) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
echo 'Success... ' . mysqli_get_host_info($link) . "\n";
mysqli_close($link);
?>
Object oriented style
<?php
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
?>
Read http://php.net/manual/en/mysqli.construct.php
Note:
OO syntax only: If a connection fails an object is still returned. To check if the connection failed then use either the mysqli_connect_error() function or the mysqli->connect_error property as in the preceding examples.
Source
That means if($result) check is always true no matter what. So no, you don't have that database connection but you are verifying it incorrectly leading you to believe you do.
Your check should be
if($result->connect_error)
// no luck
else
// game on
You should check connect_errno property which stores the error code from last connect call.
$mysqli = new mysqli($host, $user, $password, $db);
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}

php manipulations with mysql

I did connection to the DB with PHP code, everything ok. BUT i can't do any manipulations in DB with PHP code, f.e. add/edit tables.
here is piece of code:
<?php
$mysqli = new mysqli("localhost", "root", "mypass", "mybase");
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error);
}
$mysqli->query("SET NAMES 'utf8'");
// editing existing table
$success = $mysqli->query ("INSERT INTO `myBase`.`users` (`login`, `password`, `reg_date`) VALUES ('aaa', '".md5("123")."', '".time()."')");
echo $success; // this echo does not work
$mysqli->close();
?>
No new user after this edit.
Try debugging with
echo $mysqli->error;
Generally, errors that prevent you from inserting into your table are syntax or user database permissions.

My form is submitting multiple database entries and I don't want it to

I have a simple html form and a php file to execute a database insertion. The problem I am having is that when I press the submit button, my database table receives 3 copies of the same submission and I only need one. Below is the code.
html:
<!DOCTYPE html>
<html>
<form action="demo.php" method="post">
<p>
Input 1: <input type="text" name="input1" />
<input type="submit" value="Submit" />
</p>
</form>
</html>
php:
<?php
define('DB_NAME', 'phpmyadminName');
define('DB_USER', 'phpmyadminUser');
define('DB_PASSWORD', '');
define('DB_HOST', 'localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link){
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected){
die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}
$value = $_POST['input1'];
$sql = "INSERT INTO demo (input1) VALUES ('$values')";
if (!mysql_query($sql)){
die('Error: ' . mysql_error());
}
mysql_close();
?>
The DB_NAME, DB_USER, and DB_PASSWORD have all been changed for obvious reasons, but the code does work.
It just submits too many copies of the form data to the database table. Way back when I was in school, I had this issue, but it seemed like the problem was on the server's end and not our code. The server used here is mine and I do have full control over it. If the server is the issue at fault, I need help correcting that (as I am doing this to learn how to admin these tools, I do not know much more than basic level administration).
Kenneth, the code you have provided here honestly needs some work. First of all, please don't use the mysql API anymore. It's deprecated, will no longer be supported in future PHP versions, and is insecure. For all database operations use the mysqli or PDO API's, preferrably with prepared statements.
Secondly, do not ever INSERT $_POST or $_GET variables directly into the database without validating/sanitizing them first as someone could delete your data or even worse your whole database. PHP has numerous functions to make this very easy such as ctype depending on the data type.
Maybe try something like this in your code:
if (!empty($_POST['input1'])) { //checks if data was received//
$value = $_POST['input1'];
mysql_real_escape_string($value);
$sql = "INSERT INTO demo (input1) VALUES ('$value')";
} else {
echo "form was not received";
exit;
}
I also noticed that your variable names were different, which is corrected above.
EDIT :
Mistakenly used wrong syntax for PHP ctype function.
You are taking the POST input value in the variable named $value and in query you are sending $values
I have corrected the code.
Can you please try the below code
<?php
define('DB_NAME', 'phpmyadminName');
define('DB_USER', 'phpmyadminUser');
define('DB_PASSWORD', '');
define('DB_HOST', 'localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link){
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected){
die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}
$value = $_POST['input1'];
if($value!=''){
$sql = "INSERT INTO demo (input1) VALUES ('".$value."')";
}
if (!mysql_query($sql)){
die('Error: ' . mysql_error());
}
mysql_close();
?>
Below is correct code for the issue. I have checked that when you refresh your page it will create new blank entry in database and also the variable name is wrong.
You have to check for the Request method. This
$_SERVER['REQUEST_METHOD'] === 'POST'
will check the form method and it will prevent the blank entries in database.
<?php
define('DB_NAME', 'test');
define('DB_USER', 'root');
define('DB_PASSWORD', 'mysqldba');
define('DB_HOST', 'localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link){
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected){
die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}
//Test for request method
if($_SERVER['REQUEST_METHOD'] === 'POST') {
$value = $_POST['input1'];
$sql = "INSERT INTO demo (input1) VALUES ('$value')";
//echo $sql;die;
if (!mysql_query($sql)){
die('Error: ' . mysql_error());
}
}
mysql_close();
?>

Cant see data after importing in _post form

Hey i am using Mamp on imac and my problem is that when i hit the submit button (on a post form) to enter the data then nothing shows up and the database remains empty.
Here is my code :
<?php
define('DB_NAME', 'demob');
define('DB_USER','brom');
define('DB_PASSWORD','****');
Define('DB_HOST','localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link){
die('Could not connect : ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected){
die('cant use' . DB.NAME . ' : ' .mysql_error());
}
$value = $_POST['input1'];
$sql = "INSER INTO memberss ('input1') VALUES ('$value')";
mysql_close();
?>
You are not executing a query.
$sql = "INSER INTO memberss ('input1') VALUES ('$value')";
mysql_query($sql);
You should know that, the method you are using to connect to mysql is deprecated now. please read up about PDO or mysqli

Categories