I’m new to PHP, HTML and MySQL, and have encountered the following problem:
I have a PHP document which outputs the results of a MySQL query. Because the MySQL database and thus the output results have some non-standard characters (such as æ or á), my PHP document and the MySQL database/tables are encoded as utf-8.
For instance, here is an example of the a database entry and correct output:
goahteæjgáda
When the PHP document does not have anything in the HTML <head/> node (not even comments), then the search is successful and the output is displayed correctly (but then I can’t apply my external css or include the shortcut icon, etc.).
However, if there is anything at all in the HTML <head/> node, such as standard metadata concerning content type, links to css and icon files, keywords, or even just <!-- comments -->, then either:
- the search does not work if the string being searched for contains a non-standard character
OR - any non-standard characters in the resulting output are displayed as � — for instance, the example above shows up like this after searching for “goahte”:
goahte�jg�da
Any help would be appreciated!
Here is my code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href='style_mavsulasj.css' rel='stylesheet' type='text/css'/>
<link rel='shortcut icon' href='farben4.gif'/>
<title>search</title>
</head>
<body>
<div style="width:230px;padding:0px;margin:0px;float:left;">
<table border="0">
<tr><td>Search here:</td><td>
<?php if (strlen($_GET['smj'])==0) echo ""; else echo "current search"; ?>
</td></tr>
<form action="" method="GET">
<tr><td colspan="2">Entry:</td></tr>
<tr><td><input type='text' name='smj' value=''></input></td><td align='center'><?php echo "<span class='searchCrits' > ".$_GET['smj']."</span>"; ?></td></tr>
<tr><td colspan="2"><input type="submit" value="submit query"/></td></tr>
</form>
</table>
</div>
<div style="width:960px;padding:30px;margin-left:210px;">
<?php
if($_GET){
$smj = $_GET['smj'];
$connect = mysql_connect("localhost","root","root");
if($connect){
$toDB = mysql_select_db("bigG_reimport_test",$connect);
if($toDB){
$query = "SELECT * FROM reimport_Sheet1 WHERE smj LIKE '" . $smj . "%'";
$results = mysql_query($query);
echo "<span class='header4'>results:<br/>";
while($row = mysql_fetch_array($results)){
echo "-> " . $row['smj'] . "<br/>" ;
}
echo "</span><br/>";
}
else {die("Failed to connect to database!<br/>" . mysql_error());}}
else {die("Failed to connect to mysql!<br/>" . mysql_error());}}
?>
</div>
</body>
</html>
Make sure: