Including a database connection from a separate file - php

I currently have a db file I include to establish a DB connection:
db.php:
$dbs = array(
"127.0.0.1",
"127.0.0.3",
"127.0.0.2",
);
if(!$con){
$con = mysql_connect($dbs[rand(0,2)],"user_login","password") or die('Could not connect: ' . mysql_error());
}
However when I try to include it sometimes and do a query, it returns a blank result:
include("db.php");
mysql_select_db("database_table", $con);
$result = mysql_query("SELECT * FROM users where email='admin#test.com'");
while($row = mysql_fetch_array($result)){$id = $row['email'];break;}
The weird thing is, if I modify the initial db.php to connect like this, it works (non randomizing it):
db.php:
if(!$con){
$con = mysql_connect("127.0.0.1","user_login","password") or die('Could not connect: ' . mysql_error());
}
Is there any explanation in this? Is there a difference in how they are connecting?
Thanks

First I would turn on error checking.
You can do that by adding the following to the very top of your php file:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
or the long way by changing it globally: display_errors = On in your php.ini
This will output what's wrong. If you can't figure it out be then try the following:
Then check if you could connect to all databases like you did with
if(!$con){
$con = mysql_connect("127.0.0.1","user_login","password") or die('Could not connect: ' . mysql_error());
}
Then I would check if they all had the same tables.
If that's correct I would check the output of your:
if(!$con){
$con = mysql_connect($dbs[rand(0,2)],"user_login","password") or die('Could not connect: ' . mysql_error());
}
Does it really work the way it's intended?

Related

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();
?>

how to get php connect to mysql?

i've tried everything to make my site connect to the database but i always get his error: Could not connect to Master Database
i have 2 files
define('DBHOST','localhost');
define('DBUSER','root');
define('DBPASS','root');
define('DBNAME','test');
define('dbslave','test');
define('dbsiteid','1');
define('dbprefix','_blog');
and connect.php
error_reporting(0);
$connect = mysql_connect("$DBHOST", "$DBUSER", "$DBPASS");
if ( ! $connect) {
die('Could not connect to Database server');
}
$siteid = "$dbsiteid";
$prefix = "$dbprefix";
$dbmast = "$DBNAME";
$dbslave = "$dbslave";
$cmast = mysql_select_db("$DBNAME");
if ( ! $cmast) {
die('Could not connect to Master Database');
}
$cslave = mysql_select_db("$dbslave");
if ( ! $cslave) {
die('Could not connect to Slave Database');
}
how do i solve this error with connection or what i did wrong ?
You do not put constants in quotes, they do not start with $, and by convention are all uppercase.
define('DBHOST','localhost');
define('DBUSER','root');
define('DBPASS','root');
define('DBNAME','test');
define('DBSLAVE','test');
define('DBSITEID','1');
define('DBPREFIX','_blog');
$connect = mysql_connect(DBHOST, DBUSER, DBPASS);
if (!$connect) {die('Could not connect to Database server');}
$cmast = mysql_select_db(DBNAME);
if (!$cmast) {die('Could not connect to Master Database');}
$cslave = mysql_select_db(DBSLAVE);
if (!$cslave) {die('Could not connect to Slave Database');}
Also, defining constants only to assign them to variables is silly and a waste of resources. And don't turn off error reporting when developing as it hides your errors. You want to do the opposite and them them on.

How to write mysql php errors to a text file

I have the following PHP code that creates a mysql connection:
$link = mysqli_connect("$mysql_server", "$mysql_user", "$mysql_pw", "$mysql_db");
if (!$link) {
die('Could not connect: ' . mysql_error());
}
In the event the connection is not made, how can I write the error to a text file on my server?
Yes it is possible with error_log function in php
$con = mysqli_connect("$mysql_server", "$mysql_user", "$mysql_pw", "$mysql_db");
if (!$con)
{
error_log(mysqli_error($con) . "\n", 3, "/var/tmp/my-errors.log");
}

Connecting to a mysql Database via PHP?

Ok, Ive been playing around with PHP and databases. Note, Im doing all my work on a webserver hosted by bluehost.
So I went into the cpanel and set up a database called gagalugc_stocks.
I also setup a username (test) and a password (password) and added them to be able to control the databse. Below is my script in an attempt to connect to it, not it is also on the server.
<?php
$connect = mysql_connect('localhost','test','password');
if(!$connect){die('Could not reach database!');}
mysql_select_db("gagalugc_stocks", $connect)
?>
-Thanks
NOTE: The problem is that it can never reach the database.
<?php
$connection = mysql_connect ('localhost', 'test', 'password')
or die ('<b>Could not connect: </b>' . mysql_error());
$result = mysql_select_db("gagalugc_stocks")
or die ('<b>Could not connect: </b>' . mysql_error());
?>
Didn't check for port when adding database make sure to specify host and port
(localhost:2083)
How about
<?php
$connect = mysql_connect('localhost:2083','test','password');
if(!$connect){die('Could not reach database!');}
mysql_select_db("gagalugc_stocks", $connect)
?>
For local system:-
$con = mysql_connect("localhost","root","");
if (!$con){
die('Unable to connect to the server ' . mysql_error());
}
mysql_select_db("databasename", $con) or die(mysql_error());
For website or remote server:-
$con = mysql_connect("localhost","username","password");
if (!$con){
die('Unable to connect to the server ' . mysql_error());
}
mysql_select_db("databasename", $con) or die(mysql_error());
for more please visit : Algosoftwares

php mysql problem

I have installed php and mysql on windows xp with iis 5.1 as my web server. But when i run the following code it just shows me a blank page. Can anybody let me know where i am wrong. Thanks in advance.
<?php
$con = mysql_connect("localhost","root","xxxxxx");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
?>
Well, it doesn't generate any output if the connection succeeds so that's probably what's happening. What would you expect?
<?php
$con = mysql_connect('localhost', 'root', 'xxxx') or die(mysql_error());
?>
what does this print ?
* make sure you have mysql running
* make sure you have php running on IIS
* try to check in php.ini:
error_reporting = E_ALL
display_errors = On
Use the following code
$con = mysql_connect("localhost","root","xxxxxx");
if (!$con)
{
die('Could not connect: ' . mysql_error());
} else {
die('Connection ok!');
}

Categories