Hi all,
My question put simply: I have a phpBB board on my server. I want to read the latest news announcement from it and display it on my web site's main page. Everything's fine and dandy; topic title, timestamps, reply counts etc. all work, except for the actual post contents.
Let's say the post text is something along the lines of "Lorem ipsum dolor sit amet, consectetur bla bla". What my code actually returns is not that but some garbled version like "57656C6C2C2049206775657373207468697320".
Code (see below) in short: Grab the last topic's (sorting by timestamp, descending, limit 1 = latest topic) metadata from the phpbb_topics table, get the post ID of the topic's first post, and then look that post's text up in the phpbb_posts table.
`phpbb_posts`.`post_text` is a mediumtext utf8_bin (this is the column where the reading apparently goes wrong). The only other text field I'm reading from is `phpbb_topics`.`topic_title`, which is a varchar(255) utf8_general_ci. I'm lead to believe that it has something to do with the post_text column having a binary collation, but I don't know how to fix the reading problem.
Using MySQL 5.
VB.NET code:
Public Function GetLastNewsPost() As System.Collections.Generic.List(Of Object) Dim Result As New System.Collections.Generic.List(Of Object) Dim NewsTitle As String = "", NewsID As Integer, NewsTime As Long, NewsReplies As Integer Dim NewsText As String = "", NewsFirstPostID As Integer Try ' Generate SQL database connection and open. Dim SQLConn As New OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=xxxxx;Uid=xxxxx;Pwd=xxxxx;") SQLConn.Open() Dim SQLCommand As New OdbcCommand("SET NAMES 'utf8';", SQLConn) SQLCommand.ExecuteNonQuery() SQLCommand.Dispose() ' Create a reader Dim Reader As OdbcDataReader = Nothing ' Create SQL query. SQLCommand = New OdbcCommand("SELECT `topic_id`, `topic_title`, `topic_time`, `topic_replies`, `topic_first_post_id` FROM `phpbb_topics` WHERE `forum_id`=2 ORDER BY `topic_time` DESC LIMIT 1", SQLConn) Try ' Generate a reader Reader = SQLCommand.ExecuteReader If Reader.HasRows AndAlso Reader.Read Then ' read only the latest post NewsID = Reader.GetInt32(0) NewsTitle = Reader.GetString(1) NewsTime = Reader.GetInt64(2) NewsReplies = Reader.GetInt32(3) NewsFirstPostID = Reader.GetInt32(4) SQLCommand.Dispose() Reader.Close() ' Create SQL query. SQLCommand = New OdbcCommand("SELECT `post_text` FROM `phpbb_posts` WHERE `post_id`=" & NewsFirstPostID & " LIMIT 1", SQLConn) Reader = SQLCommand.ExecuteReader If Reader.HasRows AndAlso Reader.Read Then NewsText = Reader.GetString(0) End If SQLCommand.Dispose() End If Finally SQLConn.Close() If Reader IsNot Nothing Then Reader.Close() End Try Catch ex As Exception ' DB error NewsTitle = "<Database error occurred: " & ex.GetType.FullName & ">" End Try Result.Add(NewsID) Result.Add(HttpUtility.HtmlEncode(NewsTitle)) Result.Add(NewsTime) Result.Add(NewsReplies) Result.Add(HttpUtility.HtmlEncode(NewsText)) Return Result End Function
If anyone could give me some ideas, that would be awesome.
Thanks!