我在制作mysql和php菜单系统时遇到了一些问题

时间:2018-12-22 16:07:41

标签: php mysql

我正在尝试使用php和mysql创建一个菜单系统,这是我的代码:

menu.php:

<?php include 'style/MySQL.php'; ?>
<div id="mySidenav" class="sidenav">
  <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
  <?php
   echo '<a href='get_menu_link('CPMenuSite')>get_menu_name('CPMenuSite')</a>';
   ?>
</div>

<!-- Use any element to open the sidenav -->
<div class="opennavdiv" onclick="openNav()">
<a href="#" class="opennavs">=</a>

</div>
  <script src="/site/style/menu.js"></script>

<!-- Add all page content inside this div if you want the side nav to push page content to the right (not used if you only want the sidenav to sit on top of the page -->

MySQL.php:

<?php
function get_mysql() {
  $mysql = new mysqli("localhost", "root", "ww", "site");

  if ($mysql->connect_error) {
    die($mysql->connect_error);
  }
  return $mysql;
}

function get_menu_link($menu) {
  return get_mysql()->query("select url from cppages where menu = $menu");
}

function get_menu_name($menu) {
  return get_mysql()->query("select name from cppages where menu = $menu");
}
?>

我的问题是:

  

解析错误:语法错误,意外的“ get_menu_link”(T_STRING),期望为“,”或“;”在第5行的C:\ xampp \ htdocs \ site \ menu.php中

我无法从数据库cppages中获取所有信息并将其放入<a href="link">name</a>中。

1 个答案:

答案 0 :(得分:1)

单引号中的问题 您以单引号'开始echo,然后在href中再次使用它,因此将代码更改为

echo '<a href="'.get_menu_link('CPMenuSite').'">'.get_menu_name('CPMenuSite').'</a>';

使用字符串而不是整数搜索时,您错过了在mysql查询中添加单引号的操作 where menu = '$menu' ")

现在您的代码可以正常工作了

相关问题