Infinite Loading When Trying to Connect Into MySQL - mysql

I'm having a problem connecting to mysql database in XAMPP. It always takes time to load this segment of php code. What might be the problem?
<?php
session_start();
//redirect function
function returnheader($location){
$returnheader = header("location: $location");
return $returnheader;
}
$connection = mysqli_connect("localhost:85","root","") OR die(mysqli_error());
$db_select = mysqli_select_db("pts",$connection) OR die(mysqli_error());
$errors = array();
if(isset($_POST["iebugaround"])){
//lets fetch posted details
$uname = trim(htmlentities($_POST['uname']));
$passw = trim(htmlentities($_POST['psw']));
//check username is present
if(empty($uname)){
//let echo error message
$errors[] = "Please input a username";
}
//check password was present
if(empty($passw)){
//let echo error message
$errors[] = "Please input a password";
}
if(!$errors){
//encrypt the password
$passw = sha1($passw);
$salt = md5("userlogin");
$pepper = "ptsbtr";
$passencrypt = $salt . $passw . $pepper;
//find out if user and password are present
$query = "SELECT * FROM users WHERE username='".mysqli_real_escape_string($uname)."' AND password='".mysqli_real_escape_string($passencrypt)."'";
$result = mysqli_query($query) OR die(mysqli_error());
$result_num = mysqli_num_rows($result);
if($result_num > 0){
while($row = mysqli_fetch_array($result)){
$idsess = stripslashes($row["id"]);
$firstnamesess = stripslashes($row["firstname"]);
$username = stripslashes($row["username"]);
$_SESSION["SESS_USERID"] = $idsess;
$_SESSION["SESS_USERFIRSTNAME"] = $firstnamesess;
$_SESSION["SESS_USERNAME"] = $username;
setcookie("userloggedin", $username);
setcookie("userloggedin", $username, time()+43200); // expires in 1 hour
//success lets login to page
returnheader("users-area.php");
}
} else {
//tell there is no username etc
$errors[] = "Your username or password are incorrect";
}
}
} else {
$uname = "";
}
?>
And this is the error after loading it for minutes.
Warning: mysql_connect(): MySQL server has gone away in C:\xampp\htdocs\peopletrackingsystem\login.php on line 10
Warning: mysql_connect(): Error while reading greeting packet. PID=6940 in C:\xampp\htdocs\peopletrackingsystem\login.php on line 10
Warning: mysql_connect(): MySQL server has gone away in C:\xampp\htdocs\peopletrackingsystem\login.php on line 10
Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\peopletrackingsystem\login.php on line 10
(backstory: my Apache is also having a problem with localhost/127.0.0.1. It only appears blank page every time I try to access it with just localhost. So I always put the port every time I try to access it. I already removed all unnecessary port in hosts file. I already change the listen port of the httpd.conf and the Server name too with the port in it)

You can't add the port in "localhost:85", try it like this:
$connection = mysqli_connect("localhost","root","") OR die(mysqli_error());

Related

Database login incorrect

I'm having some trouble with some software that I'm tweaking. During my install script I can type my database username, database name, localhost, etc and once I click next it is suppose to connect to the database and start the install. However when I click next I have shown this error:
Database Login Incorrect: config.php
Here is my config.php file:
<?php
// Mysql Settings
$mysqli_host = "";
$mysqli_login = "";
$mysqli_pass = "";
$mysqli_database = "";
$db_prefix = "";
$_dbConn = mysqli_connect($mysqli_host, $mysqli_login, $mysqli_pass)
or die ('Database Login Incorrect: config.php');
mysqli_select_db($mysqli_database, $_dbConn)
or die ('Unable to select the database: config.php');
?>
Although the database name and username are correct and have proper permissions. I can show the install file but it is over 2k lines and didn't want to post all of that :p
Thanks for your time!
Edit:
After using Geo V L's fix, I now have the error of "have you run install.php yet?". This is located in my structure.php file:
// Include functions...
include "scripts/php/functions.php";
// get board theme
$query2 = "select THEME from {$db_prefix}settings";
$result2 = mysqli_query($query2) or die("Have you run install.php yet?");
$theme = mysqli_result($result2, 0);
In this code snippent $_dbConn will be the first parameter for mysqli_select_db
<?php
// Mysql Settings
$mysqli_host = "";
$mysqli_login = "";
$mysqli_pass = "";
$mysqli_database = "";
$db_prefix = "";
$_dbConn = mysqli_connect($mysqli_host, $mysqli_login, $mysqli_pass)
or die ('Database Login Incorrect: config.php');
mysqli_select_db( $_dbConn,$mysqli_database)
or die ('Unable to select the database: config.php');
?>
Or you can directly add databasename on mysqli connect string like the following
<?php
// Create connection
$conn = mysqli_connect($mysqli_host, $mysqli_login, $mysqli_pass, $mysqli_database);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>

Qt isOpen method false value MySQL

I have a MySQL database, which i created in Workbench. I can easily connect to it using php code:
<?php
$host = "localhost";
$port = 3306;
$socket = "";
$user = "hate";
$password = "88005553535";
$dbname = "galleries_db";
$con = new mysqli($host, $user, $password, $dbname, $port, $socket)
or die ('Could not connect to the database server' . mysqli_connect_error());
echo "DONE!";
?>
But when I'm trying to connect to my DB with Qt like this:
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setPort(3306);
db.setDatabaseName("galleries_db");
db.setUserName("hate");
db.setPassword("88005553535");
db.open();
if(!db.isOpen()) {
ui->label->setText("-");
qDebug() << "Error: "<< db.lastError() << " " << db.lastError().text();
}
else
ui->label->setText("+");
I always get false value returned by isOpen method. And in stream I have:
Error: QSqlError("", "", "") " "
May be problem in MySQL driver for Qt ? Or there is something else ?
Will appriciate any help or advice.
There should be:
if(db.isOpen())
Not:
if(!db.isOpen())
Actually it is very silly mistake. isOpen always returned me true value, and I made it false by myself.

Unable to Change Password Correctly

As much as I've tried I can't see why this code wouldn't work. Whenever I atmempt to change a password using this page, it says an incorrect password has been supplied.
This is the last page I have to get to work for this site, so any help would be appreciated.
<?php
require_once ("dbconnect.php");
// include file to do db connect
require_once ('checklog.php');
require_once ("functions.php");
session_start();
$username = ($_POST['username']);
$password = ($_POST['password']);
$newpassword = ($_POST['newpassword']);
$repeatpassword = ($_POST['repeatpassword']);
if (isset($_POST['submit'])) {
if ($username && $password) {
$hashpass = salt($password);
$query = "SELECT ID FROM users WHERE username='$username' AND password='$hashpass'";
if ($username == $datausername && salt($password) == $datapassword) {
// PASSWORD CHANGING IS DONE HERE
if ($newpassword == $repeatpassword) {
// From register
if (strlen($newpassword) > 25 || strlen($newpassword) < 6) {
$message = "Password must be 6-25 characters long";
}
else {
// part 8
// Process details here
if ($db_server) {
die("Unable to connect to MySQL: " . mysqli_connect_error());
$db_status = "not connected";
}
else {
if ($db_server) {
// clean the input now that we have a db connection
$newpassword = clean_string($db_server, $newpassword);
$repeatpassword = clean_string($db_server, $repeatpassword);
mysqli_select_db($db_server, $db_database);
$result = mysqli_query($db_server, $query);
if ($row = mysqli_fetch_array($result)) {
$message = "This is your current password. Please try again.";
}
else {
// Process further here
$newpassword = salt($newpassword);
$query = "UPDATE INTO users (password) VALUES ('$password')";
mysqli_query($db_server, $query) or die("Insert failed. " . mysqli_error($db_server));
$message = "<h1>Your password has been changed!</h1>";
}
mysqli_free_result($result);
}
else {
$message = "Error: could not connect to the database.";
}
mysqli_close($db_server);
// include file to do db close
}
}
// This code appears if passwords dont match
}
else {
$message = "<h1>Your new passwords do not match! Try again.</h1>";
}
}
else {
$message = "<h1>Incorrect password!</h1>";
}
}
else {
$message = "<h1>That user does not exist!</h1>" . "Please <a href='password.php'>try again</a>";
}
// Close connection!
}
else {
$message = "<h1>Please enter a valid username/password</h1>";
}
?>
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<?php
include_once ("header.php");
?>
<div id="apDiv1"><span class="rich-list">
<title>Change your Password</title>
<h1>So you want to change your password, <?php
echo $_SESSION['username'];
?>?</h1>
<form action='password.php' method='POST'>
<div align="right">Current Username:
<input type='text' name='username'><br />
Current Password: <input type='password' name='password'><br />
New Password: <input type='password' name='newpassword'><br />
Repeat New Password: <input type='password' name='repeatpassword'><br />
<input type='submit' name='submit' value='Confirm'>
<input name='reset' type='reset' value='Reset'>
</div>
</form>
<?php
echo $message
?>
<?php
include_once ("footer.php");
?>
</div>
</body>
</html>
</body>
</html>
I don't know how the 'salt' function is defined - you might want to include it in your post.
I suspect that salt will pick some random salt, and hash with that - if so, to verify, you will need to use the salt from the encrypted password, or you will get a different outcome.
So, to verify a password:
get the username and guess,
get the encrypted password for the username
get the salt from the encrypted password (often the first few characters)
encrypt the guess with the salt from the encrypted password
compare this encrypted guess to the encrypted password.
Ignoring the many bad practices in your code for now, the problem is that you never retrieve the user's password from the database:
// $datausername and $datapassword have not been set, so this will always be false
if ($username == $datausername && salt($password) == $datapassword) {
// ... code
} else {
$message = "<h1>Incorrect password!</h1>";
}
You did set the $query string, but you forgot to run the actual query in the database and retrieve the results, if any.
To run a query, first you establish a database connection:
$dbc = new mysqli("db_host", "db_user", "db_password", "db_name");
if (mysqli_connect_errno()) {
die("Could not connect to MySQL: " . mysqli_connect_error());
}
Let's assume you did that in your "dbconnect.php" file, so now you have a valid $dbc connection and your $query string. The following will give you a mysqli_result object, or FALSE if the query fails.
$result = $dbc->query($query);
Please see the PHP manual for more information on how to use that object.
Also, PHP has an excellent built-in function called password_hash - I suggest you use this instead of what appears to be a custom-defined function 'salt'.
To further improve your code, you should learn about SQL Injection and how to avoid it, such as by using prepared statements.

Getting 2 Notice: Undefined Variable errors

The two errors are as below:
Notice: Undefined variable: HawA_Homes in C:\wamp\www\HawA_CIS241\InsertRecord.php on line 48
Notice: Undefined variable: HawA_Homes in C:\wamp\www\HawA_CIS241\InsertRecord.php on line 56
I've checked my names and they appear correct and I am not sure how to proceed now.
Code is as below:
<?php
$hostName = "localhost";
$databaseName = "test";
$userName = "root";
$password = "";
$tableName = "HawA_Homes";
//try to connect report error if cannot
$db = new mysqli($hostName, $userName, $password, $databaseName) or die(" Could not connect:" . mysql_error());
print(" Connection successful to host $hostName <br /> <br />"); //report connection success
//Get data to create a new record
$Address = $_REQUEST["address"];
$DateBuilt = $_REQUEST["dateBuilt"];
$Value = $_REQUEST["value"];
$Size = $_REQUEST["size"];
$Number_of_floors = $_REQUEST["floors"];
$sql = "INSERT INTO $HawA_Homes('Address','DateBuilt','Value','Size','Number_of_floors')VALUES{'$Address','$DateBuilt','$Value','$Size','$Number_of_floors')"; //Create insert query for new record
//try to query dataase / store returned results and report error if not successful
if(!$result =$db->query($sql))
{
//die('There was an error running the query[' .$db->error . ']';
}
print("SQL query $sql successful to database: $HawA_Homes <br /><br />"); //report sql query successful.
?>
You have these notices because the variable $HawA_Homes isn't declared in your code before being used at line 48 and 56. (These are just notices, they are not critical errors, you can avoid displaying them by adding error_reporting(E_ALL & ~E_NOTICE); at the begining of your code, like explained here)
In fact, you used $HawA_Homes instead of $tableName in these lines. Replace them, you won't have notices anymore for these lines.

undefined variable using PDO [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”
I am new to PDO, and i am currently trying to convert all of my mysql_query's to PDO-> and I keep getting an error of an undefined variable $db
here is my code for the Database connection page:
$host = "localhost";
$user = "root";
$password = "";
$dbname = "XXXX";
global $db;
$db = new PDO("mysql:host=$host;dbname=$dbname;charset=UTF8", $user, $password);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
Edit: my register function that is having problems
<?php
###########################
# #
# Database Authentication #
# #
###########################
require('Database.php');
require('Bcrypt.php');
require('Session.php');
function register($username, $password, $email){
if($username != null && $email != null && $password != null){
/*
$check = $database->prepare("SELECT * FROM users WHERE Username = '$username' OR Email = '$email'");
$sql = $check->execute();
if(count($check) > 0){
echo "The username or email address you entered is already in use, please try another combination";
}
I currently have this commented out to test the INSERT query below
*/
if(true){
$salt = create_salt($username);
$password = hash_pass($password, $salt);
$query = $db->prepare("INSERT INTO users (Username, Password, Email) VALUES('$username', '$password', '$email')");
$query->execute();
return true;
}
else{
echo "Something went wrong with inserting into table";
return false;
}
}
else{
echo "Please fill in all of the information in order to register";
return false;
}
}
When running this i get
Notice: Undefined variable: db in ...\Authentication.php on line 33
You need to move the global $db; statement from where it is into the function scope.
See "Example #1 Using global" here.
A quick fix would be to pass your $db into the function register so it will be available inside this functions scope. Make sure you drop the global keyword from your $db variable first, and you will need to update any other scripts using the function register() and pass in the $db variable:
function register($username, $password, $email, $db){
//now you will have $db available inside this scope
Hope this helps, however as a sidenote this is rather clunky and you should probably focus on setting up your application to use classes instead.
You seem to be declaring $db as global before it has been declared.