<mark id="sd9ka"></mark>
      \n \n
      <\/pre>

      示例 2:PHP-PDO-JSON-MySQL-Google Chart<\/strong><\/p>

      本示例使用 PHP 數(shù)據(jù)對(duì)象 (PDO) 連接數(shù)據(jù)庫(kù),提供了更大的靈活性和<\/p>

      代碼:<\/strong><\/p>

      \/*\n... (code) ...\n*\/\n\ntry {\n    \/* Establish the database connection *\/\n    $conn = new PDO(\"mysql:host=localhost;dbname=$dbname\", $username, $password);\n    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);\n\n    \/* Select all the weekly tasks from the table googlechart *\/\n    $result = $conn->query('SELECT * FROM googlechart');\n\n    \/*\n        ---------------------------\n        example data: Table (googlechart)\n        --------------------------\n        weekly_task     percentage\n        Sleep           30\n        Watching Movie  10\n        job             40\n        Exercise        20     \n\n\n    *\/\n\n\n    $rows = array();\n    $table = array();\n    $table['cols'] = array(\n\n        \/\/ Labels for your chart, these represent the column titles.\n        \/* \n            Note that one column is in \"string\" format and another one is in \"number\" format \n            as pie chart only required \"numbers\" for calculating percentage \n            and string will be used for Slice title\n        *\/\n\n        array('label' => 'Weekly Task', 'type' => 'string'),\n        array('label' => 'Percentage', 'type' => 'number')\n\n    );\n\n    \/* Extract the information from $result *\/\n    foreach($result as $r) {\n\n        $temp = array();\n\n        \/\/ The following line will be used to slice the Pie chart\n\n        $temp[] = array('v' => (string) $r['weekly_task']); \n\n        \/\/ Values of each slice\n\n        $temp[] = array('v' => (int) $r['percentage']); \n        $rows[] = array('c' => $temp);\n    }\n\n    $table['rows'] = $rows;\n\n    \/\/ convert data into JSON format\n    $jsonTable = json_encode($table);\n    \/\/echo $jsonTable;\n} catch(PDOException $e) {\n    echo 'ERROR: ' . $e->getMessage();\n}\n\n?>\n<\/pre>\n

      示例 3:PHP-MySQLi-JSON-Google Chart<\/strong><\/p>\n

      此示例利用 MySQLi(MySQL 擴(kuò)展的改進(jìn)版本)進(jìn)行數(shù)據(jù)庫(kù)交互。<\/p>\n

      \/*\n... (code) ...\n*\/\n\n\/* Establish the database connection *\/\n$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);\n\nif (mysqli_connect_errno()) {\n    printf(\"Connect failed: %s\\n\", mysqli_connect_error());\n    exit();\n}\n\n\/* Select all the weekly tasks from the table googlechart *\/\n$result = $mysqli->query('SELECT * FROM googlechart');\n\n\/*\n    ---------------------------\n    example data: Table (googlechart)\n    --------------------------\n    Weekly_Task     percentage\n    Sleep           30\n    Watching Movie  10\n    job             40\n    Exercise        20     \n*\/\n\n\n$rows = array();\n$table = array();\n$table['cols'] = array(\n\n    \/\/ Labels for your chart, these represent the column titles.\n    \/* \n        Note that one column is in \"string\" format and another one is in \"number\" format \n        as pie chart only required \"numbers\" for calculating percentage \n        and string will be used for Slice title\n    *\/\n\n    array('label' => 'Weekly Task', 'type' => 'string'),\n    array('label' => 'Percentage', 'type' => 'number')\n\n);\n\n\/* Extract the information from $result *\/\nforeach($result as $r) {\n\n    $temp = array();\n\n    \/\/ The following line will be used to slice the Pie chart\n\n    $temp[] = array('v' => (string) $r['weekly_task']); \n\n    \/\/ Values of the each slice\n\n    $temp[] = array('v' => (int) $r['percentage']); \n    $rows[] = array('c' => $temp);\n}\n\n$table['rows'] = $rows;\n\n\/\/ convert data into JSON format\n$jsonTable = json_encode($table);\n\/\/echo $jsonTable;\n\n\n<\/pre>"}	
      	
      
      
      
      
      
      
      

      国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

      首頁(yè) 數(shù)據(jù)庫(kù) mysql教程 如何使用 PHP、MySQL 和 JSON 創(chuàng)建 Google 圖表?

      如何使用 PHP、MySQL 和 JSON 創(chuàng)建 Google 圖表?

      Dec 08, 2024 am 04:40 AM

      How can I create a Google Chart using PHP, MySQL, and JSON?

      PHP MySQL Google Chart JSON - 完整示例

      隨著技術(shù)格局的發(fā)展,可視化數(shù)據(jù)的能力變得越來(lái)越重要。 Google Chart 是一種強(qiáng)大的數(shù)據(jù)可視化工具。它使開(kāi)發(fā)人員能夠創(chuàng)建各種圖表,包括餅圖、條形圖和折線圖。然而,將 Google Chart 與 MySQL 數(shù)據(jù)源集成可能會(huì)帶來(lái)挑戰(zhàn),特別是在使用 PHP 作為編程語(yǔ)言時(shí)。

      本文提供了使用 PHP 和 MySQL 生成 Google Charts 的全面解決方案。我們將介紹多個(gè)示例來(lái)說(shuō)明不同 PHP 數(shù)據(jù)訪問(wèn)方法的使用:

      示例 1:PHP-MySQL-JSON-Google Chart(非 Ajax)

      用法:

      1. 創(chuàng)建一個(gè)名為 MySQL 數(shù)據(jù)庫(kù)“圖表”。
      2. 創(chuàng)建一個(gè)名為“googlechart”的表,其中包含兩列:“weekly_task”和“百分比”。
      3. 將示例數(shù)據(jù)插入表中,確?!鞍俜直取绷邪瑑H數(shù)字

      代碼:

      $con = mysql_connect("localhost", "Username", "Password") or die("Failed to connect with database!!!!");
      mysql_select_db("Database Name", $con); 
      // The Chart table contains two fields: weekly_task and percentage
      // This example will display a pie chart. If you need other charts such as a Bar chart, you will need to modify the code a little to make it work with bar chart and other charts
      $sth = mysql_query("SELECT * FROM chart");
      
      /*
      ---------------------------
      example data: Table (Chart)
      --------------------------
      weekly_task     percentage
      Sleep           30
      Watching Movie  40
      work            44
      */
      
      //flag is not needed
      $flag = true;
      $table = array();
      $table['cols'] = array(
      
          // Labels for your chart, these represent the column titles
          // Note that one column is in "string" format and another one is in "number" format as pie chart only required "numbers" for calculating percentage and string will be used for column title
          array('label' => 'Weekly Task', 'type' => 'string'),
          array('label' => 'Percentage', 'type' => 'number')
      
      );
      
      $rows = array();
      while($r = mysql_fetch_assoc($sth)) {
          $temp = array();
          // The following line will be used to slice the Pie chart
          $temp[] = array('v' => (string) $r['Weekly_task']); 
      
          // Values of each slice
          $temp[] = array('v' => (int) $r['percentage']); 
          $rows[] = array('c' => $temp);
      }
      
      $table['rows'] = $rows;
      $jsonTable = json_encode($table);
      //echo $jsonTable;
      ?>
      
      <html>
        <head>
        <!--Load the Ajax API-->
          <script type="text/javascript" src="https://www.google.com/jsapi"></script>
          <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
          <script type="text/javascript">
      
          // Load the Visualization API and the piechart package.
          google.load('visualization', '1', {'packages':['corechart']});
      
          // Set a callback to run when the Google Visualization API is loaded.
          google.setOnLoadCallback(drawChart);
      
          function drawChart() {
      
            // Create our data table out of JSON data loaded from server.
            var data = new google.visualization.DataTable(<?=$jsonTable?>);
            var options = {
                 title: 'My Weekly Plan',
                is3D: 'true',
                width: 800,
                height: 600
              };
            // Instantiate and draw our chart, passing in some options.
            // Do not forget to check your div ID
            var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
            chart.draw(data, options);
          }
          </script>
        </head>
      
        <body>
          <!--this is the div that will hold the pie chart-->
          <div>

      示例 2:PHP-PDO-JSON-MySQL-Google Chart

      本示例使用 PHP 數(shù)據(jù)對(duì)象 (PDO) 連接數(shù)據(jù)庫(kù),提供了更大的靈活性和

      代碼:

      /*
      ... (code) ...
      */
      
      try {
          /* Establish the database connection */
          $conn = new PDO("mysql:host=localhost;dbname=$dbname", $username, $password);
          $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      
          /* Select all the weekly tasks from the table googlechart */
          $result = $conn->query('SELECT * FROM googlechart');
      
          /*
              ---------------------------
              example data: Table (googlechart)
              --------------------------
              weekly_task     percentage
              Sleep           30
              Watching Movie  10
              job             40
              Exercise        20     
      
      
          */
      
      
          $rows = array();
          $table = array();
          $table['cols'] = array(
      
              // Labels for your chart, these represent the column titles.
              /* 
                  Note that one column is in "string" format and another one is in "number" format 
                  as pie chart only required "numbers" for calculating percentage 
                  and string will be used for Slice title
              */
      
              array('label' => 'Weekly Task', 'type' => 'string'),
              array('label' => 'Percentage', 'type' => 'number')
      
          );
      
          /* Extract the information from $result */
          foreach($result as $r) {
      
              $temp = array();
      
              // The following line will be used to slice the Pie chart
      
              $temp[] = array('v' => (string) $r['weekly_task']); 
      
              // Values of each slice
      
              $temp[] = array('v' => (int) $r['percentage']); 
              $rows[] = array('c' => $temp);
          }
      
          $table['rows'] = $rows;
      
          // convert data into JSON format
          $jsonTable = json_encode($table);
          //echo $jsonTable;
      } catch(PDOException $e) {
          echo 'ERROR: ' . $e->getMessage();
      }
      
      ?>
      

      示例 3:PHP-MySQLi-JSON-Google Chart

      此示例利用 MySQLi(MySQL 擴(kuò)展的改進(jìn)版本)進(jìn)行數(shù)據(jù)庫(kù)交互。

      /*
      ... (code) ...
      */
      
      /* Establish the database connection */
      $mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
      
      if (mysqli_connect_errno()) {
          printf("Connect failed: %s\n", mysqli_connect_error());
          exit();
      }
      
      /* Select all the weekly tasks from the table googlechart */
      $result = $mysqli->query('SELECT * FROM googlechart');
      
      /*
          ---------------------------
          example data: Table (googlechart)
          --------------------------
          Weekly_Task     percentage
          Sleep           30
          Watching Movie  10
          job             40
          Exercise        20     
      */
      
      
      $rows = array();
      $table = array();
      $table['cols'] = array(
      
          // Labels for your chart, these represent the column titles.
          /* 
              Note that one column is in "string" format and another one is in "number" format 
              as pie chart only required "numbers" for calculating percentage 
              and string will be used for Slice title
          */
      
          array('label' => 'Weekly Task', 'type' => 'string'),
          array('label' => 'Percentage', 'type' => 'number')
      
      );
      
      /* Extract the information from $result */
      foreach($result as $r) {
      
          $temp = array();
      
          // The following line will be used to slice the Pie chart
      
          $temp[] = array('v' => (string) $r['weekly_task']); 
      
          // Values of the each slice
      
          $temp[] = array('v' => (int) $r['percentage']); 
          $rows[] = array('c' => $temp);
      }
      
      $table['rows'] = $rows;
      
      // convert data into JSON format
      $jsonTable = json_encode($table);
      //echo $jsonTable;
      
      
      

      以上是如何使用 PHP、MySQL 和 JSON 創(chuàng)建 Google 圖表?的詳細(xì)內(nèi)容。更多信息請(qǐng)關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

      本站聲明
      本文內(nèi)容由網(wǎng)友自發(fā)貢獻(xiàn),版權(quán)歸原作者所有,本站不承擔(dān)相應(yīng)法律責(zé)任。如您發(fā)現(xiàn)有涉嫌抄襲侵權(quán)的內(nèi)容,請(qǐng)聯(lián)系admin@php.cn

      熱AI工具

      Undress AI Tool

      Undress AI Tool

      免費(fèi)脫衣服圖片

      Undresser.AI Undress

      Undresser.AI Undress

      人工智能驅(qū)動(dòng)的應(yīng)用程序,用于創(chuàng)建逼真的裸體照片

      AI Clothes Remover

      AI Clothes Remover

      用于從照片中去除衣服的在線人工智能工具。

      Clothoff.io

      Clothoff.io

      AI脫衣機(jī)

      Video Face Swap

      Video Face Swap

      使用我們完全免費(fèi)的人工智能換臉工具輕松在任何視頻中換臉!

      熱工具

      記事本++7.3.1

      記事本++7.3.1

      好用且免費(fèi)的代碼編輯器

      SublimeText3漢化版

      SublimeText3漢化版

      中文版,非常好用

      禪工作室 13.0.1

      禪工作室 13.0.1

      功能強(qiáng)大的PHP集成開(kāi)發(fā)環(huán)境

      Dreamweaver CS6

      Dreamweaver CS6

      視覺(jué)化網(wǎng)頁(yè)開(kāi)發(fā)工具

      SublimeText3 Mac版

      SublimeText3 Mac版

      神級(jí)代碼編輯軟件(SublimeText3)

      熱門(mén)話題

      建立與MySQL Server的安全遠(yuǎn)程連接 建立與MySQL Server的安全遠(yuǎn)程連接 Jul 04, 2025 am 01:44 AM

      TosecurelyConnectToaremoteMysqlServer,Usesshtunneling,configuremysqlforremoteaccess,setFireWallrules,andConsidersSlencryption 。首先,stardansshtunnelwithssh-l3307:localhost:3306user@remote-Server-server-nandConnectViamySql-h127.0.0.0.0.1-p3307.second,editmys

      如何將MySQL bin目錄添加到系統(tǒng)路徑 如何將MySQL bin目錄添加到系統(tǒng)路徑 Jul 01, 2025 am 01:39 AM

      要將MySQL的bin目錄添加到系統(tǒng)PATH,需根據(jù)不同操作系統(tǒng)進(jìn)行配置。1.Windows系統(tǒng):找到MySQL安裝目錄下的bin文件夾(默認(rèn)路徑通常為C:\ProgramFiles\MySQL\MySQLServerX.X\bin),右鍵“此電腦”→“屬性”→“高級(jí)系統(tǒng)設(shè)置”→“環(huán)境變量”,在“系統(tǒng)變量”中選中Path并編輯,新增MySQLbin路徑,保存后重啟命令提示符并輸入mysql--version驗(yàn)證;2.macOS和Linux系統(tǒng):Bash用戶編輯~/.bashrc或~/.bash_

      MySQL中的交易隔離級(jí)別是多少?默認(rèn)值是哪個(gè)? MySQL中的交易隔離級(jí)別是多少?默認(rèn)值是哪個(gè)? Jun 23, 2025 pm 03:05 PM

      MySQL的默認(rèn)事務(wù)隔離級(jí)別是可重復(fù)讀(RepeatableRead),它通過(guò)MVCC和間隙鎖防止臟讀和不可重復(fù)讀,并在大多數(shù)情況下避免幻讀;其他主要級(jí)別包括讀未提交(ReadUncommitted),允許臟讀但性能最快,1.讀已提交(ReadCommitted)確保讀取已提交數(shù)據(jù)但可能遇到不可重復(fù)讀和幻讀,2.可重復(fù)讀(RepeatableRead)默認(rèn)級(jí)別,保證事務(wù)內(nèi)多次讀取結(jié)果一致,3.串行化(Serializable)最高級(jí)別,通過(guò)鎖阻止其他事務(wù)修改數(shù)據(jù),確保數(shù)據(jù)完整性但犧牲性能;可通過(guò)

      MySQL WorkBench在哪里保存連接信息 MySQL WorkBench在哪里保存連接信息 Jun 26, 2025 am 05:23 AM

      MySQLWorkbench將連接信息存儲(chǔ)在系統(tǒng)的配置文件中,具體路徑因操作系統(tǒng)而異:1.Windows系統(tǒng)中位于%APPDATA%\MySQL\Workbench\connections.xml;2.macOS系統(tǒng)中位于~/Library/ApplicationSupport/MySQL/Workbench/connections.xml;3.Linux系統(tǒng)中通常位于~/.mysql/workbench/connections.xml或~/.local/share/data/MySQL/Wor

      分析MySQL緩慢查詢?nèi)罩疽圆檎倚阅芷款i 分析MySQL緩慢查詢?nèi)罩疽圆檎倚阅芷款i Jul 04, 2025 am 02:46 AM

      開(kāi)啟MySQL慢查詢?nèi)罩静⒎治隹啥ㄎ恍阅軉?wèn)題。 1.編輯配置文件或動(dòng)態(tài)設(shè)置slow_query_log和long_query_time;2.日志包含Query_time、Lock_time、Rows_examined等關(guān)鍵字段,輔助判斷效率瓶頸;3.使用mysqldumpslow或pt-query-digest工具高效分析日志;4.優(yōu)化建議包括添加索引、避免SELECT*、拆分復(fù)雜查詢等。例如為user_id加索引能顯著減少掃描行數(shù),提升查詢效率。

      使用mySQL中的mysqldump執(zhí)行邏輯備份 使用mySQL中的mysqldump執(zhí)行邏輯備份 Jul 06, 2025 am 02:55 AM

      mysqldump是用于執(zhí)行MySQL數(shù)據(jù)庫(kù)邏輯備份的常用工具,它生成包含CREATE和INSERT語(yǔ)句的SQL文件以重建數(shù)據(jù)庫(kù)。1.它不備份原始文件,而是將數(shù)據(jù)庫(kù)結(jié)構(gòu)和內(nèi)容轉(zhuǎn)換為可移植的SQL命令;2.適用于小型數(shù)據(jù)庫(kù)或選擇性恢復(fù),不適合TB級(jí)數(shù)據(jù)快速恢復(fù);3.常用選項(xiàng)包括--single-transaction、--databases、--all-databases、--routines等;4.恢復(fù)時(shí)使用mysql命令導(dǎo)入,并可關(guān)閉外鍵檢查以提升速度;5.建議定期測(cè)試備份、使用壓縮、自動(dòng)化調(diào)

      在MySQL列和查詢中處理零值 在MySQL列和查詢中處理零值 Jul 05, 2025 am 02:46 AM

      處理MySQL中的NULL值需注意:1.設(shè)計(jì)表時(shí)關(guān)鍵字段設(shè)為NOTNULL,可選字段允許NULL;2.查詢判斷必須用ISNULL或ISNOTNULL,不能用=或!=;3.可用IFNULL或COALESCE函數(shù)替換顯示默認(rèn)值;4.插入或更新時(shí)直接使用NULL值需謹(jǐn)慎,注意數(shù)據(jù)源和ORM框架處理方式。NULL表示未知值,不等于任何值,包括自身,因此查詢、統(tǒng)計(jì)、連接表時(shí)要特別小心,避免漏數(shù)據(jù)或邏輯錯(cuò)誤。合理使用函數(shù)和約束可以有效減少因NULL帶來(lái)的干擾。

      重置MySQL Server的root密碼 重置MySQL Server的root密碼 Jul 03, 2025 am 02:32 AM

      要重置MySQL的root密碼,請(qǐng)按以下步驟操作:1.停止MySQL服務(wù)器,使用sudosystemctlstopmysql或sudosystemctlstopmysqld;2.以--skip-grant-tables模式啟動(dòng)MySQL,執(zhí)行sudomysqld--skip-grant-tables&;3.登錄MySQL并根據(jù)版本執(zhí)行相應(yīng)的SQL命令修改密碼,如FLUSHPRIVILEGES;ALTERUSER'root'@'localhost'IDENTIFIEDBY'your_new

      See all articles