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 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染