[英]How to pass the fetch data from table from one function to another function in php
我在mysql中有一个名为tab2
的表。 它包含名称和电话字段。
我创建了一个名为gettabledata()
的函数:
function gettabledata()
{
$data = array();
$query = mysql_query("SELECT * FROM tab2");
$row = mysql_fetch_assoc($query);
//Here I am unable to get that how can I pass the fetched data in row
// variable to anywhere this function called.
// After update:
return $row;
}
if(isset($_POST['action']))
{
$gettab = createtable();//calling the function gettabledata
//Here I want to get the all data from that table
// After doing Update:
foreach($gettab as $row){
echo $row['name'];
echo $row['phone'];
}
}
更新后,我收到警告,例如:
`
Warning: Illegal string offset 'name' in E:\xampp\htdocs\practive\csvtest.php on line 29
Notice: Uninitialized string offset: 0 in E:\xampp\htdocs\practive\csvtest.php on line 29
Warning: Illegal string offset 'phone' in E:\xampp\htdocs\practive\csvtest.php on line 30
Notice: Uninitialized string offset: 0 in E:\xampp\htdocs\practive\csvtest.php on line 30
Warning: Illegal string offset 'name' in E:\xampp\htdocs\practive\csvtest.php on line 29
0
Warning: Illegal string offset 'phone' in E:\xampp\htdocs\practive\csvtest.php on line 30 `
另外在做print_r($gettab)
,我得到了:
Array ( [name] => [phone] => 0 )
它只是表中的第一条记录。
帮我解决这个问题。感谢您的期待。
您只需要在末尾添加return语句即可
gettabledata()
使用return语句修改函数,如下所示。
function gettabledata()
{
$query = mysql_query("SELECT * FROM tab2");
$row = mysql_fetch_assoc($query);
return $row;
}
if(isset($_POST['action']))
{
$row_arr = gettabledata();//calling the function gettabledata
foreach($row_arr as $row){
echo $row['id'];
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.