每日学习笔记(2)
1,使用python提交post请求时,如果参数中包含中文,则会提交失败,因此需要将参数进行utf-8编码,示例如下:
2,一直在linux下使用python2.4,习惯了print 'hello'这样的写法,今天换到windows下,并且安装了python3.1后发现print是一个函数,因此要写成print('hello'),悲剧,但目前项目中都还是用的老旧的语法,还是得继续2.4才好。
3,python中序列化及反序列化一个对象,

def serializeObject(obj, filePath):
output = open(filePath, 'wb') #二进制写模式打开
pickle.dump(obj, output) #序列化
output.close()
def deSerializeObject(filePath):
try:
in = open(filePath, 'rb') #二进制读模式打开
obj = pickle.load(in) #反序列化
return obj
except EOFError:
return None
except IOError:
return None
4,使用trigger_error而不是die,这样对用户来说更加友好
5,mysql_query对于select等返回resultset的sql语句来说,运行成功返回一个resource,发生错误则返回FALSE,而对于insert,update,
delete,drop等sql语句,运行成功返回TRUE,发生错误则返回FALSE;

// This could be supplied by a user, for example
$firstname = 'fred';
$lastname = 'fox';
// Formulate Query
// This is the best way to perform a SQL query
// For more examples, see mysql_real_escape_string()
$query = sprintf("SELECT firstname, lastname, address, age FROM friends WHERE firstname='%s' AND lastname='%s'",
mysql_real_escape_string($firstname),
mysql_real_escape_string($lastname));
// Perform Query
$result = mysql_query($query);
// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
// Use result
// Attempting to print $result won't allow access to information in the resource
// One of the mysql result functions must be used
// See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc.
while ($row = mysql_fetch_assoc($result)) {
echo $row['firstname'];
echo $row['lastname'];
echo $row['address'];
echo $row['age'];
}
// Free the resources associated with the result set
// This is done automatically at the end of the script
mysql_free_result($result);
?>
6,mysql_fetch_array其实是取结果集的一行到一个数组中,它可以是一个关联数组(MYSQL_ASSOC)或数字数组(MYSQL_NUM),默认情况下两者都可以使用

mysql_connect("localhost", "mysql_user", "mysql_password") or
die("Could not connect: " . mysql_error());
mysql_select_db("mydb");
$result = mysql_query("SELECT id, name FROM mytable");
while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
printf ("ID: %s Name: %s", $row[0], $row["name"]);
}
mysql_free_result($result);
?>
与mysql_fetch_row相比,mysql_fetch_array也并不慢的

$result = mysql_query("SELECT id,email FROM people WHERE id = '42'");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
$row = mysql_fetch_row($result);
echo $row[0]; // 42
echo $row[1]; // the email value
?>
7,mysql_close()并不一定需要写,因为非持久的连接会在脚本执行完毕后自动关闭
作者:洞庭散人
出处:http://phinecos.cnblogs.com/
posted on 2010-04-28 20:31 Phinecos(洞庭散人) 阅读(708) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
2007-04-28 直线的生成算法