donglian2106 2015-03-01 17:52
浏览 28

包含文件和函数中的MySQLi Connections

I've started converting all my old websites from MySQL to MySQLi. The issue I'm having is that some functions I use have stopped working and also files that are "included" or use a config file (with the connection) are not successfully connecting. I found an answer on here on how to make a connection.

EXAMPLE

config.php

class DBi {
    public static $db;
}

DBi::$db = new mysqli('host', 'uname', 'pword', 'database');

if ($db->connect_error) {
  die('Connect Error (' . $db->connect_errno . ') '
  .$db->connect_error);
}

search-autocomplete-business-names.php

<?

require_once ('config.php');

if (!isset($_REQUEST['term'])) {

  exit;

} else {

  $rs = DBi::$db->query('SELECT...');

  $data = array();

  if ($rs && $rs->num_rows) {

    while($row = $rs->fetch_assoc()) {

      $busname = htmlspecialchars_decode($row['BName'], ENT_QUOTES);
      $label = $busname;
      $data[] = array(
        'label' =>  $label,
        'value' => $row['BName']

      );

   }

}

echo json_encode($data);
flush();

}

?>

Javascript that uses the above include

<script>
<!-- business name autocomplete -->
$(function() {
  $(".src-bn").autocomplete({
    type: "POST",
    source: "lib/search-autocomplete-business-names.php",
    minLength: 1,
    focus: function(e, t) {
      return $(".src-bn").val(t.item.label), $(".src-spc").val(t.item.value), !1
    },
    select: function(e, t) {
      return $(".src-bn").val(t.item.label), $(".src-spc").val(t.item.value), !1
      }
    }).data("ui-autocomplete")._renderItem = function(e, t) {
      return $("<li>").append("<a>" + t.label + "</a>").appendTo(e)
    }
  });
</script>

What I want is to be able to create a connection that I can run in any file, whether it's a main page, within a function or an included file. Any ideas please? Thank you in advance.

G

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥16 Qphython 用xlrd读取excel报错
    • ¥15 单片机学习顺序问题!!
    • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
    • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
    • ¥15 相敏解调 matlab
    • ¥15 求lingo代码和思路
    • ¥15 公交车和无人机协同运输
    • ¥15 stm32代码移植没反应
    • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
    • ¥100 连续两帧图像高速减法