Weid characters in mysql output

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shanecody
Forum Newbie
Posts: 23
Joined: Fri May 08, 2009 4:12 pm

Weid characters in mysql output

Post by shanecody »

Don't know if this has already been posted, but I can figure out what to search for. I am trying to output from a database and keep getting weird � in the output, I know this is caused by “ ” and ’. You can see what i mean here http://www.rootsmusicreport.com/NewLook ... ingle&id=6

What do I need to do to stop it, my code is:
$review = nl2br($_POST['review']);
to input the review field.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Weid characters in mysql output

Post by Eric! »

From your link it appears you need to use http://cr.php.net/mysql_real_escape_string
shanecody
Forum Newbie
Posts: 23
Joined: Fri May 08, 2009 4:12 pm

Re: Weid characters in mysql output

Post by shanecody »

i am doing that before entering data, it doesn't help

the characters that are the issue are not " (") rather “ (“) ” (”)
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Weid characters in mysql output

Post by Eric! »

Then it is due to your echo to the browser, use http://cr.php.net/htmlspecialchars
shanecody
Forum Newbie
Posts: 23
Joined: Fri May 08, 2009 4:12 pm

Re: Weid characters in mysql output

Post by shanecody »

still no, here is my code:

Input form:

Code: Select all

 
<?php
    function mysql_prep( $value ) {
        $magic_quotes_active = get_magic_quotes_gpc();
        $new_enough_php = function_exists( "mysql_real_escape_string" ); // i.e. PHP >= v4.3.0
        if( $new_enough_php ) { // PHP v4.3.0 or higher
            // undo any magic quote effects so mysql_real_escape_string can do the work
            if( $magic_quotes_active ) { $value = stripslashes( $value ); }
            $value = mysql_real_escape_string( $value );
        } else { // before PHP v4.3.0
            // if magic quotes aren't already on then add slashes manually
            if( !$magic_quotes_active ) { $value = addslashes( $value ); }
            // if magic quotes are active, then the slashes already exist
        }
        return $value;
    }
 
    if (isset($_POST['submit'])) { // Form has been submitted.
        $errors = array();
 
        $revid = $_SESSION['user_id']; 
        $artist = trim(mysql_prep($_POST['artistName'])); 
        $cd = trim(mysql_prep($_POST['album'])); 
        $label = trim(mysql_prep($_POST['label']));
        $genre = $_POST['genre'];
        $rating = $_POST['rating'];
        $review = mysql_prep($_POST['review'], ENT_QUOTES);
        $web = trim(mysql_prep(htmlentities($_POST['webSite'])));  
        $today = date("y/m/d");
        
        $image = getImage('file_upload', true, 200, 300);
        
        if($_SESSION['type'] == 'admin'){
            $query = sprintf("INSERT INTO reviews (RevId, ArtistName, CDTitle, Label, Genre, Rating, Review, ArtistSite, DateAdded, Image) 
                                                             VALUES ('". $revid ."', '". $artist ."', '". $cd ."', '". $label ."', '". $genre ."', '". $rating ."', '".$review."', '". $web ."',  
                                                                             '". $today ."', '". $image ."')");
        } elseif ($_SESSION['type'] == 'reviewer'){
            $query = "INSERT INTO revcheck (RevId, ArtistName, CDTitle, Label, Genre, Rating, Review, ArtistSite, DateAdded, Image) 
                                                             VALUES ('". $revid ."', '". $artist ."', '". $cd ."', '". $label ."', '". $genre ."', '". $rating ."', '". $review ."', '". $web ."',  
                                                                             '". $today ."', '". $image ."')";
        }
        mysql_query($query);
        $id = mysql_insert_id();
 
        $thumb = getImage('file_upload', true, 100, 100);
        if(is_array($image)){
            array_merge($errors, $image);
        }
        
        if($_SESSION['type'] == 'admin'){
            $query = "UPDATE reviews SET Thumb = '{$thumb}' WHERE id ={$id} LIMIT 1";
        } elseif ($_SESSION['type'] == 'reviewer'){
            $query = "UPDATE revcheck SET Thumb = '{$thumb}' WHERE id ={$id} LIMIT 1";
        }
        
        mysql_query($query);
        
        //$review = stripslashes($review);
    
        if($_SESSION['type'] == 'reviewer'){
            echo "<h1>Thank You For Your Submition</h1>
                        <h3>Your review will be checked for errors, and then posted.  This may take up to 24 hours.</h3>";
        } 
        echo "
            <p>
            Below is what you submitted, if you notice any errors below, please email us at ***
            </p>
            
            <p>
            <div style='float:left; width=300;'>
            Image:<br />
            ";
            
        if($_SESSION['type'] == 'admin'){
            echo "
            <img src='../images/image.php?id=$id&type=review' /><br />
            </div>
            <div style='float:left; margin-left:10px; width:600px;'>
            Thumbnail:<br />
            <img src='../images/image.php?id=$id&type=reviewThumb' /><br />
            </p>";
        } elseif ($_SESSION['type'] == 'reviewer'){
            echo "
            <img src='../images/image.php?id=$id&type=reviewCheck' /><br />
            </div>
            <div style='float:left; margin-left:10px; width:600px;'>
            Thumbnail:<br />
            <img src='../images/image.php?id=$id&type=reviewThumbCheck' /><br />
            </p>";
        }
        echo"
            Artist Name: $artist<br />
            Album Title: $cd<br />
            Label: $label<br />
            Genre: $genre<br />
            Rating: $rating<br />
            Artist's Website Address: $web<br />
            Review: $review<br />
            </p>
            </div>";
    }
?>
<div style="clear:both;">
<h1>Post New Review</h1>
<form action='reviewSubmitForm.php' enctype='multipart/form-data' method='post'>
<label for='artistName'>Artist Name:</label><br />
<input type='text' name='artistName' id='artistName' /><br />
<label for='album'>Album Title:</label><br />
<input type='text' name='album' id='album' /><br />
<label for='label'>Label:</label><br />
<input type='text' name='label' id='label' /><br />
<label for='genre'>Genre:</label><br />
<select name='genre' id='genre'>
    <option value='Bluegrass' selected='selected'>Bluegrass</option>
    <option value='Blues'>Blues</option>
    <option value='Folk'>Folk</option>
    <option value='Gospel'>Gospel</option>
    <option value='Jazz'>Jazz</option>
    <option value='Mixed Genres'>Mixed Genres</option>
    <option value='Reggae'>Reggae</option>
    <option value='Roots Country'>Roots Country</option>
    <option value='Roots Rock'>Roots Rock</option>
    <option value='True Country'>True Country</option>
    <option value='Western Swing'>Western Swing</option>
    <option value='Zydeco'>Zydeco</option>
</select><br />
Rating:<br />
<label for='rating'>3</label>
<input name='rating' type='radio' value='3' />
<label for='rating'>4</label>
<input name='rating' type='radio' value='4' />
<label for='rating'>5</label>
<input name='rating' type='radio' value='5' /><br />
<input type='hidden' name='MAX_FILE_SIZE' value='1000000' />
<label for='file'>Upload CD Image: (in .jpg format please)</label><br />
<input name='file_upload' id='file_upload' type='file' /><br />
<label for='webSite'>Artist's Website Address: (Don't forget the http://)</label><br />
<input name='webSite' type='text' value='http://' /><br />
<label for='review'>Review:</label><br />
<textarea name='review' cols='100' rows='15' id='review'></textarea><br />
<input type="submit" name="submit" value="Submit Review" />
</form>
 
to output:
 
<?php
                    $id = $_GET['id'];
                    
                    $query = "SELECT id, RevId, ArtistName, CDTitle, Label, Genre, Rating, Review, ArtistSite, DateAdded FROM reviews WHERE id = {$id}"; 
                    $result = mysql_query($query);
                    $row = mysql_fetch_assoc($result);
                    
                    $query2 = "SELECT id, RevId, ArtistName, CDTitle, Label, Genre, Rating, Review, ArtistSite, DateAdded FROM reviewers WHERE id = {$id}"; 
                    $result = mysql_query($query);
                    
                    if($result){
                        $lastWeek = time() - (7 * 24 * 60 * 60);
                        echo "<div style=\"margin-top:10px;\">
                                    <div style=\"float:left; width:200px; color:#fff;\">
                                        <img style=\"float:left; width:200px; height:200px;\" 
                                            src=\"images/image.php?id=".$row['id']."&type=review\" alt=\"Review of ".$row['ArtistName']."'s CD: ".$row['CDTitle']. "\" />
                                        <b>Label:</b> ".$row['Label']."<br />
                                        <b>Genre:</b> ".$row['Genre']."<br />
                                        <b>Rating:</b> ".$row['Rating']."
                                    </div>
                                    <div style=\"float:left; margin:0 0 0 7px;\" >
                                        <span style=\"color: #fff; font-size:22px; font-weight:bold; \">" .$row['ArtistName']."</span>";
                                    if(strtotime($row['DateAdded']) > $lastWeek)
                                        echo "<span style=\"color:#F00; font-weight:bold; vertical-align:super;\">*NEW*</span>";
                        echo "<br />
                                        <span style=\"color: #febd01; font-size:18px; font-weight:bold; \">".$row['CDTitle']."</span><br />
                                    </div>
                                    <div style=\"background-color:#4975B6; border: 2px #fff solid; float:left; width:485px; font-size:12px; margin:7px 0 7px 7px; padding:10px; text-align:justify;\">
                                        ".htmlentities($row['Review'])."<br /><br />Reviewed By: 
                                    </div>
                                    </div>
                                    </div>";
                    }
 
  ?>
Last edited by Benjamin on Tue Jun 23, 2009 11:32 pm, edited 2 times in total.
Reason: Added [code=php] tags.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Weid characters in mysql output

Post by Eric! »

the functions for some reason aren't complete. Just replace the characters using the following char # and matching string as you need it:

str_replace with the char and the strings listed below

Code: Select all

 
        $trans[chr(130)] = '&sbquo;';    // Single Low-9 Quotation Mark 
        $trans[chr(131)] = '&fnof;';    // Latin Small Letter F With Hook 
        $trans[chr(132)] = '&bdquo;';    // Double Low-9 Quotation Mark 
        $trans[chr(133)] = '&hellip;';    // Horizontal Ellipsis 
        $trans[chr(134)] = '&dagger;';    // Dagger 
        $trans[chr(135)] = '&Dagger;';    // Double Dagger 
        $trans[chr(136)] = '&circ;';    // Modifier Letter Circumflex Accent 
        $trans[chr(137)] = '&permil;';    // Per Mille Sign 
        $trans[chr(138)] = '&Scaron;';    // Latin Capital Letter S With Caron 
        $trans[chr(139)] = '&lsaquo;';    // Single Left-Pointing Angle Quotation Mark 
        $trans[chr(140)] = '&OElig;';    // Latin Capital Ligature OE 
        $trans[chr(145)] = '&lsquo;';    // Left Single Quotation Mark 
        $trans[chr(146)] = '&rsquo;';    // Right Single Quotation Mark 
        $trans[chr(147)] = '&ldquo;';    // Left Double Quotation Mark 
        $trans[chr(148)] = '&rdquo;';    // Right Double Quotation Mark 
        $trans[chr(149)] = '&bull;';    // Bullet
 
Last edited by Benjamin on Tue Jun 23, 2009 11:33 pm, edited 1 time in total.
Reason: Added [code=php] tags.
shanecody
Forum Newbie
Posts: 23
Joined: Fri May 08, 2009 4:12 pm

Re: Weid characters in mysql output

Post by shanecody »

IT worked!
Thank you so much!

here is the function I created so if anyone else has this problem

Code: Select all

 
    function replace_bad_chars($str_to_replace){
        $trans = array(chr(130) => '&sbquo;', // Single Low-9 Quotation Mark 
                    chr(131) => '&fnof;', // Latin Small Letter F With Hook
                    chr(132) => '&bdquo;', // Double Low-9 Quotation Mark
                    chr(133) => '&hellip;', // Horizontal Ellipsis 
                    chr(134) => '&dagger;', // Dagger 
                    chr(135) => '&Dagger;', // Double Dagger 
                    chr(136) => '&circ;', // Modifier Letter Circumflex Accent 
                    chr(137) => '&permil;', // Per Mille Sign 
                    chr(138) => '&Scaron;', // Latin Capital Letter S With Caron 
                    chr(139) => '&lsaquo;', // Single Left-Pointing Angle Quotation Mark
                    chr(140) => '&OElig;', // Latin Capital Ligature OE 
                    chr(145) => '&lsquo;', // Left Single Quotation Mark 
                    chr(146) => '&rsquo;', // Right Single Quotation Mark
                    chr(147) => '&ldquo;', // Left Double Quotation Mark
                    chr(148) => '&rdquo;', // Right Double Quotation Mark 
                    chr(149) => '&bull;'); // Bullet
    
        foreach($trans as $char => $value){
            $str_to_replace = str_replace($char, $value, $str_to_replace);
        }
        return $str_to_replace;
    }
 
Last edited by Benjamin on Tue Jun 23, 2009 11:34 pm, edited 1 time in total.
Reason: Added [code=php] tags.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Weid characters in mysql output

Post by Benjamin »

If I continue to see large blocks of code such as this posted without

Code: Select all

tags I will start issuing warnings per occurrence.  @Eric!, you have been around long enough to know better.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Weid characters in mysql output

Post by Eric! »

Good to hear. I don't know why the PHP function isn't complete. I read somewhere that there are only about 100 translations in the php tables and there are about 250 html codes.

Here's the link where I got the translations I posted.

http://php.net/manual/en/function.get-h ... .php#76564

I shortened the list for the purpose of the post.
Post Reply