English 中文(简体)
如何使营地职能包含我的指挥。
原标题:How to make a php function contain mysql commands
  • 时间:2010-04-14 13:16:15
  •  标签:
  • php
  • function

我想创设一个简单的菜单功能,可以称之为get_menu(

这里是我目前的法典。

<?php 
$select =  SELECT * FROM pages ;
$query  = $db->rq($select);
while ($page = $db->fetch($query)) { 
$id = $page[ id ];
$title = $page[ title ];
?>
<a href="page.php?id=<?php echo $id; ?>" title="<?php echo $title; ?>"><?php echo $title; ?></a>
<?php } ?>

如何做到这一点?

function get_menu() {
}

让我知道。

最佳回答

这里的职能是:

function get_menu(&$db)
{
    $select =  SELECT * FROM pages ;
    $query  = $db->rq($select);
    $menu =   ;

    while ($page = $db->fetch($query)) { 
    $id = $page[ id ];
    $title = $page[ title ];

    $menu 。=  <a href="page。php?id=  。 $id 。  " &title="  。 $title 。 ></a> 
    }

    return $menu;
}

www.un.org/Depts/DGACM/index_spanish.htm 页: 1

  • You were missing = after id
  • You were missing & after title

<>Suggestion:

您可以按照您的男性需要,把一等和风格联系起来。

问题回答

<代码>get_menu(,必须参考$db。 最好和最容易的方式是将这一提法作为参数:

function get_menu(MyDatabaseHandler $db) {
  // code proposed by Sarfraz here
}

现在你已经犯了错误:

<a href="page.php?id=<?php echo $id; ?>" title="<?php echo $title; ?>"><?php echo $title; ?></a>

<代码><>>>>后>

你不能太谨慎

登出的《Safraz法典》将产生无效的主轴(即链接)。 他们也是失踪的人。 下面是短/短文:

function get_menu($db)
{
    $result = $db->rq( SELECT id,title FROM pages );
    $menu =   ;

    while ($page = $db->fetch($result))
    { 
        $id = $page[ id ];
        $title = $page[ title ];

        $menu .= "<a href= page.php?id={$id}&title={$title} >{$title}</a>
";
    }

    return $menu;
}

为此:

echo get_menu($db);

您发现的错误很可能是由于没有将数据库连接功能。

注:为了安全起见,一般不宜向用户显示数据库识别号码;用于确定网页的插图更好,而且对地静止轨道友好。 此外,没有必要将页标题改为网页。 php是因为,如果你走过这个身份证,你就可以从数据库中获取。 这里要牢记:

function get_menu($db)
{
    $result = $db->rq( SELECT id,title FROM pages );
    $menu =   ;

    while ($page = $db->fetch($result))
    { 
        $menu .= "<a href= page.php?id={$page[ id ]} >{$page[ title ]}</a>
";
    }

    return $menu;
}

∗∗∗∗∗∗

还是这样?

function get_menu( $title, $id ) {
    $menu =   ;

    $menu .=  <a href="page.php?id  . $id .  " title="  . $title . ></a> 

    echo $menu;
}
------------------------
   $select =  SELECT * FROM pages ;
    $query  = $db->rq($select);
    while ($page = $db->fetch($query)) { 
        $id = $page[ id ];
        $title = $page[ title ];

        get_menu($title, $id );
    }
function getMenu()
{
    $select =  SELECT FROM pages ;
    $query  = $db->rq($select);
    $menu = new Array;

    while ($page = $db->fetch($query)) { 
       $menu[] =  <a href="page.php?id=  . $page[ id ] .  &title=  . $page[ title ] . "></a> ;
    }

    return $menu;
}




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签