doubangzhang6992 2015-01-03 19:25
浏览 15

MySql php没有返回数据

I'm trying a simple php example to confirm data retrieval from a MySql database and I'm not seeing any data returned, but also no specific error either. Any advice for what to try next will be greatly appreciated!


I have a local MySql db as well as an internet web site instance and they are both doing the same thing ... which is ... I can connect to the mysql server, then connect to the database, but the sql result has no data.

Viewing over the respective db's I can see the table and data. I've not previously seen a failure running a valid query after connecting to the server and db.

Locally I'm using VS2012 with a php tool (DEVSENSE) and on the internet web site I've created the below php file and am accessing it via web address.

The following is the php file ... note that I've added some additional output to show the contents of some of the variables.

<body>

    <form>        
            <?php

            $mysqlserver="localhost";
            $mysqlusername="user";
            $mysqlpassword="mypassword";
            $mysqldatabase="mydata";

$link = mysql_connect($mysqlserver, $mysqlusername, $mysqlpassword);
if (!$link) {
    die('Could not connect: ' . mysql_error());
}


if ($link) {

echo '<a>MySql db link= ' . $link . '</a><br>';
$db_selected = mysql_select_db($mysqldatabase, $link);
if (!$db_selected) {
      echo '<select id="Error" name="err">';
      echo '<option value="Error"></option>';
      echo '</select>';
      die ('<a> Can\'t use database: ' . $mysqldatabase . '</a>');
}

  echo '<a>database= ' . $mysqldatabase . '</a><br>';

  $sql = sprintf("SELECT Location FROM `myLocations` Order by Location");

  echo '<a>sql= ' . $sql . '</a><br>';

  $result = mysql_query($link, $sql);

  echo '<a>result= ' . $result . '</a><br>';

  if (!$Result) {
      echo '<label>Locations (none) &nbsp;</label>';
      echo '<select id="Error" name="err">';
      echo '<option value="Error"></option>';
      echo '</select>';
      die ("<a>Error: (" . mysql_errno($link) . "): " . mysql_error($link) . "</a>");
      }

   echo '<label>Locations</label>&nbsp;';
   echo '<select name="selectedLocation">';
   echo '<option value=""></option>';

   $num_results = mysql_num_rows($result);
   for ($i=0;$i<$num_results;$i++) {
       $row = mssql_fetch_array($result);
       $name = $row['name'];
       echo '<option value="' .$name. '">' .$name. '</option>';
   }

   echo '</select>';

   mysql_close($link);
}

?>
    </form>

</body>

Here's a sample returned result page using the above php file:

MySql db link= Resource id #2
database= mydata
sql= SELECT Location FROM myLocations Order by Location
result=
Locations (none)  Error: (0):


Here's the generated schema for table 'myLocations'

-- ----------------------------------------------------------------------------
-- Table mydata_mylocations
-- ----------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS 'mydata'.`mylocations` (
  `Id` INT(11) NOT NULL,
  `Location` VARCHAR(50) NOT NULL,
  `IsActive` TINYINT(1) NOT NULL DEFAULT '1',
  `AsOfDate` DATE NULL DEFAULT NULL,
  PRIMARY KEY (`Id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
    • ¥20 软件测试决策法疑问求解答
    • ¥15 win11 23H2删除推荐的项目,支持注册表等
    • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
    • ¥15 qt6.6.3 基于百度云的语音识别 不会改
    • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
    • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
    • ¥15 lingo18勾选global solver求解使用的算法
    • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
    • ¥20 测距传感器数据手册i2c