這裏是我的代碼我使用來自4頁不同的表加入MySQL表然後過濾結果作爲列名
SELECT DISTINCT c.title as CourseTitle, t.title as TopicTitle, l.title as LessonTitle, r.title as ResourceTitle, r.location, r.type, r.duration
FROM j17_lessons l, j17_topics t, j17_courses c, j17_resources r
WHERE
CONCAT(c.title, t.title, l.title, r.title, r.type, r.location) LIKE '%Fatih%'
AND c.id = t.course_id
AND l.topic_id = t.id
AND r.lesson_id = l.id
ORDER BY c.title, t.id, l.id, r.id;
這裏取mysql的結果的屏幕截圖,我的結果取 http://i40.tinypic.com/2v1w0ib.png
現在什麼我需要爲數據庫中的每個「CourseTitle」創建一個HTML表格。
使用SQL語句和PHP代碼中,我能得到導致了第一次查詢,但我還需要第二次查詢拆分表的foreach「CourseTitle」
/* connect to the db */
$connection = mysql_connect('localhost','root','123');
mysql_select_db('alhudapk',$connection);
/* show tables */
$result = mysql_query('SELECT DISTINCT c.title as CourseTitle, t.title as TopicTitle, l.title as LessonTitle, r.title as ResourceTitle, r.location, r.type, r.duration
FROM j17_lessons l, j17_topics t, j17_courses c, j17_resources r
WHERE
CONCAT(c.title, t.title, l.title, r.title, r.type, r.location) LIKE '%Taleem%'
AND c.id = t.course_id
AND l.topic_id = t.id
AND r.lesson_id = l.id
ORDER BY c.title, t.id, l.id, r.id',$connection) or die('cannot show tables');
while($tableName = mysql_fetch_row($result)) {
$table = $tableName[0];
echo '<h3>',$table,'</h3>';
$result2 = mysql_query('SELECT '.$table . 'AS' .$table);
if(mysql_num_rows($result2)) {
請指引我建立一個正確的和更好的代碼
如果您的設計需要將表格/字段名稱存儲在其他字段中,則應重新設計您的設計。它會很快變成不可維護的混亂。 – 2012-04-04 18:14:18