我有两个文件名 func.inc.php 和 profile.php 我试图通过在 func.inc.php 中创建一个函数 get_data($id) 并在配置文件页面 (profile.php) 中显示来从 mysql 中获取数据。但是数据没有显示。
<-- func.inc.php 文件 -->
<?php session_start();
function get_data($id){
$query_in="SELECT * FROM user WHERE id ='$id'";
$query=mysql_query($query_in);
while($row=mysql_fetch_assoc($query)){
$name=$row['name'];
$book=$row['book'];
$mobile=$row['mobile'];
$computer=$row['computer'];
}
}
?>
<-- 配置文件.php -->
<?
include'func.inc.php';
echo "Name: ".$name;
echo "Book: ".$book;?>
最佳答案
函数应该返回一些值,然后只会得到函数的值
function get_data($id){
$query_in= sprintf("SELECT * FROM user WHERE id ='%d'", mysql_real_escape_string($id));
$query=mysql_query($query_in);
$result = array();
while($row=mysql_fetch_assoc($query)){
$result[]['name'] = $row['name'];
}
return $result;
}
<-- 配置文件.php -->
$values = get_data($id);
print_R($values);
关于php - 无法回显函数中获取的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13696116/