我在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 )
它只是表中的第一张记录。
帮助我。谢谢你。
答案 0 :(得分: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'];
}
}