Php 从特定时间添加的MySQL获取信息
我创建了一个脚本来接收在特定时间添加到sql中的所有行,并设置变量,从每个新记录(行)中的特定列获取值。这就是我所做的:Php 从特定时间添加的MySQL获取信息,php,mysql,arrays,row,Php,Mysql,Arrays,Row,我创建了一个脚本来接收在特定时间添加到sql中的所有行,并设置变量,从每个新记录(行)中的特定列获取值。这就是我所做的: <?php header('Content-type: text/html; charset=utf-8'); $host='localhost'; $user='username'; $password='password'; $dbname='racheaqui'; $connection=mysql_connect('localhost:/var/run/my
<?php
header('Content-type: text/html; charset=utf-8');
$host='localhost';
$user='username';
$password='password';
$dbname='racheaqui';
$connection=mysql_connect('localhost:/var/run/mysqld/mysqld.sock',$user,$password) or die ("connection failed");
mysql_select_db($dbname, $connection);
$query = "SELECT * FROM store WHERE date >= NOW() - INTERVAL 20 MINUTE;";
$result=mysql_query($query, $conexao);
if ($result){
while($row = mysql_fetch_assoc($result)){
$email=$row['email_contact'];
$name=$row['name'];
$phone=$row['phone_contact'];
}
}
#if (!empty($name)) {
print_r("The user ".$name." with phone ".$phone." and e-mail: ".$email." has created an account in the system.\n");
mysql_close($connection);
#}
?>
但在这段代码中,我将只使用最后一行的值设置变量。我计划设置:$variable1,$variable2,$variable3,$variableX。其中$variableX是添加的行数(以数组格式接收)
你能给我一些帮助吗?我是php编码的新手,但相信我,在这里提问之前,我真的很努力,也在google和stackoverflow的数据库中搜索过
提前感谢。请注意:
while($row = mysql_fetch_assoc($result)){
$email=$row['email_contact'];
$name=$row['name'];
$phone=$row['phone_contact'];
}
您正在为每条记录覆盖$email
、$name
和$phone
。您可以将它们收集到单独的阵列中,如下所示:
$emails = array();
$names = array();
$phones = array();
while($row = mysql_fetch_assoc($result)){
$emails[]=$row['email_contact'];
$names[]=$row['name'];
$phones[]=$row['phone_contact'];
}
while($row = mysql_fetch_assoc($result)){
$email=$row['email_contact'];
$name=$row['name'];
$phone=$row['phone_contact'];
print_r("The user ".$name." with phone ".$phone." and e-mail: ".$email." has created an account in the system.\n");
}
或者为每个存储创建一个包含数据的大数组:
$data = array();
while($row = mysql_fetch_assoc($result)){
$data[] = array(
'email' => $row['email_contact'],
'name' => $row['name'],
'phone' => $row['phone_contact']
);
}
或者,您可以将print\u r()
放入循环中,如下所示:
$emails = array();
$names = array();
$phones = array();
while($row = mysql_fetch_assoc($result)){
$emails[]=$row['email_contact'];
$names[]=$row['name'];
$phones[]=$row['phone_contact'];
}
while($row = mysql_fetch_assoc($result)){
$email=$row['email_contact'];
$name=$row['name'];
$phone=$row['phone_contact'];
print_r("The user ".$name." with phone ".$phone." and e-mail: ".$email." has created an account in the system.\n");
}
每次通过while循环时都会覆盖变量。尝试将打印语句放入while循环中,如 打印$email。“
”; 打印$name。“
”; 打印$phone。“
”; 而不是使用打印
您可能希望在循环中放置一个计数器并将其打印出来。如果您想知道所选行的数量,请使用
mysql\u num\u rows
if ($result)
{
$nr_rows = mysql_num_rows($result);
while($row = mysql_fetch_assoc($result))
{
$email=$row['email_contact'];
$name=$row['name'];
$phone=$row['phone_contact'];
print "The user ".$name." with phone ".$phone." and e-mail: ".$email." has created an account in the system.\n";
}
print "Accounts created the last 20 minutes: ".$nr_rows;
}
尝试使用pdo,mysql很容易被贬损,@mopo922。在第一种情况下,如何从每个数组中提取值?
$emails[0]
,$emails[1]
,等等@Marcelo停止?