如何使php函数包含mysql命令

I want to create a simple menu function which can call it example get_menu()

Here is my current code.

<?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 } ?>

How to do that in?

function get_menu() {
}

Let me know.

Here is the function for that:

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;
}

.

Some Quick Corrections In Your Script:

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

Suggestion:

You can give your menu links a class and style as per your menu needs :)

just put function get_menu() { above your code and } below

now here you already have a mistake:

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

notice the = after id

you can't be too careful

or like this??

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 );
    }

First, separate the part where you're doing something, and the one used to display things.

Second, the alternative syntax looks better for the display part.

<?php
function get_menu(){
  $items = array();
  $select = 'SELECT * FROM pages';
  $query  = $db->rq($select);
  while ($page = $db->fetch($query)) { 
    $items[] = $page['id'];
  }
  return $items;
}

$menuItems = get_menu();
?>
<ul>
<?php foreach($menuItems as $item): ?>
  <li><a href="page.php?id=<?php echo $item['id']; ?>" title="<?php echo $item['title']; ?>"><?php echo $item['title']; ?></a></li>
<?php endforeach;?>
</ul>

get_menu() has to get reference to $db somehow. Probably the best and easiest way is to pass that reference as parameter:

function get_menu(MyDatabaseHandler $db) {
  // code proposed by Sarfraz here
}
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;
}

The code Sarfraz posted is going to create invalid anchor tags (i.e. links). They'll also be missing names. Here is the shorter/faster version:

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;
}

To use that do this:

echo get_menu($db);

The error you were getting was probably resulting from not passing the database connection to the function.

NOTE: It's generally not a good idea to show database ID numbers to the user in the interest of security; slugs are much better for identifying pages and are SEO friendly. Also, there shouldn't be any need to pass the page title to page.php because if you've got the ID you can get that when you need it from the database. Here's the code with this in mind:

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;
}