[英]How to pass the fetch data from table from one function to another function in php
I have a table named as tab2
in mysql. 我在mysql中有一个名为
tab2
的表。 It contains name and phone fields. 它包含名称和电话字段。
I have created a function named as gettabledata()
: 我创建了一个名为
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'];
}
}
After Update I am getting warning like: 更新后,我收到警告,例如:
`
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 `
Also on doing print_r($gettab)
, I got: 另外在做
print_r($gettab)
,我得到了:
Array ( [name] => [phone] => 0 )
Its just first record from table. 它只是表中的第一条记录。
Help me with this.Thanks in anticipation. 帮我解决这个问题。感谢您的期待。
You just need to add a return statement at the end of the 您只需要在末尾添加return语句即可
gettabledata()
gettabledata()
Modify the function with return statement as follows. 使用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.