在php变量中输出mysql查询结果

时间:2021-09-23 15:43:47

I have this variable ($sql) in PHP:

我在PHP中有这个变量($ sql):

$sql = "SELECT DATE_FORMAT(FROM_DAYS(DATEDIFF(CURRENT_DATE, dob)),'%y Years %m Months %d Days') AS age WHERE id=$row[id]";

How to output the produced data as a PHP variable?

如何将生成的数据输出为PHP变量?

The query is taking the date of birth from my table and calculating the age from today's date.

该查询从我的表中获取出生日期并计算从今天开始的年龄。

How to echo the age in PHP?

如何在PHP中回应年龄?

3 个解决方案

#1


3  

Just try this:

试试这个:

$con = mysqli_connect("localhost","user","pass", "database_name"); //your connection
$sql = "SELECT DATE_FORMAT(FROM_DAYS(DATEDIFF(CURRENT_DATE, dob)),'%y Years %m Months %d Days') AS age FROM table_name WHERE id=".$row['id'];    
$query = mysqli_query($con, $sql);
$result = mysqli_fetch_assoc($query);
echo $result['age'];

Don't forget to replace table_name.

不要忘记替换table_name。

#2


0  

try this code

试试这段代码

i see your code you miss table name

我看到你错过了表名的代码

$sql = "SELECT DATE_FORMAT(FROM_DAYS(DATEDIFF(CURRENT_DATE, dob)),'%y Years %m Months %d Days') AS age WHERE id=$row[id]";

 $result = mysql_query($sql);
    while($query_data = mysql_fetch_row($result))
    $age= $query_data[0];
    print $age;

#3


0  

You can try as below:

你可以尝试如下:

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

$sql = "SELECT DATE_FORMAT(FROM_DAYS(DATEDIFF(CURRENT_DATE, dob)),'%y Years %m Months %d Days') AS age WHERE id=$row[id]";

$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
    echo "age: " . $row["age"]. "<br>";}
} 
else {
echo "No result!";
}

You can reference from http://www.w3schools.com

您可以参考http://www.w3schools.com

#1


3  

Just try this:

试试这个:

$con = mysqli_connect("localhost","user","pass", "database_name"); //your connection
$sql = "SELECT DATE_FORMAT(FROM_DAYS(DATEDIFF(CURRENT_DATE, dob)),'%y Years %m Months %d Days') AS age FROM table_name WHERE id=".$row['id'];    
$query = mysqli_query($con, $sql);
$result = mysqli_fetch_assoc($query);
echo $result['age'];

Don't forget to replace table_name.

不要忘记替换table_name。

#2


0  

try this code

试试这段代码

i see your code you miss table name

我看到你错过了表名的代码

$sql = "SELECT DATE_FORMAT(FROM_DAYS(DATEDIFF(CURRENT_DATE, dob)),'%y Years %m Months %d Days') AS age WHERE id=$row[id]";

 $result = mysql_query($sql);
    while($query_data = mysql_fetch_row($result))
    $age= $query_data[0];
    print $age;

#3


0  

You can try as below:

你可以尝试如下:

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

$sql = "SELECT DATE_FORMAT(FROM_DAYS(DATEDIFF(CURRENT_DATE, dob)),'%y Years %m Months %d Days') AS age WHERE id=$row[id]";

$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
    echo "age: " . $row["age"]. "<br>";}
} 
else {
echo "No result!";
}

You can reference from http://www.w3schools.com

您可以参考http://www.w3schools.com