纠正pdo连接以使用重音符号

时间:2013-09-25 16:29:43

标签: mysql pdo non-ascii-characters

您好我在PDO连接上显示重音符号时遇到问题。 我正在学习php开发web,我开始使用mysql旧的php连接开发

function db_connect()
{
   $result = new mysqli('localhost', 'database', 'password', 'user'); 
   if (!$result)
     return false;
   return $result;
}

我的表使用latin1和utf8字符集,网页工作正常,显示带重音的单词。

我知道charset和collat​​ion是好的,因为它以旧方式工作。 但是当我尝试使用PDO时,带有重音的所有单词都会有所不同。 以下是选择类别名称

的示例
$db = database_connect();
$query = " select xxx from yyy where xxxyid = :xxxyid ";  
$query_params = array( 
    ':xxxyid' => $xxxyid
    );
// Execute the query against the database 
$query = $db->prepare($query);
$result = $query->execute($query_params); 
if (!$result) 
  trigger_error('Query failed: ' . mysql_error(), E_USER_ERROR); 
$num_xxx = $query->rowCount();
if ($num_xxxs == 0)
  return false;  
$row = $query->fetch();
return $row['name'];

我的连接以这种方式设置

$db_options = array( 
    PDO::ATTR_EMULATE_PREPARES => false                     // important! use actual prepared statements (default: emulate prepared statements) 
    , PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION           // throw exceptions on errors (default: stay silent) 
    , PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC      // fetch associative arrays (default: mixed arrays) 
); 
$database = new PDO('mysql:host=server;dbname=dbname;charset=utf8', 'user', 'password', $db_options);    // important! specify the character encoding in the DSN string, don't use SET NAMES
// create prepared statement with one parameter for the category ID 

我试图找到一种方法来显示带重音的字母,但我无法解决它。

2 个答案:

答案 0 :(得分:0)

我在您发布的同一链接中找到了解决方案。此解决方案对我有用:

http://forums.devshed.com/php-faqs-stickies/938827-pdo-security-php-5-3-6-a-post2851557.html#post2851557

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Movie_Vault"
        xmlns:Controls="clr-namespace:System.Windows.Controls;assembly=DotNetProjects.Layout.Toolkit" xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit" x:Class="Movie_Vault.frmStatistics"
        mc:Ignorable="d"
        Title="frmStatistics" Style="{StaticResource RoundedFormStyle}" WindowStartupLocation="CenterOwner">
    <Border Style="{StaticResource WindowBorderStyle}">
        <DockPanel x:Name="OuterPanel" >
            <Border Style="{StaticResource PanelBorderStyle}" DockPanel.Dock="Top">
                <DockPanel x:Name="TopPanel" HorizontalAlignment="Stretch" VerticalAlignment="Top">
                    <Image Height="16" Margin="8,4,8,4" Source="Images/process.png"/>
                    <TextBlock Style="{StaticResource MediumFont}"><Run Text="Statistics"/></TextBlock>
                    <DockPanel DockPanel.Dock="Right" HorizontalAlignment="Right" VerticalAlignment="Center">
                        <Button x:Name="btnClose" Content="X" Style="{StaticResource MicroButtonStyle}" Margin="0,0,8,0" Click="btnClose_Click"/>
                    </DockPanel>
                </DockPanel>
            </Border>
            <Border Style="{StaticResource PanelBorderStyle}" DockPanel.Dock="Left" Margin="0,8,0,0">
                <DockPanel VerticalAlignment="Top"  >
                    <ScrollViewer VerticalScrollBarVisibility="Auto" Height="400" Width="500">
                        <Grid Name="panelTopGenres">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="*" />
                                <RowDefinition Height="Auto" />
                            </Grid.RowDefinitions>
                            <toolkit:Chart Name="chartTopGenres" Title="Top Genres" VerticalAlignment="Top" 
                   Margin="16,16,16,8" Padding="8" Background="White" >
                                <toolkit:BarSeries DependentValuePath="Value" IndependentValuePath="Key" 
                           ItemsSource="{Binding}" 
                           IsSelectionEnabled="False" Background="White"/>
                            </toolkit:Chart>
                            <Button Grid.Row="1" x:Name="btnTopGenresToggle" Style="{StaticResource SmallButtonStyle}" 
            Content="Show All Genres" Width="120" 
            Margin="16,0,4,4" HorizontalAlignment="Left" Click="btnToggle_Click" />
                        </Grid>
                    </ScrollViewer>
                </DockPanel>
            </Border>
        </DockPanel>
    </Border>
</Window>

答案 1 :(得分:-1)

我的解决方案完全不同。没有PDO的任何新选项等等....我的问题是当我通过phpadmin或脚本填写db的值时,总是存在这种问题。但是当我在电子表格软件中填充db表然后将填充的文件导入db时,问题就解决了。这种知识花了我一天的生命。希望能帮助到你。对不起我的英语,我是斯洛伐克人。