EzwanAbid Posted December 20, 2012 Share Posted December 20, 2012 Aw I never I found another new error because I though I already fix them all.. So here my problem, my while loop is looping twice.. They exactly display the data that I want, but they will display it twice.. so that's my problem. here my code : <?php $host="localhost"; // Host name $username="root"; // Mysql username $password=""; // Mysql password $db_name="finalproject"; // Database name // Connect to server and select database. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $sql="SELECT * FROM student,surat,admin WHERE student.student_id=surat.student_id"; $result=mysql_query($sql); ?> <link href="TableCSSCodeupdate.css" rel="stylesheet" type="text/css"> <script src="Scripts/AC_RunActiveContent.js" type="text/javascript"></script> <table width="612" border="0" cellspacing="2" cellpadding="0" class="CSSTableGenerator"> <tr align="center"> </tr> <?php while($rows=mysql_fetch_array($result)){ ?> <td align="left"></td> <td align="left"><div align="left"><b>NO.</b> <td>:</td><td><? echo $rows['id_surat']; ?></div></td></tr> <td align="left"></td> <td align="left"><div align="left"><b>NAME</b> <td>:</td><td><? echo $rows['fullname']; ?></div></td></tr> <td align colspan="1" rowspan="2"></td> <tr align="left"> <td align="left"><div align="left"><b>STUDENT ID</b> <td>:</td><td> <? echo $rows['student_id']; ?></div></td></tr> <td align colspan="1" rowspan="2"></td> <tr align="left"> <td align="left"><div align="left"><b>IC NO.</b> <td>:</td><td><? echo $rows['ic_number']; ?></div></td></tr> <td align colspan="1" rowspan="2"></td> <tr align="left"> <td align="left"><div align="left"><b>COURSE</b> <td>:</td><td><? echo $rows['course']; ?></div></td></tr> <td align colspan="1" rowspan="2"></td></tr> <tr align="left"> <td align="left"><div align="left"><b>TYPE OF LETTER</b><td>:</td><td><? echo $rows['jenissurat']; ?></div></td></tr> <tr> <th scope="col"> </th> <th scope="col"> </th> <th scope="col"> </th> <th scope="col"> </th> </tr> <tr align="left"> <th scope="col"> </th> <th scope="col"> <?php $ids=$rows['id_surat']; $jenis=$rows['jenissurat']; switch($jenis) { case "TamatBelajar": echo '<a href="PHPWord/TamatBelajar-code.php">Generate Letter</a><br><br>'; mysql_query("UPDATE surat SET status='Approve' WHERE id_surat='$ids'"); break; case "PengesahanBelajar": echo '<a href="PHPWord/PengesahanBelajar-code.php">Generate Letter</a><br><br>'; mysql_query("UPDATE surat SET status='Approve' WHERE id_surat='$ids'"); break; case "KebenaranProjek": echo '<a href="PHPWord/KebenaranProjek-code.php">Generate Letter</a><br><br>'; mysql_query("UPDATE surat SET status='Approve' WHERE id_surat='$ids'"); break; case "PelepasanPeperiksaanAkhir": echo '<a href="PHPWord/PelepasanExam-code.php">Generate Letter</a><br><br>'; mysql_query("UPDATE surat SET status='Approve' WHERE id_surat='$ids'"); break; }?> </tr> <tr> <th scope="col"> </th> <th scope="col"> </th> <th scope="col"> </th> <th scope="col"> </th> </tr> </tr> <?php } ?> </table> </td> </tr> </table> <br> <?php mysql_close(); ?> </body> </html> I already download the file for that contain the full code. I really need help and solution ASAP.. Please :'( QueueLetterForm.php Quote Link to comment Share on other sites More sharing options...
cpd Posted December 20, 2012 Share Posted December 20, 2012 Do a var_dump on the $results variable and you'll likely see you have dupes meaning you need to review your SQL as its doubling up the data. Quote Link to comment Share on other sites More sharing options...
EzwanAbid Posted December 20, 2012 Author Share Posted December 20, 2012 Thank you for your reply..but what is var_dump ? it's first time I heard that kind of function.. Yes I've already search at google about it but all I can see, I need a huge change in my code, and I don't think I have enough time for that. Any other way sir? Sorry Quote Link to comment Share on other sites More sharing options...
EzwanAbid Posted December 20, 2012 Author Share Posted December 20, 2012 OMG too many typo inside my post.. sorry for that.. I'm in hurry. Sorry guys.. Quote Link to comment Share on other sites More sharing options...
DavidAM Posted December 20, 2012 Share Posted December 20, 2012 @CPD Do a var_dump on the $results variable and you'll likely see you have dupes meaning you need to review your SQL as its doubling up the data. $results does not exist in that script. $result is a mysql resource and var_dump'ing it will not show the duplicate rows. Other than that, you are on the right track. @EzwanAbid $sql="SELECT * FROM student,surat,admin WHERE student.student_id=surat.student_id"; You have three tables in that query, but only one "join" condition. So every row that matches student and surat will be matched with every row in the admin table (a Cartesian product). My guess is you have two rows in that table. You need to fix the query to fix your problem. Quote Link to comment Share on other sites More sharing options...
codefossa Posted December 20, 2012 Share Posted December 20, 2012 (edited) Didn't see him respond. ^^ Edited December 20, 2012 by Xaotique Quote Link to comment Share on other sites More sharing options...
EzwanAbid Posted December 20, 2012 Author Share Posted December 20, 2012 Thank for your reply.. How to make those var_dump? I already try to learn from certain tutorial about var_dump.. and I not really understand how to do it. @EzwanAbid You have three tables in that query, but only one "join" condition. So every row that matches student and surat will be matched with every row in the admin table (a Cartesian product). My guess is you have two rows in that table. You need to fix the query to fix your problem. Yes, I only need to 'join up' student and surat table which the data I want is only student_id.. the others data from admin and surat table I don't think I need to combine both of them because I need to display almost all of the data inside both table. Quote Link to comment Share on other sites More sharing options...
DavidAM Posted December 20, 2012 Share Posted December 20, 2012 I think you need to learn more about SQL, and databases in general. I don't see any reason for the while loop to run twice. In fact, it is not running twice. The problem is that you are retrieving the data "twice". If you are not using data from the admin table, then take it out of the query. If you are using data from the table, then you need to join that table to one of the other tables in the query. As it is, you are getting a Cartesian product, which will cause the output to look like you are printing each row twice, when in fact you are retrieving all of the data twice. var_dump is a PHP function that prints the contents and data-type of a variable. It is a useful tool for debugging when you want to see what a variable contains. As I stated earlier, it is not going to help you in this case; 1) $result is a resource not all of the data from the query, and 2) the problem is not with any of the variables, the problem is your query. Try running that query directly in mysql and see what the output is. Quote Link to comment Share on other sites More sharing options...
EzwanAbid Posted December 21, 2012 Author Share Posted December 21, 2012 Thank you for replying DavidAM, ok I try this code var_dump ($result); and after that I get this output resource(4) of type (mysql result) resource(4) of type (mysql result) can someone tell me the correct code to "join the condition" like $sql="SELECT * FROM student,surat,admin WHERE student.student_id=surat.student_id"; actually, I'm just a newbie in PHP and MySQL .. some of the code above I take from website and some I make it myself base on tutorials. I don't understand the code "student.student_id=surat.student_id" .. what is the meaning of this code? Quote Link to comment Share on other sites More sharing options...
cpd Posted December 21, 2012 Share Posted December 21, 2012 Sorry for my earlier suggestion I wasn't thinking right. Unless you explain what your expected result set is no-one can really help you write your query. Quote Link to comment Share on other sites More sharing options...
EzwanAbid Posted December 21, 2012 Author Share Posted December 21, 2012 the result I want is to get data and display : Table : admin Column needed : adminfullname , department Table : student Column needed : student_id, fullname, ic_number, course, Table : surat Column needed : id_surat, jenissurat What I'm trying to do here is to display all the data that waiting for the "Letter Process" .. This system is about the "Letter Generator System" which it will bring the data from database and send into Microsoft Word template to be processes. Quote Link to comment Share on other sites More sharing options...
cpd Posted December 21, 2012 Share Posted December 21, 2012 Still not good enough I'm afraid. We need the foreign and primary keys of your database so we can help you with the query. Quote Link to comment Share on other sites More sharing options...
EzwanAbid Posted December 21, 2012 Author Share Posted December 21, 2012 aw I forgot to tell that 1..sorry for that.. Table : admin PK : adminid Table : student PK : student_id Table : surat PK : id_surat FK : student_id Quote Link to comment Share on other sites More sharing options...
Christian F. Posted December 21, 2012 Share Posted December 21, 2012 What's the relationship between admin and the other tables? Quote Link to comment Share on other sites More sharing options...
cpd Posted December 21, 2012 Share Posted December 21, 2012 As far as I can tell there is no relationship between admin and the other two tables (hence Christian's question) and as such you would need a minimum of two queries to retrieve data from both. Quote Link to comment Share on other sites More sharing options...
EzwanAbid Posted December 21, 2012 Author Share Posted December 21, 2012 Thank you for reply me Christian, What's the relationship between admin and the other tables? admin doesn't have any relationship from student and surat table.. ermm :-\ Quote Link to comment Share on other sites More sharing options...
EzwanAbid Posted December 21, 2012 Author Share Posted December 21, 2012 As far as I can tell there is no relationship between admin and the other two tables (hence Christian's question) and as such you would need a minimum of two queries to retrieve data from both. Yes but still I need data from admin table .. so I need another query? Seriously I'm not that good in MySQL code.. so can I use this 1 and just insert it into my code? $newsql=("SELECT *FROM admin WHERE adminfullname,department"); $newresult=mysql_query($newsql); Quote Link to comment Share on other sites More sharing options...
cpd Posted December 21, 2012 Share Posted December 21, 2012 (edited) That query doesn't make sense. If you use a WHERE clause you must give conditions of which you've given none: "SELECT * FROM `admin` WHERE `adminfullname` = 'Something' AND `department` = 'Something'" As for the other query you can do something like: "SELECT * FROM `student` `st` LEFT JOIN `surat` `su` ON `su`.`student_id` = `st`.`student_id`" Edited December 21, 2012 by CPD Quote Link to comment Share on other sites More sharing options...
EzwanAbid Posted December 21, 2012 Author Share Posted December 21, 2012 As for the other query you can do something like: "SELECT * FROM `student` `st` LEFT JOIN `surat` `su` ON `su`.`student_id` = `st`.`student_id`" Thank you for your help. but did you just set a new variable/name for student as 'st' ? and surat as 'su'? in that query? Why must we do so? Quote Link to comment Share on other sites More sharing options...
cpd Posted December 21, 2012 Share Posted December 21, 2012 You don't have to, I just did; its just an alias. Now in your query you can write `st` as opposed to the full `student`, makes life easier. Quote Link to comment Share on other sites More sharing options...
EzwanAbid Posted December 21, 2012 Author Share Posted December 21, 2012 Yes ! They no longer display the result twice.. Thank you for teach me guy... Now I've already learn how use var_dump and some knowledge about SQL Join Query ... Thank you very much ! It really working and I'm so happy you guys can help me ASAP.. I'm really appreciate that because I need to get this work done less in 4 days. .The problem is SOLVED. ! Quote Link to comment 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.