3

我是在 MYSQL 中使用数据类型 POINT 的新手,所以我想在 PHP 中测试一个表的输出,但我收到错误“未定义的索引”。如何修复此错误并显示表中的点?

错误消息 Notice: Undefined index: my_point in C:\xampp\htdocs\view.php 第 23 行

(这些点没有显示在表格中。我该如何解决这个问题?)

MYSQL 表

/*表的表结构highcharts_php*/

            CREATE TABLE `highcharts_php` (
              `id` int(11) NOT NULL AUTO_INCREMENT,
              `run_name` varchar(150) DEFAULT NULL,
              `my_point` POINT DEFAULT NULL,
              `cur_timestamp`  TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
               PRIMARY KEY (`id`)

            ) ENGINE=InnoDB AUTO_INCREMENT=24 DEFAULT CHARSET=latin1;
              SET time_zone='+00:00';
            /*Data for the table `highcharts_php` */

            insert into highcharts_php (`id`,`run_name`,`cur_timestamp`,`my_point`) values ( 1, 'SSTP Keystone COOPER','2012-06-28 00:00:01', GeomFromText( ' POINT(0.6 70.18) ' ) )

* PHP 代码*

            <?php
            $con = mysql_connect("localhost","root","xxxxxxxx");
            if (!$con)
              {
              die('Could not connect: ' . mysql_error());
              }

            mysql_select_db("graph", $con);
            /*$result = mysql_query("SELECT * FROM highcharts_php");*/
            $result = mysql_query("SELECT run_name,cur_timestamp, x( my_point ), y( my_point ) FROM highcharts_php LIMIT 0 , 30")or die 
            (mysql_error());
            echo "<table border='1'>
            <tr>
            <th>run_name</th>
            <th>my_point</th>
            <th>cur_timestamp</th>
            </tr>";

            while($row = mysql_fetch_array($result))
              {
              echo "<tr>";
              echo "<td>" . $row['run_name'] . "</td>";
              echo "<td>" . $row['my_point'] . "</td>";
              echo "<td>" . $row['cur_timestamp'] . "</td>";
              echo "</tr>";
              }
            echo "</table>";

            mysql_close($con);
            ?> 
4

3 回答 3

4

您需要两个计算列的列别名:

$result = mysql_query("
  SELECT 
    run_name,
    cur_timestamp,
    /* Column aliases via AS */
    x( my_point ) AS my_point_x, 
    y( my_point ) AS my_point_y 
  FROM highcharts_php LIMIT 0 , 30") or die();

$row['my_point_x'], $row['my_point_y'].

如果没有列别名,它们就存在于您的$rowas 中,与它们在您的列表中$row['x( my_point )'], $row['y (my_point)']出现的完全一样。SELECT

于 2012-06-29T16:30:05.820 回答
1

别名来自X()Y()使用 AS 的返回值,如下所示:

X( my_point ) AS x_value, Y( my_point ) AS y_value

然后在 PHP 中访问它们:

$row['x_value']
$row['y_value']

否则,您必须使用以下命令访问该列:

$row['X( my_point )']

或类似的东西 - 请参阅文档中的示例以了解如何根据您的查询动态生成列名。

于 2012-06-29T16:30:03.343 回答
1
SELECT run_name,
cur_timestamp, 
CONCAT(x( my_point ), ' ', y( my_point )) as my_point 
FROM highcharts_php LIMIT 0 , 30
于 2012-06-29T16:38:34.337 回答