Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 无法回显函数中提取的变量_Php_Mysql_Function - Fatal编程技术网

Php 无法回显函数中提取的变量

Php 无法回显函数中提取的变量,php,mysql,function,Php,Mysql,Function,我有两个文件名func.inc.php和profile.php 我试图通过在func.inc.php中创建一个函数get_data($id)并在profile页面(profile.php)中显示来从mysql获取数据,但是数据没有显示 函数应该返回一些值,然后只从函数中获取值 function get_data($id){ $query_in= sprintf("SELECT * FROM user WHERE id ='%d'", mysql_real_escape_s

我有两个文件名func.inc.php和profile.php 我试图通过在func.inc.php中创建一个函数get_data($id)并在profile页面(profile.php)中显示来从mysql获取数据,但是数据没有显示



函数应该返回一些值,然后只从函数中获取值

   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;
    } 

您应该通过函数调用传递id,即:此处您应该这样调用:

$values = get_data($id);

 while($row=mysql_fetch_array($values)){
 echo $name=$row['name']."<br>";
 echo $book=$row['book']."<br>";
 echo $mobile=$row['mobile']."<br>";
 echo $computer=$row['computer']."<br>";
}

你的功能并没有返回任何东西,我很好奇为什么你要这样做,这是类似于试图把两个棍子很快地揉搓在一起,希望能生火,而不是使用一个合适的框架,或者给你一个建立的基础。@ Tabman你是对的,我刚刚意识到。谢谢你的帮助。我的意思不是说听起来很挑剔,但是用手做很难。我不怀念上世纪90年代末,那时你别无选择,只能这样做。谢天谢地,那些日子已经过去了。是的,你是绝对正确的,但我正在建设的网站只是出于爱好,所以没有使用任何框架。但是在你说了之后,我在谷歌上搜索了这些框架。我将尝试学习这些框架。请,请,请,在这个框架中添加
mysql\u real\u escape\u string
,或者使用mysqli或PDO。更好
   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;
    } 
$values = get_data($id);

print_R($values);
$values = get_data($id);

 while($row=mysql_fetch_array($values)){
 echo $name=$row['name']."<br>";
 echo $book=$row['book']."<br>";
 echo $mobile=$row['mobile']."<br>";
 echo $computer=$row['computer']."<br>";
}
function  get_data($id){
  $query_in="SELECT * FROM user WHERE id ='$id'";
  $query=mysql_query($query_in);
  return $query;
}