my php function
我的php函数
<?php
function handleException($ex)
{
header('HTTP/1.1 500 Internal Server Error');
echo 'Internal error';
}
set_exception_handler('handleException');
// we are using PDO - easier to use as mysqli and much better than the mysql extension (which will be removed in the next versions of PHP)
try
{
$password = 'root';
$db = new PDO('mysql:host=localhost;dbname=finance', "root", $password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
mysql_set_charset('utf8');
// note the quote thing - this prevents your script from sql injection
mysql_query('SET NAMES utf8;');
$data = $db->query("SELECT customer_name FROM customer where customer_id = " . $db->quote($_GET["q"]));
$customer_name = array();
foreach ($data as $row) {
$customer_name[] = $row["customer_name"];
}
print json_encode(array(
"customer_name" => $customer_name,
"anotherReturnValue" => "just a demo how to return more stuff")
);
} catch (PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
?>
and my php form file
还有php表单文件
<?php include('header.php');
include('config.php');
<div>
<form id="form" method="post" action="functions/adduser.php" enctype="multipart/form-data" >
<table>
<tr><td>உ.எண்</td>
<td>
<select id="sno" name="sno" >
<option>உ.எண்</option>
<?php while($row=mysql_fetch_array($result))
{?>
<option value="<?php echo $id=$row['customer_id'];?>" id="sno"><?php echo $id=$row['customer_id'];?></option><?php }?>
</select>
<td><div id="list"></td></tr>
<tr>
<td><label>பெயர்</label></td>
<td>
<input type="text" value="" name="name" id="" class="text">
</td>
</tr>
<tr>
<td><label>ஊர்</label></td>
<td>
<input type="text" value="" name="city" class="text">
</td>
</tr>
<tr>
<tr>
<td><label>பற்று</label></td>
<td>
<input type="text" class="text" name="loan"></textarea>
</td>
</tr>
<tr>
<td><label>ஆரம்பம் தேதி</label></td>
<td>
<input type="text" class="text" name="" value="<?php echo date('Y-m-d'); ?>" />
</td>
</tr>
<tr>
<td><label>முடிவு தேதி</label></td>
<td>
<input type="text" class="text" name="" value="<?php echo $end_date; ?>" />
</td>
</tr>
<tr><td><input type="button" value="Save" id="save" name="save" class="text"></td></tr>
<tr><td><div id="error"></div></div></td></tr>
</table>
</form>
</div>
<script tyep="text/javascript" src="js/jquery-1.6.1.min.js"></script>
<script type="text/javascript" charset="UTF-8">
$(document).ready(function () {
// if user chooses an option from the select box...
$("#sno").change(function () {
// get selected value from selectbox with id #department_list
var cusno = $(this).val();
$.ajax({
url: "functions/get_name.php",
data: "q=" + cusno,
contentType: "application/json; charset=utf-8",
dataType: "json",
// if successful
success: function (response, textStatus, jqXHR) {
// no teachers found -> an empty array was returned from the backend
if (response.customer_name.length == 0) {
$('#result').html("nothing found");
}
else {
// backend returned an array of names
var list = $("#list");
//remove items from previous searches from the result list
$('#list').empty();
//append each teachername to the list and wrap in <li>
$.each(response.customer_name, function (i, val) {
//list.append($("<li>" + val + "</li>"));
});
}
$.each(response.customer_name, function (i, val) {
});
}});
});
// if anywhere in our application happens an ajax error,this function will catch it
// and show an error message to the user
$(document).ajaxError(function (e, xhr, settings, exception) {
$("#error").html("<div class='alert alert-warning'> Uups, an error occurred.</div>");
});
});
</script>
</script>
<?php include('footer.php');?>
3 个解决方案
#1
1
Have you set correct charset in your database?
你在数据库中设置了正确的字符集吗?
Or there may be Problem with Connection. Try set Connection to UTF-8
或者可能是连接有问题。尝试将连接设置为UTF-8
mysql_query("SET NAMES utf8");
In PDO you can directly set charset.
在PDO中,可以直接设置字符集。
"mysql:host=$host;dbname=$db;charset=utf8"
字符集,设置名称?
#2
1
You can't just use utf-8 encoding, it depends on the character set in your database.
不能只使用utf-8编码,这取决于数据库中的字符集。
Do SHOW CHARACTER SET FOR mydatabase;
first, then also SHOW CHARACTER SET FOR mydatabase.mytable;
for the table you are accessing data from to get the proper character set.
显示mydatabase的字符集;首先,还要显示mydatabase.mytable的字符集;对于表,您正在访问数据,以获得适当的字符集。
Only then can you know the correct encoding to use which may resolve your issue and set it accordingly.
只有这样,您才能知道要使用的正确编码,从而解决您的问题并相应地设置它。
#3
1
you should probably missing the valid meta tag in the html template, try adding this
您可能会在html模板中丢失有效的meta标记,尝试添加它
<html lang="en">
<head>
<meta charset="utf-8">
and since you use the PDO, why on earth you still use mysql_* extension anymore? you should set the correct charset like this
既然使用了PDO,为什么还在使用mysql_*扩展?您应该像这样设置正确的字符集
$db = new PDO('mysql:charset=utf8mb4;host=localhost;dbname=finance', "root", $password);
make sure that your tables have been set to the utf8mb4 (not utf8) collate/charset
确保您的表已设置为utf8mb4(不是utf8)排序规则/字符集
for better understanding and to make the application utf8 compliant
为了更好地理解和使应用程序与utf8兼容
-
utf-8-all-the-way-through
-
绝对最小的软件开发人员绝对必须了解Unicode和字符集(没有借口!)
#1
1
Have you set correct charset in your database?
你在数据库中设置了正确的字符集吗?
Or there may be Problem with Connection. Try set Connection to UTF-8
或者可能是连接有问题。尝试将连接设置为UTF-8
mysql_query("SET NAMES utf8");
In PDO you can directly set charset.
在PDO中,可以直接设置字符集。
"mysql:host=$host;dbname=$db;charset=utf8"
字符集,设置名称?
#2
1
You can't just use utf-8 encoding, it depends on the character set in your database.
不能只使用utf-8编码,这取决于数据库中的字符集。
Do SHOW CHARACTER SET FOR mydatabase;
first, then also SHOW CHARACTER SET FOR mydatabase.mytable;
for the table you are accessing data from to get the proper character set.
显示mydatabase的字符集;首先,还要显示mydatabase.mytable的字符集;对于表,您正在访问数据,以获得适当的字符集。
Only then can you know the correct encoding to use which may resolve your issue and set it accordingly.
只有这样,您才能知道要使用的正确编码,从而解决您的问题并相应地设置它。
#3
1
you should probably missing the valid meta tag in the html template, try adding this
您可能会在html模板中丢失有效的meta标记,尝试添加它
<html lang="en">
<head>
<meta charset="utf-8">
and since you use the PDO, why on earth you still use mysql_* extension anymore? you should set the correct charset like this
既然使用了PDO,为什么还在使用mysql_*扩展?您应该像这样设置正确的字符集
$db = new PDO('mysql:charset=utf8mb4;host=localhost;dbname=finance', "root", $password);
make sure that your tables have been set to the utf8mb4 (not utf8) collate/charset
确保您的表已设置为utf8mb4(不是utf8)排序规则/字符集
for better understanding and to make the application utf8 compliant
为了更好地理解和使应用程序与utf8兼容
-
utf-8-all-the-way-through
-
绝对最小的软件开发人员绝对必须了解Unicode和字符集(没有借口!)