[英]UNIX_TIMESTAMP usage query
in my php5 code i want to use the UNIX_TIMESTAMP function to convert a datetime to a unix timestamp without using php code to convert as i believe this way is quicker but how do you then get the unix timestamp out of the rowset ie how to index into the rowset to get the integer unix timestamp out 在我的php5代码中,我想使用UNIX_TIMESTAMP函数将日期时间转换为unix时间戳,而无需使用php代码进行转换,因为我认为这种方式更快,但是如何从行集中获取unix时间戳,即如何索引到行集以获取整数unix时间戳
Below is a summary of the database schema 以下是数据库架构的摘要
age int(5)
user varchar(256)
created timestamp CURRENT_TIMESTAMP
$query="SELECT user, UNIX_TIMESTAMP(created) FROM accounts WHERE age='$age'" ;
$result = mysql_query($query) or die ('Error querying domain');
while($row = mysql_fetch_array($result))
{
$callerid = $row['callerid'];
$created_ts = $row['created'];
etc....
Or use AS
clause to give your column more descriptive name like this: 或使用
AS
子句为您的列提供更具描述性的名称,如下所示:
UNIX_TIMESTAMP(created) AS ucreated
And you can get it like: 您可以像这样获得它:
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$created_ts = $row['ucreated'];
............
More Info: 更多信息:
使用别名SELECT user, UNIX_TIMESTAMP(created) AS created FROM accounts
By default (MYSQL_BOTH), mysql_fetch_array
gives you both numeric and associative indices. 默认情况下(MYSQL_BOTH),
mysql_fetch_array
为您提供数字索引和关联索引。 So you can do: 所以你可以这样做:
$created_ts = $row[1];
Or, give it a name: 或者,给它起一个名字:
$query="SELECT user, UNIX_TIMESTAMP(created) AS created_ts FROM accounts WHERE age='$age';";
// ...
$created_ts = $row['created_ts'];
Also note that if age
comes from user input you should use mysql_real_escape_string
before concatenating it. 还要注意,如果
age
来自用户输入,则应在连接前使用mysql_real_escape_string
。 Or switch to a framework (like PDO or MySQLi) that supports prepared statements. 或切换到支持预准备语句的框架(如PDO或MySQLi)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.