1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Convert Amharic to English

Discussion in 'PHP' started by KangBroke, Dec 2, 2015.

  1. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #21
    Oh, that was a single URL not two separate ones... hah... So I was looking at the wrong pages.

    Well, at least that page only shows 17 blocked items.

    I'd be tempted to point at the CSS trying to use "visual geez unicode" -- but those characters are actually encoding as question marks in the output -- not even extended character replacement. Those are ACTUAL question marks, ASCII 0x3F's... the only way for that to happen would be if they were NOT stored properly in the database in the first place. Using a tool like phpMyAdmin are you seeing the actual characters there, or were they banjaxed being added to the database?

    Hmm... out of missiles, time to go Winchester on this; Let's go down the "must do" list for UTF-8 support.

    http header -- you've added the header() command so good there.

    html charset line... you've got the http-equiv which is correct for a XML tranny document.

    Database format -- you said it's "utf8_general_ci" so that checks out...

    Assuming everything is copacetic in the database that just leaves...

    SQL connection -- Are you setting the character encoding after or during connection? Here's the relevant PHP.net manual pages and example code

    outdated mysql_ crap:
    http://php.net/manual/en/function.mysql-set-charset.php
    mysql_set_charset('utf8', $conn);
    Code (markup):
    mysqli:
    http://php.net/manual/en/mysqli.set-charset.php
    $db->set_charset('utf8');
    Code (markup):
    PDO: (you do it on the constructor)
    http://php.net/manual/en/pdo.construct.php
    $db = new PDO('mysql:host=localhost;dbname=yourdb;charset=utf8');
    Code (markup):
    In all three cases, hypens are invalid in SQL config values, so it's utf8, NOT utf-8 on those. :/ Pain in the ass when everything else want's you to say "utf-8"... and some things will ignore it if you make it uppercase as "UTF-8" or "UTF8" :(

    Are you doing that? If not that could be corrupting them both on saving them and on reading them.

    Apart from that... well... I just blew through all 511 rounds in the drum.
     
    deathshadow, Dec 3, 2015 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #22
    That's the icon from the "blink" (chrome, opera, vivaldi) version of Adblock Pro, telling me how many advertisements and suspect scripts/uri's it blocked during the page load. Not so much an error as an indication the page is filled with crap I'd never see. I've NEVER seen the counter go that high before -- most I've ever seen has been mid 30's.
     
    deathshadow, Dec 3, 2015 IP
    KangBroke likes this.
  3. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #23
    Oh, almost forgot, if it's PDO and you're on a outdated pile of **** version of PHP (pretty much anything pre 5.4) the charset is ignored in the DSN, so you have to do a exec query to set it.

    $db->exec("set names utf8");
    Code (markup):
     
    deathshadow, Dec 3, 2015 IP
  4. KangBroke

    KangBroke Notable Member

    Messages:
    1,026
    Likes Received:
    59
    Best Answers:
    4
    Trophy Points:
    265
    #24
    Ok I do want to figure this out, no I am using mysqli Procedural not mysql or PDO.

    I have created a page using 1 video which is stored SQL in Amharic, and I want shown frontend English.
    Below is a clip to show you how it is stored SQL.
    [​IMG]
    [​IMG]

    ethiopianthisweek.com/ethioonutube.com/1.php is where you can see the video 1564 on the front end.

    I used the code from http://php.net/manual/en/mysqli.set-charset.php above the output.
     
    KangBroke, Dec 3, 2015 IP
  5. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #25
    Before or after the query? It should be before ANY queries are run at all, once you run a query you can't change that value.

    It definitely is gettting banjaxed somewhere between the DB and it landing in PHP.

    I'm almost wondering if PHP itself isn't defaulting to utf-8, but then the other pages on the same server shouldn't work if they are pulling from mysql as well.

    Hmm, I've got some conflicting reports that the procedural version of "mysqli_set_charset" doesn't actually work in PHP 5.5 or newer; again why I say they should just axe the stupid procedural wrappers altogether.

    Make sure you are setting the charset RIGHT after connection before any queries are called, and make sure you are using the object version instead of the procedural.

    Apart from that, short of editing the PHP.ini to force the charset I've no clue... and if you needed to do that, none of the other text would be working either. It's obvious it CAN serve that text, it's just not clear why your code isn't working.

    You might want to dig in and figure out why their code works when yours doesn't.

    -- EDIT --

    ARE you running ANY string manipulation functions on that? The normal string functions in PHP 5.x/earlier are NOT UTF safe, you have to use the iconv functions or mbstring extension to handle any changes to them safely. Likewise htmlspecialchars in PHP 5/earlier can make a real mess of UTF.

    One of the other reasons PHP 7 is a welcome change; since not only is everything utf friendly, the CODE is now UTF-8 locked... so you can use utf-8 characters in your function, class and variable names. A LOT of these headaches simply don't exist in it.
     
    deathshadow, Dec 3, 2015 IP
  6. KangBroke

    KangBroke Notable Member

    Messages:
    1,026
    Likes Received:
    59
    Best Answers:
    4
    Trophy Points:
    265
    #26
    <?php
    header('Content-Type: text/html; charset=utf-8');
    ?><!DOCTYPE html>
    <html lang="en">
      <head>
    <meta charset="utf-8"/><html xmlns="http://www.w3.org/1999/xhtml" lang="am" xml:lang="am">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="" />
    <meta name="keywords" content="" />
    
        <meta name="author" content="">
    <title></title>
    </head>
      <body>
      <?php
      $servername = "";
    $username = "";
    $password = "";
    $dbname = "";
    // Create connection
    $conn = mysqli_connect($servername, $username, $password, $dbname);
    /* check connection */
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }
    printf("<br>Initial character set: %s\n", mysqli_character_set_name($conn));
    /* change character set to utf8 */
    if (!mysqli_set_charset($conn, "utf8")) {
        printf("<br>Error loading character set utf8: %s\n", mysqli_error($conn));
        exit();
    } else {
        printf("<br>Current character set: %s\n", mysqli_character_set_name($conn));
    }
    $conn = mysqli_connect($servername, $username, $password, $dbname);
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }
    ?> <div class="viewbox"><?php
    $conn = mysqli_connect($servername, $username, $password, $dbname);
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }
    $sql = "SELECT * FROM video WHERE VID=1564 ";
    $result = mysqli_query($conn, $sql);
    
    
                   
                  if (mysqli_num_rows($result) > 0) {
        // output data of each row
        while($row = mysqli_fetch_assoc($result)) {
                              
    $title=$row['title'];  
    
    mysqli_close($conn);
    Code (markup):
     
    KangBroke, Dec 3, 2015 IP
  7. KangBroke

    KangBroke Notable Member

    Messages:
    1,026
    Likes Received:
    59
    Best Answers:
    4
    Trophy Points:
    265
    #27
    <span class="title">
    <?php echo $title; ?></span>

    I just ran echo 'Current PHP version: ' . phpversion();

    Current PHP version: 5.4.452.0

    Using a brand new shared hosting account with ehost
     
    KangBroke, Dec 3, 2015 IP
  8. #28
    Uhm, you did it BEFORE you connected for the actual connection live in the query. You are reconnecting three different times, you need to do it for EACH $conn! Not that I have ANY clue why you'd do that over and over and over again like that. Why are you making three different connection attempts?!? That's just gibberish.

    You also have TWO HTML tags :D (I know, left over from copypasta)... of course you also have that X-UA crap that should only be in there to set OLDER ua behaviors, not new ones. (Has no business in new code)

    Try this:
    <?php
    
    header('Content-Type: text/html; charset=utf-8');
    
    echo '<!DOCTYPE html><html lang="en"><head><meta charset="utf-8">
    
    <meta
    	name="viewport"
    	content="width=device-width, height=device-height, initial-scale=1"
    >
    
    <title>UTF-8 Test</title>
    
    </head><body>';
    
    $servername = "";
    $username = "";
    $password = "";
    $dbname = "";
    
    $conn = new mysqli($servername, $username, $password, $dbname);
    if ($conn->connect_error) die(
    	'Connect failed: ' . $conn->connect_error
    );
    
    echo 'Initial character set: ', $conn->character_set_name(), '<br>';
    
    if (!$conn->set_charset('utf8')) die(
    	'Error loading character set utf8: ' . $conn->error . '<br>'
    );
    
    echo '
    	Current character set: ', $conn->character_set_name(), '<br>
    	
    	<div class="viewbox">';
    	
    $result = $conn-> = mysqli_query($conn, '
    	SELECT title FROM video WHERE VID=1564
    ');
    
    if ($row = $result->fetch_assoc()) echo $row['title'], '<br>';
    
    $conn->close();
    
    echo '
    	</div>
    	
    </body></html>';
    Code (markup):
    ONE connection! If you make a new connection, you're not releasing the old one (you should have one close for each connect) and anything you set -- like the character set, is unset!
     
    Last edited: Dec 3, 2015
    deathshadow, Dec 3, 2015 IP
  9. KangBroke

    KangBroke Notable Member

    Messages:
    1,026
    Likes Received:
    59
    Best Answers:
    4
    Trophy Points:
    265
    #29
    Ok trying that code I get Parse error: syntax error, unexpected '=', expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' in 1.php on line 37
     
    KangBroke, Dec 3, 2015 IP
  10. KangBroke

    KangBroke Notable Member

    Messages:
    1,026
    Likes Received:
    59
    Best Answers:
    4
    Trophy Points:
    265
    #30
    Mainly because your online, and I was being fast about it. Normally I run my code through the w3 validator and clear any errors. Now that you told me about adblock pro, I want to give that a try as well.

    How do you write PHP? PDO, Procedural or OO?
     
    KangBroke, Dec 3, 2015 IP
  11. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #31
    Yeah, typo, this:
    $result = $conn-> = mysqli_query

    Should have been this:
    $result = $conn->mysqli_query

    PDO only has OO... I prefer OO as that's the native for PDO and mysqli, the "function" based versions are just wrappers for the object, so they're extra overhead.

    In general terms I'm not a "functions or objects" coder, both has strengths and weaknesses and I use each when appropriate. Functions are great for clean simple one-offs where you don't need values to be retained across execution. They're simple and fast. Objects have more overhead, but are great for when you want to preserve values, or restrict the scope of them using the "private" keyword.

    Both have their place. When it comes to database access, objects just make more sense, particularly if you are restricting their scope so things like the connection variable, much less the security data doesn't end up global.

    One of the DUMBEST things you'll see in a lot of production code is putting the SQL login info into global scope, or worse into an undeletable access anywhere global scope using DEFINE. Both are some serious herpafreakingderp -- but then PHP security in general is herpaderp since they allow readfile and fopen to access files with .php extensions, and allow include or require to access files without those extensions... so trying to hide information you want to keep secure -- like the SQL connection info -- is pretty much a joke.
     
    deathshadow, Dec 4, 2015 IP
    KangBroke likes this.
  12. KangBroke

    KangBroke Notable Member

    Messages:
    1,026
    Likes Received:
    59
    Best Answers:
    4
    Trophy Points:
    265
    #32
    Fatal error: Call to undefined method mysqli::mysqli_query() in 1.php on line 37

    That throws this error now
     
    KangBroke, Dec 4, 2015 IP
  13. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #33
    Gah, and this is why I hate translating code from functional to object, so I just use object.

    $result = $conn->query

    Is what it should be.
     
    deathshadow, Dec 4, 2015 IP
  14. KangBroke

    KangBroke Notable Member

    Messages:
    1,026
    Likes Received:
    59
    Best Answers:
    4
    Trophy Points:
    265
    #34
    If I run it like this

    $result = $conn->query($conn, 'SELECT title FROM video WHERE VID=1564');

    I get


    Warning: mysqli::query() expects parameter 1 to be string, object given in 1.php on line 37

    Fatal error: Call to a member function fetch_assoc() on a non-object in 1.php on line 39


    $result = $conn->query;

    I get

    Fatal error: Call to a member function fetch_assoc() on a non-object in 1.php on line 39
     
    KangBroke, Dec 4, 2015 IP
  15. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #35
    Ok, this is stuff you REALLY should know how to fix on your own ...

    $result = $conn->query('SELECT title FROM video WHERE VID=1564');
     
    deathshadow, Dec 4, 2015 IP
    KangBroke likes this.
  16. KangBroke

    KangBroke Notable Member

    Messages:
    1,026
    Likes Received:
    59
    Best Answers:
    4
    Trophy Points:
    265
    #36
    You are right, I should. I did that as soon as I woke up and didn't really give it much thought, just did what you recommended and posted the results. Now that I see what fixed it, my face was hit by my palm.
     
    KangBroke, Dec 4, 2015 IP