• Hey,
    I am creating a PHP file that will make changes to the wordpress database tables. For reasons that are explained in detail, I do not want to use the internal functions of WordPress to insert data into the database.

    In the PHP file, I save the data in the database, but in WordPress, it displays as a question mark.

    $mysqli = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
    $mysqli->set_charset("utf8mb4");
    $sql = "INSERT INTO
    wp_st_room_availability (post_id,check_in,check_out,number,post_type,adult_dbl_price,adult_price,child_price,child_nb_price,infant_price,extra_bed_price,status,priority,number_booked,parent_id,allow_full_day,number_end,booking_period,is_base,adult_number,child_number,room_price_modified,modified_by)
    VALUES ('$_room_id', '$_stamp', '$_stamp', '$_room_stock', 'hotel_room', '$_room_dbl_c', '$_room_sgl_c', '$_room_chdwb_c', '$_room_chdnb_c', '$_room_infant_c', '$_room_tpl_c', '$_status', NULL, 0, '$_hotel_id', 'on', NULL, 0, 0, 1, 0, NOW(), '$_modifier_name')";
    $result = $mysqli->query($sql);

    The data stored in the database through WordPress is also read as unintelligible characters when read by this PHP file.

    نام کارگزار


    Searching the internet, I found out that the problem is in the charset. In the wp-config.php file, the value

    define( 'DB_CHARSET', 'latin1' );

    is set.
    This WordPress site uses the Traveler theme, and I don’t know how to find out what charset it uses.

    I tried utf-8, utf8mb4 and latin1 in my PHP file, but the problem was not solved.

    I will be grateful if you help me to find the problem and solve it.

    • This topic was modified 1 year, 4 months ago by omipand.
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    Your theme uses whatever charset your WP installation is configured for. The default is UTF-8 and is hidden unless an alternative is chosen. You can see it at /wp-admin/options.php. Search the options list for “blog_charset”.

    Mismatched charsets will be an ongoing problem. What charset does your DB actually use? You can find out via the phpMyAdmin app, usually accessed through your hosting account. The charsets for every DB is at information_schema.SCHEMATA. “utf8mb4” is a good choice. Latin1 not so much, but it’s workable as long as everything is Latin1. With Latin1 you will have difficulty with foreign characters that are beyond ASCII code 255. Utf8mb4 will work for pretty much any character in existence.

    The definition for ‘DB_CHARSET’ should match that of your DB. If there’s any doubt, define it as an empty string to inherit the DB’s charset, whatever it may be.

    If all of your textual data is ASCII code 255 or less, you could switch from Latin1 to utf8mb4 since the encoding is the same up to that point. There are collations that relate back to a specific charset. The collations chosen should relate to your DB’s charset. There are collation settings for the entire DB, for each table, and for each textual column. Unless there’s a specific reason not to, all of these should match.

    Changing the collation in phpMyAdmin is trivial, but changing the charset itself requires certain SQL queries. Always make a full backup before doing so, as the conversion may have unanticipated undesirable consequences.

    Your WP charset in options.php should match that of your DB, but the way it’s presented may vary because it’s used in a HTML tag. For example, “utf8mb4” in the DB should be presented in options and HTML as “UTF-8”.

    Finally, it’s possible your theme’s chosen font simply has no glyphs (the graphic representation of a character) defined for certain characters. This can result in question marks for the missing glyphs. It’s possible there’s nothing wrong with your charsets and the issue is the chosen font. I don’t think this is the case here, but it’s worth mentioning.

Viewing 1 replies (of 1 total)

The topic ‘charset problem with wordpress’ is closed to new replies.