Page 1 of 1

Help with code

Posted: Tue Jan 22, 2019 8:30 pm
by frankus
I once used the below code to display user's data on a page using e.g. '.$extra2.' after login. But now it stopped.
Anyone knows how to make below code work in php 5.6 server, with MYSQLi functionality? Thank you.

Code: Select all

<?php
error_reporting(0);
session_start();
if($_SESSION['username'] != '')
{

$mysql_server = 'localhost';
$mysql_username = 'username';
$mysql_password = 'password';
$mysql_database = 'databasename';
$mysql_table = 'tablename';

$db = mysql_connect($mysql_server, $mysql_username, $mysql_password);
mysql_select_db($mysql_database, $db);
$sql = "SELECT fullname, email, extra1, extra2, extra3, extra4, extra5, extra6, extra7, extra8, extra9, extra10, extra11, extra12, extra13, extra14, extra15 FROM ".$mysql_table." WHERE username = '".$_SESSION['username']."'";
$result = mysql_query($sql, $db);
if ($data = mysql_fetch_array($result))
{
extract ($data, EXTR_OVERWRITE);
$status = array("Pending", "Active");
$_SESSION['fullname'] = $fullname;
$_SESSION['email'] = $email;
$_SESSION['extra1'] = $extra1;
$_SESSION['extra2'] = $extra2;
$_SESSION['extra3'] = $extra3;
$_SESSION['extra4'] = $extra4;
$_SESSION['extra5'] = $extra5;
$_SESSION['extra6'] = $extra6;
$_SESSION['extra7'] = $extra7;
$_SESSION['extra8'] = $extra8;
$_SESSION['extra9'] = $extra9;
$_SESSION['extra10'] = $extra10;
$_SESSION['extra11'] = $extra11;
$_SESSION['extra12'] = $extra12;
$_SESSION['extra13'] = $extra13;
$_SESSION['extra14'] = $extra14;
$_SESSION['extra15'] = $extra15;

}
mysql_close($db);
}
else{
header("Location: login.php"); // change according to what your login page is.
exit;
}
?>
Please NOTE: I'm aware of Dataviewer, Responsivedatatable, Mysqlconnector and Mysqltableeditor but they display data as list or table which I don't want.

Re: Help with code

Posted: Tue Jan 22, 2019 9:21 pm
by lummis
I'm not an expert but I believe that you need to add the i after all the functions eg $mysql_server = 'localhost'; needs to be $mysqli_server = 'localhost';

Re: Help with code

Posted: Tue Jan 22, 2019 9:48 pm
by Pablo
You will need to replace all mysql functions with mysqli ones.
https://www.phpclasses.org/blog/package ... ySQLi.html

Re: Help with code

Posted: Tue Jan 22, 2019 11:06 pm
by frankus
It didn't work.

Code: Select all

<?php
error_reporting(0);
session_start();
if($_SESSION['username'] != '')
{

$mysqli_server = 'localhost';
$mysqli_username = 'username';
$mysqli_password = 'password';
$mysqli_database = 'databasename';
$mysqli_table = 'tablename';

$db = mysqli_connect($mysqli_server, $mysqli_username, $mysqli_password);
mysqli_select_db($mysqli_database, $db);
$sql = "SELECT fullname, email, extra1, extra2, extra3, extra4, extra5, extra6, extra7, extra8, extra9, extra10, extra11, extra12, extra13, extra14, extra15 FROM ".$mysqli_table." WHERE username = '".$_SESSION['username']."'";
$result = mysqli_query($sql, $db);
if ($data = mysqli_fetch_array($result))
{
extract ($data, EXTR_OVERWRITE);
$status = array("Pending", "Active");
$_SESSION['fullname'] = $fullname;
$_SESSION['email'] = $email;
$_SESSION['extra1'] = $extra1;
$_SESSION['extra2'] = $extra2;
$_SESSION['extra3'] = $extra3;
$_SESSION['extra4'] = $extra4;
$_SESSION['extra5'] = $extra5;
$_SESSION['extra6'] = $extra6;
$_SESSION['extra7'] = $extra7;
$_SESSION['extra8'] = $extra8;
$_SESSION['extra9'] = $extra9;
$_SESSION['extra10'] = $extra10;
$_SESSION['extra11'] = $extra11;
$_SESSION['extra12'] = $extra12;
$_SESSION['extra13'] = $extra13;
$_SESSION['extra14'] = $extra14;
$_SESSION['extra15'] = $extra15;

}
mysqli_close($db);
}
else{
header("Location: login.php"); // change according to what your login page is.
exit;
}
?>

Re: Help with code

Posted: Tue Jan 22, 2019 11:13 pm
by lummis
It may help if you turn on error reporting as that may give a clue as to why it is not working.

Re: Help with code

Posted: Tue Jan 22, 2019 11:25 pm
by WWBman
It is not as simple as just changing mysql to mysqli.
E.g.
$result = mysql_query($sql, $db);
should be:
$result = mysqli_query($db, $sql);

EDIT:
For the error reporting you can put the following at the start of the page :
<?php
error_reporting(E_ALL);
?>

Re: Help with code

Posted: Wed Jan 23, 2019 12:27 am
by frankus
WWBman wrote: Tue Jan 22, 2019 11:25 pm It is not as simple as just changing mysql to mysqli.
E.g.
$result = mysql_query($sql, $db);
should be:
$result = mysqli_query($db, $sql);

EDIT:
For the error reporting you can put the following at the start of the page :
<?php
error_reporting(E_ALL);
?>
It didn't work out. And no error.

Re: Help with code

Posted: Wed Jan 23, 2019 7:20 am
by Pablo
Did you update ALL mysql statements?

Re: Help with code

Posted: Wed Jan 23, 2019 9:00 am
by WWBman
Depending on your php.ini settings, error messages were probably written to an "error_log" file.
If so you'll find the file in your hosting account in the folder where the page is that caused the error.
You can view the file to see the errors.

EDIT:
Just noticed that you have error_reporting(0) at the start of your code.
Remove or change this to get the errors reported.

BTW: You don't need the "i" added in the following, they are just variables.
$mysqli_server = 'localhost';
$mysqli_username = 'username';
$mysqli_password = 'password';
$mysqli_database = 'databasename';
$mysqli_table = 'tablename';

BUT:
mysqli_select_db($mysqli_database, $db);
should be:
mysqli_select_db($db, $mysqli_database);
and
$result = mysqli_query($sql, $db);
should be:
$result = mysqli_query($db, $sql);

Re: Help with code

Posted: Wed Jan 23, 2019 5:37 pm
by frankus
Pablo wrote: Wed Jan 23, 2019 7:20 am Did you update ALL mysql statements?
WWBman wrote: Wed Jan 23, 2019 9:00 am Depending on your php.ini settings, error messages were probably written to an "error_log" file.
If so you'll find the file in your hosting account in the folder where the page is that caused the error.
You can view the file to see the errors.

EDIT:
Just noticed that you have error_reporting(0) at the start of your code.
Remove or change this to get the errors reported.

BTW: You don't need the "i" added in the following, they are just variables.
$mysqli_server = 'localhost';
$mysqli_username = 'username';
$mysqli_password = 'password';
$mysqli_database = 'databasename';
$mysqli_table = 'tablename';

BUT:
mysqli_select_db($mysqli_database, $db);
should be:
mysqli_select_db($db, $mysqli_database);
and
$result = mysqli_query($sql, $db);
should be:
$result = mysqli_query($db, $sql);
It still doesn't work. No error shows up. Server log shows nothing about php :x

Code: Select all

<?php
error_reporting(E_ALL);
session_start();
if($_SESSION['username'] != '')
{

$mysql_server = 'localhost';
$mysql_username = 'username';
$mysql_password = 'password';
$mysql_database = 'databasename';
$mysql_table = 'tablename';

$db = mysqli_connect($mysqli_server, $mysqli_username, $mysqli_password);
mysqli_select_db($db, $mysqli_database);
$sql = "SELECT fullname, email, extra1, extra2, extra3, extra4, extra5, extra6, extra7, extra8, extra9, extra10, extra11, extra12, extra13, extra14, extra15 FROM ".$mysqli_table." WHERE username = '".$_SESSION['username']."'";
$result = mysqli_query($db, $sql);
if ($data = mysqli_fetch_array($result))
{
extract ($data, EXTR_OVERWRITE);
$status = array("Pending", "Active");
$_SESSION['fullname'] = $fullname;
$_SESSION['email'] = $email;
$_SESSION['extra1'] = $extra1;
$_SESSION['extra2'] = $extra2;
$_SESSION['extra3'] = $extra3;
$_SESSION['extra4'] = $extra4;
$_SESSION['extra5'] = $extra5;
$_SESSION['extra6'] = $extra6;
$_SESSION['extra7'] = $extra7;
$_SESSION['extra8'] = $extra8;
$_SESSION['extra9'] = $extra9;
$_SESSION['extra10'] = $extra10;
$_SESSION['extra11'] = $extra11;
$_SESSION['extra12'] = $extra12;
$_SESSION['extra13'] = $extra13;
$_SESSION['extra14'] = $extra14;
$_SESSION['extra15'] = $extra15;

}
mysqli_close($db);
}
else{
header("Location: login.php"); // change according to what your login page is.
exit;
}
?>

Re: Help with code

Posted: Wed Jan 23, 2019 6:40 pm
by WWBman
When you load the page is the result just a blank page?
If so then this does probably mean it's a PHP error.

I doubt whether the errors will be shown in the server log (but might!).

Clear the browser's cache and reload the website.
Then look in the folder of the problem page and see if an error file has just been created (by looking at the date created).

If there is no error file there may be options in the php.ini file preventing the logging of errors.
It should include:
log_errors = On
error_log = error_log (this will be the filename of the file containing the error messages)

The php.ini file is usually well documented with comments so reading it may give you a clue why no errors are being shown.

BTW you've changed the following:
$mysql_server = 'localhost';
$mysql_username = 'username';
$mysql_password = 'password';
$mysql_database = 'databasename';
$mysql_table = 'tablename';

but you haven't changed the corresponding values in the functions!
E.g.
mysqli_select_db($db, $mysqli_database);
should be
mysqli_select_db($db, $mysql_database);