我知道它可能有点旧,但我需要声明一个变量,否则这就是我的想法。
基本上我收到了一个表名请求,我需要从日期列中获取最后一个值。我在 phpMyAdmin 中所做的。现在我需要提取那个确切的值并将其用作 php 中的变量。
<?php
include 'mydb.php';
require_once 'init.php';
$user_id = $user->data()->id ;
$data = mysql_query("SELECT date FROM request WHERE user_id= $user_id ORDER BY latest_request DESC LIMIT 1") or die(mysql_error());
$test = array();
while($row = mysql_fetch_array( $data ))
{
echo "<td>" .$row['date']. "</td>" ;
}
?>
<?php
$current_date = echo "<td> .$row['date']. "</td>" ;
// result im expecing is 2016-10-17 23:53:48
// echo " $current_date" --- will be 2016-10-17 23:53:48
>?
但是我得到的只是一个空值,我怎样才能做到这一点
最佳答案
如果你只想有一个,最后一行,那么也许更好的主意是这样的:
$sql = 'SELECT date FROM request WHERE user_id= $user_id ORDER BY latest_request DESC LIMIT 1';
$result = mysql_query($sql, $yourDBCon) or die(mysql_error());
$row = mysql_fetch_assoc($result);
print_r($row);
此解决方案只为您带来一行。
还要确保你从 sql 语句中得到任何东西。
关于php - 如何从 mysql 获取结果中声明一个 php 变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40295708/