fixing database encoding to UTF8 - php

I need to change encoding of whole database to UTF8.
I was connect to database like this before:
// MySQL connect information.
$username = "root";
$password = "";
$host = "localhost";
$database = "yac";
// Connect.
$connection = mysql_connect($host, $username, $password)
or die ("It seems this site's database isn't responding.");
mysql_select_db($database)
or
die ("It seems this site's database isn't responding.");
my database content was stored to database without mysql_query("SET NAMES UTF8"); and data was like this:
باشگاه هوانوردان جوان
now recently I connect to database like this:
$mysql_username = "root";
$mysql_password = "";
$mysql_host = "localhost";
$mysql_database = "tick5";
$connection = mysql_connect($mysql_host, $mysql_username, $mysql_password) or die ("It seems this site's database isn't responding.");
mysql_select_db($mysql_database) or die ("It seems this site's database isn't responding.");
mysql_query("SET NAMES utf8");
mysql_query("SET CHARACTER_SET utf8");
and stored data is like this:
عنوان مقاله
since I use mysql_query("SET NAMES UTF8"); before INSERT queries I haven't any problem but now I need my old content and I want to change encoding and fix this.
How to do this?
NOTE : my tables Collation is utf8_unicode_ci

Try to see if this can help you. http://www.gentoo-wiki.info/TIP_Convert_latin1_to_UTF-8_in_MySQL
If you do not have a running Linux system, you can download an Ubuntu LiveCD and run it directly from the CD. http://www.ubuntu.com/download/desktop

Related

Php script update VIEW from other data base

I need to write a script which will take the values from two columns and use them to update the column in a view that I created in another database. In the first database I have sku and qty as well as in the view.
here is my code:
$server = 'localhost';
$user = 'invodata';
$pass = 'Abcd1234!1';
$dbname = 'tboinvodata';
$con = mysql_connect($server, $user, $pass) or die("Can't connect");
mysql_select_db("tboinvodata") or die(mysql_error());
$result = mysql_query("SELECT item, onhand FROM immaster"); <- this is getting the values from the columns in the first data base
$server = 'localhost';<-setting up my second connection to other database
$user = 'tbo';
$pass = 'Abcd1234!1';
$dbname = 'i187358_mage1';
$con = mysql_connect($server, $user, $pass) or die("Can't connect");
mysql_select_db("i187358_mage1") or die(mysql_error());
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {<-this gets the array from other database
UPDATE qtyview SET qty = $row["onhand"] WHERE sku = item;<- this should update the necessary columns "sku" is used in my view and "item" is used in the first data base I use this so the proper rows in the other columns get updated.
}
?>
really not sure what I am doing wrong I am pretty new to this though.
You can make multiple calls to mysql_connect() and use them like this.
First connect to two your MYSQL USER
$con1 = mysql_connect($server, $user, $pass);
$con2 = mysql_connect($server, $user, $pass, true);
Then establish a connect with different DATABASE
mysql_select_db('firstdatabase', $con1);
mysql_select_db('seconddatabase', $con2);
Then query from firstdatabase like this
mysql_query('select * from views', $con1);
And query from seconddatabase
mysql_query('select * from views', $con2);
This code is not tested by me...but i think it will work good for you.. :)

json_encode, utf8 and Greek characters

I am writing an android app and i want to query a db and get the results with json_encode.
So i have this db:
CREATE DATABASE `my_db` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
I use utf8 because i want to store greek characters. All the tables have utf8_general_ci collation.
At last i have this php service that i use to query my db.
<?php
$host = "localhost";
$username = "root";
$password = "password";
$dbname = "my_db";
$con = mysql_connect($host,$username,$password) or die ('Error connecting to mysql');
mysql_query("SET character_set_results=utf8", $con);
mysql_query("SET NAMES 'utf8'", $con);
mysql_select_db($dbname,$con);
$query = "SELECT * from patients";
$result = mysql_query($query,$con);
while($e=mysql_fetch_assoc($result))
$output[]=$e;
print(json_encode($output));
mysql_close($con);
?>
The problem is that in the output i dont get greek characters. Instead i get this:
[{"patient_id":"2","email":"patienta#mail.com","password":"password2","firstname":"\u03a7\u03a1\u0397\u03a3\u03a4\u039f\u03a3","lastname":"\u039c\u03a0\u0391\u0396\u0399\u03a9\u03a4\u0397\u03a3","birth":"1989-11-11","mobile":"6941212121","tameio":"\u039f\u0393\u0391"}]
That is right. All the chars not in ascii will be converted into UTF-8 format.
The data will be decoded correctly by json_decode.

PHP Select fields from database where username equals X

im having problems in PHP with selecting Infomation from a database where username is equal to $myusername
I can get it to echo the username using sessions from the login page to the logged in page.
But I want to be able to select things like 'bio' and 'email' from that database and put them into variables called $bio and $email so i can echo them.
This is what the database looks like:
Any ideas?:/
You should connect to your database and then fetch the row like this:
// DATABASE INFORMATION
$server = 'localhost';
$database = 'DATABASE';
$dbuser = 'DATABASE_USERNAME';
$dbpassword = 'DATABASE_PASSWORD';
//CONNECT TO DATABASE
$connect = mysql_connect("$server", "$dbuser", "$dbpassword")
OR die(mysql_error());
mysql_select_db("$database", $connect);
//ALWAYS ESCAPE STRINGS IF YOU HAVE RECEIVED THEM FROM USERS
$safe_username = mysql_real_escape_string($X);
//FIND AND GET THE ROW
$getit = mysql_query("SELECT * FROM table_name WHERE username='$safe_username'", $connect);
$row = mysql_fetch_array($getit);
//YOUR NEEDED VALUES
$bio = $row['bio'];
$email = $row['email'];
Note 1:
Dont Use Plain Text for Passwords, Always hash the passwords with a salt
Note 2:
I used MYSQL_QUERY for your code because i don't know PDO or Mysqli, Escaping in MYSQL is good enought but Consider Using PDO or Mysqli , as i don't know them i can't write the code with them for you
Simplistic PDO examples.
Create a connection to the database.
$link = new PDO("mysql:host=$db_server;dbname=$db_name", $db_user, $db_pw, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
$link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
Use the $link variable when creating (preparing) and executing your SQL scripts.
$stmt = $link->prepare('insert into `history` (`user_id`) values(:userId)');
$stmt->execute(array(':userId' => $userId));
Code below will read data. Note that this code is only expecting one record (with 2 data elements) to be returned, so I'm storing whatever is returned into a single variable (per data element), $webId and $deviceId.
$stmt = $link->prepare('select `web_id`, `device_id` from `history` where `user_id` = :userId');
$stmt->execute(array(':userId' => $userId));
while($row = $stmt->fetch()) {
$webId = $row["web_id"];
$deviceId = $row["device_id"];
}
From the picture I can see you are using phpMyAdmin - a tool used to handle MySQL databases. You first must make a connection to the MySql server and then select a database to work with. This is shown how below:
<?php
$username = "your_name"; //Change to your server's username
$password = "your_password"; //Change to your server's password
$database = "your_database" //Change to your database name
$hostname = "localhost"; // Change to the location of your server (this will prolly be the same for you I believe tho
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
$selected = mysql_select_db($database, $dbhandle)
or die("Could not select examples");
?>
Then you can write something like this:
<?php
$bio = mysql_query("SELECT bio FROM *your_database_table_name* WHERE username='bob' AND id=1");
?>
and
<?php
$email = mysql_query("SELECT email FROM *your_database_table_name* WHERE username='bob' AND id=1");
?>
Where *your_database_table_name* is the table in the database you selected which you are trying to query.
When I was answering your question, I was referencing this site: http://webcheatsheet.com/PHP/connect_mysql_database.php. So it might help to check it out as well.

connect oracle database with php and execute sql?

Hey guys im trying to connect to an oracle database with php. I tried it like i do it with mysql.
How to do it like this:
$host="localhost";
$user="username";
$pass="password";
$db="database";
$link = mysql_connect($host, $user, $pass) or die ("Keine Verbindung zu der Datenbank moeglich.");
mysql_select_db($db, $link);
$sql = "SQL query goes here";
$result = mysql_query($sql);
How can i do exact this with an oracle database. I have the following connection details
sid, ip, port, username, password.
Simple script:
$DB = '//1.2.3.4:1521/XE';
$DB_USER = 'user';
$DB_PASS = 'pass';
$DB_CHAR = 'AL32UTF8';
$conn = oci_connect($DB_USER, $DB_PASS, $DB, $DB_CHAR);
$statement = oci_parse($conn, 'select 1 from dual');
oci_execute($statement);
$row = oci_fetch_array($statement, OCI_ASSOC+OCI_RETURN_NULLS);
To connect with an Oracle database, you don't use the mysql extension (since that is for MySQL). You should use PDO, with the OCI/Oracle adapter.
You'll want to use PDO to connect to Oracle here is the PHP manual page on creating a connection using PDO, the example given is for MySQL but it will work fine with Oracle. You will need to ensure the PDO:Oracle extension is installed and running on your PHP configuration.

How can i store Hindi data in xampp PHPmyadmin?

How can i store Hindi data in Mysql?
I want to code using zend.
I created 1 table in Mysql, I set collation as utf8_general_ci.
You should probably use UTF-16 rather than UTF-8. See: http://forums.mysql.com/read.php?103,86499,86910
As you are coding using ZEND PHP Framework, just before the SQL Query for insert/select add the 2 lines for Setting NAMES & Character Set:
$dbhost = 'xx.xx.xx.xx';
$dbuser = 'xyz';
$dbpass = 'xxxxxx';
$dbname = 'yyyy';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
mysql_query("SET NAMES utf8");
mysql_query("set characer set utf8");
$result = mysql_query("SELECT * FROM tablename etc...");

Categories