根据另一个查询从数据库中获取变量

时间:2016-10-28 18:38:52

标签: php mysql

我不知道我在做什么。

在我的代码中,我得到了一个嵌套的类别的csv列表。我想在每个csv行中添加父类别的名称。

如果我正在获取category_name,category_parent_id,category_image等等

我想在category_description表中进行另一次搜索,找到category_id,它与我之前获得的parent_id的category_id相匹配 我已经注释了我遇到问题

的问题



<?php
DEFINE ('DBUSER', 'myosjrjp_osco638'); 
DEFINE ('DBPW', 'plS]q4)U78'); 
DEFINE ('DBHOST', 'localhost'); 
DEFINE ('DBNAME', 'myosjrjp_osco638'); 
 error_reporting(E_ALL | E_ALL); 
 
$dbc = mysqli_connect(DBHOST,DBUSER,DBPW);
if (!$dbc) {
    die("Database connection failed: " . mysqli_error($dbc));
    exit();
}

$dbs = mysqli_select_db($dbc, DBNAME);
if (!$dbs) {
    die("Database selection failed: " . mysqli_error($dbc));
    exit(); 
}

$result = mysqli_query($dbc, "SHOW COLUMNS FROM categories");
$numberOfRows = mysqli_num_rows($result);
if ($numberOfRows > 0) {

/* By changing Fred below to another specific persons name you can limit access to just the part of the database for that individual. You could eliminate WHERE recorder_id='Fred' all together if you want to give full access to everyone. */

$values = mysqli_query($dbc, "SELECT categories.categories_id, categories.categories_image, categories.parent_id, categories_description.categories_id, categories_description.language_id, categories_description.categories_name, categories_description.categories_description
FROM categories
JOIN categories_description
WHERE categories.categories_id = categories_description.categories_id ");
while ($rowr = mysqli_fetch_row($values)) {
 for ($j=0;$j<$numberOfRows;$j++) {
	 /////////
//	 $parent= mysqli_fetch_row($values,'categories.parent_id');
//	 $values2 = mysqli_query($dbc, "SELECT categories_name, 
//FROM categories_description
//WHERE categories_id = '$parent'");
	 //////

  $csv_output .= $rowr[$j].", ";
 // $csv_output .= $values2('categories_name').", ";
 }
 $csv_output .= "\n";
}

}

print $csv_output;
exit;
?>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

很容易就可以使用mysql查询直接创建一个csv。使用进入mysql的文件功能。您可以使用UNION ALL连接2个表,您可以为每列添加标题。您还可以使用select * from table mane从查询中的另一个表中获取数据,其中表I&#39; d =您当前的表I&#39; d。您可以使用该查询自定义的任何内容。它也会非常快。搜索mysql INTO OUTFILE

请在此处How to add headers in CSV when using into out file的答案中查看示例中的查询。 :