函数只返回一次,为什么? [英] function returning only once, why?
本文介绍了函数只返回一次,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我运行了一个
foreach
循环,每一个项目都必须从函数中获得一个特定的值。 但是我只有一个返回。我无法弄清楚发生了什么事。我希望你们一定会的。
下面是我的程序的简短版本。
数据库结构最后给出。
<?php
函数opendb(){
mysql_connect(localhost,root,root);
mysql_select_db(something_db);
函数sql_query($ sql){
$ datas = array();
if($ res = mysql_query($ sql)){
$ x = 0;
while($ data = mysql_fetch_assoc($ res)){
$ datas [$ x] = $ data;
$ x + = 1;
}
}
返回$ datas;
$ b $ SELECT * FROM $ table WHERE id = $ cid;
$ datas = sql_query($ sql);
$ pid = $ datas [0] ['parent'];
$ p_id = $ datas [0] ['id'];
if($ pid!= 0){
get_parent_id($ table,$ parent,$ pid);
} else {
return $ p_id;
opendb();
$ datas_pkg = sql_query(SELECT * FROM tbl_packages WHERE 1);
foreach($ datas_pkg as $ data_pkg){
echo $ data_pkg ['destination_id']。 - >;
echo $ parent_id = get_parent_id('tbl_destinations','parent',$ data_pkg ['destination_id']);
echo'< br />';
}
?>
数据库结构..
+ -------- + ----------------- -------- + ----------- +
| ID(INT)| DESTINATION_NAME(VARCHAR)|父(INT)|
+ -------- + ------------------------- + ---------- - +
+ ------- + --------------------- + ------------- ------ +
| id(int)| package_name(varchar)| destination_id(int)|
+ ------- + --------------------- + --------------- ---- +
如果我不清楚我的问题,请让我知道,以便我可以帮助($ pid!= 0)$ b $
解决方案 b {
get_parent_id($ table,$ parent,$ pid);
get_parent_id($ table,$ parent,$ pid);
您可以调用该函数,但不要使用它的值。 / p>
During my coding I really got stuck into this problem.
I ran a foreach
loop and for every item I had to get a certain value from a function.
But I got only one returned. I could not figure out what was happening. I hope you guys surely will.
Below is the short version of my program.
Database structure is given at last.
<?php
function opendb() {
mysql_connect("localhost", "root", "root");
mysql_select_db("something_db");
}
function sql_query($sql) {
$datas = array();
if ($res = mysql_query($sql)) {
$x = 0;
while ( $data = mysql_fetch_assoc($res) ) {
$datas[$x] = $data;
$x += 1;
}
}
return $datas;
}
function get_parent_id($table, $parent, $cid) {
// cid=>child id
$sql = "SELECT * FROM $table WHERE id=$cid";
$datas = sql_query($sql);
$pid = $datas[0]['parent'];
$p_id = $datas[0]['id'];
if ($pid != 0) {
get_parent_id($table, $parent, $pid);
} else {
return $p_id;
}
}
opendb();
$datas_pkg = sql_query("SELECT * FROM tbl_packages WHERE 1");
foreach ( $datas_pkg as $data_pkg ) {
echo $data_pkg['destination_id'] . '-->';
echo $parent_id = get_parent_id('tbl_destinations', 'parent', $data_pkg['destination_id']);
echo '<br/>';
}
?>
Database structure..
tbl_destinations
+--------+-------------------------+-----------+
| id(int)|destination_name(Varchar)|parent(int)|
+--------+-------------------------+-----------+
tbl_packages
+-------+---------------------+-------------------+
|id(int)|package_name(varchar)|destination_id(int)|
+-------+---------------------+-------------------+
If I did not clear my question please let me know so that I can help you to help me.
解决方案
if($pid!=0)
{
get_parent_id($table,$parent,$pid);
}
You call the function, but never use its value.
这篇关于函数只返回一次,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文