douweng1904 2017-07-21 23:58
浏览 8
已采纳

如何连接数据库文件来提取数据? [重复]

This question already has an answer here:

I need some help with my PHP code. I want to connect to the database file called myChannel.db to extract the data from the rows, but I have got no idea how to do that by using this code:

Here is the config:

<?php
  define('DB_HOST', 'localhost');
  define('DB_USER', 'myusername');
  define('DB_PASSWORD', 'mypassword');
  define('DB_DATABASE', 'mydbname');
  $errmsg_arr = array();
  $errflag = false;
  $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

  if(!$link) 
  {
    die('Failed to connect to server: ' . mysql_error());
  }
  $db = mysql_select_db(DB_DATABASE);

  if(!$db) 
  {
    die("Unable to select database");
  }
?>

Here is the get-listing.php script:

<?php
$errmsg_arr = array();
$errflag = true;
$link;

//Connect to the database
require_once('config.php');

$qrytable1="SELECT id, channels, programme_title, programme_description, programme_start_date, programme_end_date FROM tvguide WHERE channels='$channels' && id='$id'";
$result1 = mysql_query($qrytable1) or die('Error:<br />' . $qry . '<br />' . mysql_error());

while ($row = mysql_fetch_array($result1)) 
{
  //output the data for channels, programme_title, programme_description, programme_start_date, programme_end
}
?>

The code will only allow me to connect to mysql and nothing is else.

Can you please show me an example how I could connect to myChannel.db and extract the data from the columns called channels, programme_title, programme_description, programme_start_date, programme_end where the channels is matched?

</div>
  • 写回答

1条回答 默认 最新

  • douaoren4402 2017-07-22 00:30
    关注

    You asked for an example... this was my solution for querying geocoordinates from mySQL database. I call all of this in my php file...

         //set up a MySQL query, this is the data you are asking for.  This asks for all data from tabele 'shelters'.   Be sure to call your specific table.  
    $sql = "SELECT * FROM shelters;";
    
    if(!$results = $link->query($sql)){
      die("Query Unsuccessful");
    }
    
    //Here I create a value map and pull out the results I care about from my request query.  
    $valueMap = array();
    while ($data = $results->fetch_assoc()){
        $valueMap[] = array(
          'title' => $data['title'],
          'lat' => $data['lat'],
          'lng' => $data['lng'],
    
        );
    }
    //encoding it into JSON is more manageble for me, but you will have to parse it into a javascript object in your index file. 
      $JSONData = json_encode($valueMap);
    
     //heere you want to echo your data in order to retrieve it in your index file. Has the functionality of connecting to an API
      echo $JSONData;
    

    Hit me up in the comments with any more specific questions.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?