简体   繁体   English

如何调试不再起作用的PHP / SQL代码?

[英]How to debug PHP/SQL code that no longer works?

I'm new to PHP but have the code below. 我是PHP的新手,但是有下面的代码。 It essentially should load some values into an array from an SQL query then use the values in the array to output from another query. 本质上,它应该从SQL查询将一些值加载到数组中,然后使用该数组中的值从另一个查询中输出。 Loop within a loop. 循环内循环。

Oddly it did work at first but now doesn't. 奇怪的是,它起初确实有效,但现在没有。 If I separate the two queries they still work fine. 如果我将两个查询分开,它们仍然可以正常工作。 So no database issues, and no error messages given. 因此,没有数据库问题,也没有错误消息。 I just want to know if it's OK to use this method as it seems the easiest. 我只想知道使用此方法是否可行,因为这似乎最简单。 Or do I have to separate the loops out? 还是我必须将循环分开?

$sql = 'SELECT menuID FROM explore_section1 WHERE menukey="sea-ovr"';
$place = $conn->query($sql);
while($row = $place->fetch_assoc()) {
  $sql2 = 'SELECT url, name FROM explore_items WHERE menuID="'.$row["menuID"].'"';
  $result = $conn->query($sql2);
  while($row2 = $result->fetch_assoc()) {
    echo '<li><a href="'.$row2["url"].'">'.$row2["name"].'</a></li>';
  }     
}   

Thanks! 谢谢!

I don't see any reason for 2 queries, just that simple: 我看不出有2个查询的任何原因,就这么简单:

$sql = 'SELECT s.menuID, i.url, i.name 
        FROM explore_section1 s
        LEFT JOIN explore_items i
        ON s.menuID = i.menuID
        WHERE s.menukey="sea-ovr"';
if($result = $conn->query($sql)) {
    while($row = $result->fetch_assoc()) {
        echo '<li><a href="'.$row["url"].'">'.$row["name"].'</a></li>';
    }
} else { 
    echo "Error:". $conn->error);
}

UPDATE Query should work: http://sqlfiddle.com/#!9/76f2f/1 UPDATE查询应该工作: http : //sqlfiddle.com/#!9/76f2f/1

OK, after hours of playing. 好,经过几个小时的游戏。 MySQL has a weird thing. MySQL有一个奇怪的事情。 I was importing the data as nearly everywhere advises using the following structure queries: 我使用以下结构查询在几乎所有建议的位置导入数据:

LOAD DATA LOCAL INFILE 'C:/data.csv' INTO TABLE explore_items
FIELDS TERMINATED BY ','
ENCLOSED BY ';'
LINES TERMINATED BY '\n'
(field1, field2);

This looked to work fine and queries work if using just one table but soon as you join them they don't work. 如果仅使用一个表,这看起来可以很好地工作,查询也可以工作,但是一旦加入它们,它们就无法工作。 ODD!! 奇!! The way I have to import the data is as per the import instructions below (found in another thread): 我必须导入数据的方式如下所示(在另一个线程中找到):

phpMyAdmin has the necessary tool to handle CSV import. phpMyAdmin具有必要的工具来处理CSV导入。 Here are the steps to follow: 以下是要遵循的步骤:

  • prepare the CSV file to have the fields in the same order as the MySQL table fields 准备CSV文件以使字段具有与MySQL表字段相同的顺序

  • remove the header row from CSV (if any), so only data is there in file go to phpMyAdmin interface select the table in left menu 从CSV中删除标题行(如果有的话),因此文件中只有数据。请转到phpMyAdmin界面,在左侧菜单中选择表格

  • click the import button at top 点击顶部的导入按钮

  • browser the CSV file 浏览器CSV文件

  • select the option "CSV using LOAD DATA" 选择选项“使用LOAD DATA的CSV”

  • enter "," in the "fields terminated by" 在“终止于”字段中输入“,”

  • enter the column names in the same order as they are in db table 以与db表中相同的顺序输入列名称

  • click go button and you are done. 单击执行按钮,您就完成了。

Thanks @Alex for all your time on that one!! 感谢@Alex在这上面的所有时间!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM