Can't connect to database after PHP 7.2.4 upgrade - php

We had PHP v5.5 installed on our webserver, but when I upgraded to v7.2.4 I can't connect to my database. All my other PHP pages work except when I have to talk to the database. We're on a windows 2008 r2 standard server running iis 7.
Here's a snippet of the code where I connect to the database.
<?php
session_start();
//open connection
$conn=odbc_connect("DATABASE", '','', SQL_CUR_USE_ODBC);
//get all possible login info
$query = "SELECT * FROM TABLE";
$rs = odbc_exec($conn, $query);
//get user's login info from login.php
$username = $_POST['username'];
$password = $_POST['userpassword'];
$username = stripslashes($username);
$password = stripslashes($password);
$result = 0;
while (odbc_fetch_row($rs)) {
$name = odbc_result($rs,"username");
$pass = odbc_result($rs,"userpassword");
if($username == $name && $password == $pass)
$result = $result + 1;
}
I double checked with the ini file from the previous version and I have the same extensions enabled. I went through it line by line. The code I'm using was already in place when I took the job when I have time I'm going to go through and clean it up. There is nothing in my error log.

Related

Establishing PHP connection with localhost server?

I am having an issue with connecting my PHP script to the database on my localhost server.
I have posted the code below, it is to enable user registration on the site.
The input boxes appear as they should when I run the code, but nothing updates to the database when I try and complete a sign up.
As a novice with PHP I don't know enough about it to spot any errors I might be making, or what they mean.
Any help on this subject would be appreciated as there is a lot of info about PHP online, but I would rather know what was causing this error in order to prevent it in the future.
Here are the errors appearing in the browser console:
Failed to load resource: the server responded with a status of 404 (Not Found)
ReferenceError: Can't find variable: $
And the UNIX socket code from MAMP (I don't know where this would fit in):
$user = 'root';
$password = 'root';
$db = 'inventory';
$socket = 'localhost:/Applications/MAMP/tmp/mysql/mysql.sock';
$link = mysql_connect(
$socket,
$user,
$password
);
$db_selected = mysql_select_db(
$db,
$link
);
And the PHP code:
//connect to database
$db = mysql_connect("localhost", "root", "", "authentication");
if (isset($_POST['register_btn'])) {
session_start();
$username =mysql_real_escape_string($_post['username']);
$email =mysql_real_escape_string($_post['email']);
$password =mysql_real_escape_string($_post['password']);
$password2 =mysql_real_escape_string($_post['password2']);
if ($password == $password2) {
//create user
$password = md5($password); //hash password before storing for security
$sql = "INSERT INTO users(username, email, password) VALUES('$username', '$email', '$password')";
mysql_query($db, $sql);
$_SESSION['message'] = "Find a Table";
$_SESSION['username'] = $username;
header("location: HomePage.html"); //redirect to homepage
}else{
$_SESSION['message'] = "Your passwords must match to proceed";
}
}
?>
Where to start? So many problems.
First off, you are using the OLD mysql functions which have been removed entirely in recent versions of PHP. Use the mysqli functions instead. The old functions like mysql_connect and mysql_query have been deprecated. You need to look for all occurrences of mysql_ in this code and think about replacing each command with its new counterpart.
You define this code to connect:
$user = 'root';
$password = 'root';
$db = 'inventory';
$socket = 'localhost:/Applications/MAMP/tmp/mysql/mysql.sock';
$link = mysql_connect(
$socket,
$user,
$password
);
$db_selected = mysql_select_db(
$db,
$link
);
and then you don't use the resulting connection -- even check if it worked. You should always check the value returned by mysqli_connect to see if it actually worked or if it returned FALSE. You reconnect again and don't bother checking to see if it worked:
//connect to database
$db = mysql_connect("localhost", "root", "", "authentication");
And in doing so, you redefine $db to something else.
Also, you run a query without checking whether it succeeded or not:
mysql_query($db, $sql);
$_SESSION['message'] = "Find a Table";
$_SESSION['username'] = $username;
header("location: HomePage.html"); //redirect to homepage
You should be checking the result of mysqli_query (not mysql_query as you have in your code) to see what it returned. It should be TRUE if the INSERT query worked.
And after you redirect, you fail to call exit, which means that all the code that follows your redirect attempt may end up actually executing anyway.

Openshift mysql write access from php

I have an issue with my mysql database that I can't resolve : I have a scalable application with mysql and php. I can access the database but I can only read ('SELECT') the data but I don't seem to have the permission to write ('INSERT INTO...').
Here is the php file for SELECT command which is working :
<?php
$cnx = mysql_connect("domain:port","username","password");
$db= mysql_select_db( "application_name" );
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT * FROM `User` where `userName`='$username' AND `password`='$password'";
$requete = mysql_query($sql ,$cnx);
if(mysql_num_fields($requete)==0){
echo "0";
}
else {
$result = mysql_fetch_assoc( $requete );
echo $result["userKey"];
}?>
And the php file for INSERT INTO command which is not working :
<?php
$con=mysql_connect("domain:port","username","password","application_name");
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
$sql="INSERT INTO User(userName,emailAdress,password) VALUES ('$username','$email','$password')";
if (mysqli_query($con,$sql))
{
echo "0";
}
?>
Thanks for your help !
Install PHPMyAdmin, and then create a new schema. You should be able to carry out all usual executions into that schema you created.
Alternatively, SSH into your gear, type 'mysql'. This will give you access to the database. Create a schema from there and then in your PHP script, when you connect to the db, specify the schema as the one you created.

Inserting data into a remote database

I'm trying to make a web/login page, where if a user is in certain location then they can be able to insert data in the database based on their location.
For example, if we have two users bob and max, one in A(bob) and the other in B(max).
If bob logs into the system username... bob password... 123 location.. A, he should be able to to insert his data on the database located on his PC same as max but they should use a single application.
Now my question is how can I achieve this. For example I've a login script below which is communicating with my localhost (A) and i also want to include a remote connection to my other PC on the LAN (B), using Mysql database installed on both computers, I can connect to Mysql database using Mysql workbench but how can I do it using a script in PHP?
login page
<?php
if(isset($_POST['login'])){
include 'includes/config.php';
$uname = $_POST['uname'];
$pass = $_POST['pass'];
$query = "SELECT * FROM admin WHERE uname = '$uname' AND pass = '$pass'";
$rs = $conn->query($query);
$num = $rs->num_rows;
$rows = $rs->fetch_assoc();
if($num > 0){
session_start();
$_SESSION['uname'] = $rows['uname'];
$_SESSION['pass'] = $rows['pass'];
echo "<script type = \"text/javascript\">
alert(\"Login Successful.................\");
window.location = (\"admin/index.php\")
</script>";
} else{
echo "<script type = \"text/javascript\">
alert(\"Login Failed. Try Again................\");
window.location = (\"login.php\")
</script>";
}
}
?>
connection.php
<?php
$host = "localhost";
$user = "root";
$pass = "";
$db = "cars";
$conn = new mysqli($host, $user, $pass, $db);
if($conn->connect_error){
echo "Failed:" . $conn->connect_error;
}
?>
Configuring two connection scripts is what i have in mind, but i don't
think it will work.
Any suggestions would be appreciated.
Thanks

PHP Login script receive blank screen in Hostinger

I have a confusing problem in login form, it seems work well when i try it in localhost but it gives a blank white screen when i upload it to hostinger. I think its not about the database connection. Please help me, because it doesn't give me an error reporting. Here's php code, the form and inputs name does match.
<?
$host = "localhost";
$user = "HIDDEN";
$pass = "HIDDEN";
$db = "connect_11265";
$koneksi=mysql_connect($host,$user,$pass) or die ("koneksi ke database gagal") ;
$koneksi_database= mysql_select_db($db,$koneksi);
if(ISSET($_POST['username'])){
$username = $_POST['username'];
$password = $_POST['password'];
$cek = mysql_query("SELECT * FROM login WHERE username='$username' AND password='$password'");
if(mysql_num_rows($cek)==1){//jika berhasil akan bernilai 1
$c = mysql_fetch_array($cek);
session_start();
$_SESSION['username'] = $c['username'];
$_SESSION['level'] = $c['level'];
if($c['level']=="administrator"){
header('location:admin/index.php?page=dashboard');
}elseif($c['level']="operator"){
header('location:admin/index.php?page=dashboard');
}
}
}
?>
I Assume, in hostinger not enabled the shortag options <? ?>, Can you try using <?php ?>.
BTW,
elseif($c['level']=="operator")
.....^
instead of
elseif($c['level']="operator")
Header is not working on Hostinger ....
Localhost for a mysql server is mysql.hostinger.hu

Registration script.php does not work

I have a registration script that doesn't work. It used to work but then suddenly it stopped working. I dont know what I've done or what happend. I have tried for like an hour now to find out where the problem is, but I can't seem to find it.
What happens? When I click register I don't get any errors but it doesn't upload to the database:
When I type: $res = mysql_query($query) or die ("ERROR"); it displays ERROR so I think it's related to that code:
//=============Configuring Server and Database=======
$host = 'localhost';
$user = 'root';
$password = '';
//=============Data Base Information=================
$database = 'login';
$conn = mysql_connect($host,$user,$password) or die('Server Information is not Correct'); //Establish Connection with Server
mysql_select_db($database,$conn) or die('Database Information is not correct');
//===============End Server Configuration============
//=============Starting Registration Script==========
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['pass1']);
$email = mysql_real_escape_string($_POST['email']);
$country = mysql_real_escape_string($_POST['country']);
$rights = '0';
$IP = $_SERVER['REMOTE_ADDR'];
//=============To Encrypt Password===================
//============New Variable of Password is Now with an Encrypted Value========
$query = mysql_query("SELECT username FROM users WHERE username = '".$username."'");
if (mysql_num_rows($query) > 0)
{
echo 'Username or email already in use.';
}
else{
if(isset($_POST['btnRegister'])) //===When I will Set the Button to 1 or Press Button to register
{
$query = "insert into users(username,password,email,country,rights,IP,registrated)values('$username','$password','$email','$country',$rights,$IP,NOW())" ;
$res = mysql_query($query);
header("location:load.php");
}
}
I think you are getting the error because you are missing some quotes in your query for two columns wich really looks like not integers
'$email','$country',$rights,$IP,NOW())" //$rights and $ip doesn't have quotes
so your query would look like
"insert into users(username,password,email,country,rights,IP,registrated)values('$username','$password','$email','$country','$rights','$IP',NOW())"
Use mysql_error() in order to see what the error message is. If you haven't changed anything in the script then it's either your database structure has changed or your rights have.
Update:
You don't have quotes around your IP value, which is surprising because you said that query worked until now. Anyway you should also consider saving IPs the RIGHT way. Good luck!

Categories