hariskar Posted January 3, 2017 Share Posted January 3, 2017 (edited) last_login in mysql is updated correctly when username is in Latin characters but not updated when in Greek. Here is the code: <?php //include config require_once('includes/config.php'); $username = $_POST['username']; $password = $_POST['password']; if($user->login($username,$password)){ $servername = "xxx"; $dbuser = "xxx"; $dbpass = "xxx"; $dbname = "xxx"; $conn = new mysqli($servername, $dbuser, $dbpass, $dbname); $sql = "UPDATE members SET last_login=now() WHERE username='$username' "; if ($conn->query($sql) === FALSE) {echo "Error updating record: " . $conn->error;}} //check if already logged in move to home page if( $user->is_logged_in() ){ header('Location: register'); } //process login form if submitted if(isset($_POST['submit'])){ $username = $_POST['username']; $password = $_POST['password']; if($user->login($username,$password)){ $_SESSION['username'] = $username; header('Location: /apotelesmata'); exit; } else { $error[] = 'Λάθος όνομα χρήστη ή κωδικός ή ο λογαριασμός σας δεν έχει ενεργοποιηθεί ακόμα.'; }}//end if submit //define page title $title = 'Login'; ?> and here is the included config.php <?php ob_start(); session_start(); //set timezone date_default_timezone_set('Europe/Athens'); //database credentials define('DBHOST','xxx'); define('DBUSER','xxx'); define('DBPASS','xxx'); define('DBNAME','xxx'); //application address define('DIR','http://www.mikroviologos.gr/'); define('SITEEMAIL','xxx@xxx.xx'); try { //create PDO connection $db = new PDO("mysql:host=".DBHOST.";dbname=".DBNAME, DBUSER, DBPASS); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $db->exec("SET NAMES 'utf8';"); } catch(PDOException $e) { //show error echo '<p class="bg-danger">'.$e->getMessage().'</p>'; exit; } //include the user class, pass in the database connection include('classes/user.php'); include('classes/phpmailer/mail.php'); $user = new User($db); ?> In the included config.php there is a line $db->exec("SET NAMES 'utf8';"); but the problem still exists. Could you please help me on that? Thank you! Edited January 3, 2017 by hariskar Link to comment Share on other sites More sharing options...
dsonesuk Posted January 3, 2017 Share Posted January 3, 2017 This has been discussed before about Greek characters see this http://w3schools.invisionzone.com/index.php?showtopic=44151 topic see if it helps. 1 Link to comment Share on other sites More sharing options...
hariskar Posted January 3, 2017 Author Share Posted January 3, 2017 Thank you but I can not understand what is happening: since there is $db->exec("SET NAMES 'utf8';"); in the included file, the username registers correctly in mysql and login/php works fine. The only problem is that when a user with Greek username logs in last_login in mysql does not update. Is there anything wrong with this line? : $sql = "UPDATE members SET last_login=now() WHERE username='$username' "; Link to comment Share on other sites More sharing options...
dsonesuk Posted January 3, 2017 Share Posted January 3, 2017 (edited) You did not read it through properly then, try again! it clearly states a better option than what you currently use and why. remove space at end of sql statement. Edited January 3, 2017 by dsonesuk 1 Link to comment Share on other sites More sharing options...
hariskar Posted January 3, 2017 Author Share Posted January 3, 2017 (edited) In included config.php I changed $db->exec("SET NAMES 'utf8';"); to $db->exec("SET CHARACTER SET utf8;"); but the problem remains: Greek username is correct in mysql with Greek characters being correctly displayed, but last_login still does not update for usernames in Greek. Edit: I had to put it again, because I had a 2nd connection to the db. Thank you! Edited January 3, 2017 by hariskar Link to comment Share on other sites More sharing options...
dsonesuk Posted January 3, 2017 Share Posted January 3, 2017 That is NOT! The solution that was written about, the solution enables correct writing AND reading from database. Link to comment Share on other sites More sharing options...
hariskar Posted January 3, 2017 Author Share Posted January 3, 2017 I was not very clear: Problem solved. I have one db connection in included file and a second in the main file. I had to put $db->exec("SET CHARACTER SET utf8;"); in both files after each connection to solve the problem. Link to comment Share on other sites More sharing options...
Ingolme Posted January 3, 2017 Share Posted January 3, 2017 The recommended way is described here for each of the MySQL libraries: http://php.net/manual/en/mysqlinfo.concepts.charset.php For MySQLi: $mysqli->set_charset() For PDO, use the constructor: new PDO("mysql:host=localhost;dbname=world;charset=utf8", 'my_user', 'my_pass') 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now