SokrMan Posted August 25, 2010 Share Posted August 25, 2010 So I have a jobs database with the following columns: id, jobtext, jobdate, and id. This is how it looks right now: http://prahan.com/jobs/display.html.php I have another table called author. In the authorid column in need the results of this query, SELECT name FROM author WHERE id = (SELECT authorid FROM job) , to be displayed for each row. I also want to be able to customize the header title for each column. Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/211721-displaying-a-mysql-table-in-a-html-page/ Share on other sites More sharing options...
mikosiko Posted August 25, 2010 Share Posted August 25, 2010 ..... I have another table called author. In the authorid column in need the results of this query, SELECT name FROM author WHERE id = (SELECT authorid FROM job) , to be displayed for each row. I also want to be able to customize the header title for each column. Thanks in advance! The first thing that you should do is read in deep this http://dev.mysql.com/doc/refman/5.0/en/join.html after that the answer to your questions should be easy... just try Quote Link to comment https://forums.phpfreaks.com/topic/211721-displaying-a-mysql-table-in-a-html-page/#findComment-1103674 Share on other sites More sharing options...
SokrMan Posted August 25, 2010 Author Share Posted August 25, 2010 The first thing that you should do is read in deep this http://dev.mysql.com/doc/refman/5.0/en/join.html after that the answer to your questions should be easy... just try Hmm.. can you give me some code examples that relate to my problem? Im a PHP beginner and that is pretty confusing.. Quote Link to comment https://forums.phpfreaks.com/topic/211721-displaying-a-mysql-table-in-a-html-page/#findComment-1103680 Share on other sites More sharing options...
mikosiko Posted August 25, 2010 Share Posted August 25, 2010 In the link that I gave to you are plenty of code example completely related to your problem should be really simple for you to figure it out just reading a few examples there Quote Link to comment https://forums.phpfreaks.com/topic/211721-displaying-a-mysql-table-in-a-html-page/#findComment-1103694 Share on other sites More sharing options...
sasa Posted August 25, 2010 Share Posted August 25, 2010 use thi SQL code SELECT jobs.id, jobtext, jobdate, name FROM jobs LEFT JOIN author ON author.id=jobs.authorid Quote Link to comment https://forums.phpfreaks.com/topic/211721-displaying-a-mysql-table-in-a-html-page/#findComment-1103697 Share on other sites More sharing options...
SokrMan Posted August 25, 2010 Author Share Posted August 25, 2010 Awesome! Thanks! Now how do i display that on a HTML page? Btw. the table name is job. Also, customizing the header titles. Quote Link to comment https://forums.phpfreaks.com/topic/211721-displaying-a-mysql-table-in-a-html-page/#findComment-1103702 Share on other sites More sharing options...
SokrMan Posted August 25, 2010 Author Share Posted August 25, 2010 Nevermind bout inserting it into the html page. This is the code i have. <html><head><title>Available Jobs</title></head><body> <?php $db_host = 'localhost'; $db_user = 'root'; $db_pwd = 'pass'; $database = 'db'; $table = 'tb'; if (!mysql_connect($db_host, $db_user, $db_pwd)) die("Can't connect to database"); if (!mysql_select_db($database)) die("Can't select database"); // sending query $result = mysql_query("SELECT job.id, jobtext, jobdate, name FROM job LEFT JOIN author ON author.id=job.authorid "); if (!$result) { die("Query to show fields from table failed"); } $fields_num = mysql_num_fields($result); echo "<h1>Jobs:</h1>"; echo "<table border='1'><tr>"; // printing table headers for($i=0; $i<$fields_num; $i++) { $field = mysql_fetch_field($result); echo "<td>{$field->name}</td>"; } echo "</tr>\n"; // printing table rows while($row = mysql_fetch_row($result)) { echo "<tr>"; // $row is array... foreach( .. ) puts every element // of $row to $cell variable foreach($row as $cell) echo "<td>$cell</td>"; echo "</tr>\n"; } mysql_free_result($result); ?> </body></html> How do I edit the code so i can customize the headers for each column? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/211721-displaying-a-mysql-table-in-a-html-page/#findComment-1103705 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.