acap89 Posted April 15, 2012 Share Posted April 15, 2012 <?php $un = $_POST['Username']; // connect to the db $mysql_host = "localhost"; $mysql_database = "****"; $mysql_user = "*****"; $mysql_password = "*****"; $conn = mysql_connect($mysql_host,$mysql_user,$mysql_password); mysql_select_db($mysql_database,$conn); $query = "SELECT * FROM student WHERE Username='$un'"; $result = mysql_query($query); //Store all the course code into an array $course_code = mysql_fetch_array($result); //verification if(mysql_num_rows($result) == 1) { echo 1; // for correct login response $format = ".txt"; $output = $un.$format; $fh = fopen($output,'w') or die ("Can't open file"); for($num=1;$num<=5;$num++) { $code = $course_code['Course'.$num]; fwrite($fh,$code."\r\n"); } chmod($output,0777); //chmod the file to 0777 fclose($fh); } else { echo 0; // for incorrect login response } ?> im doing the Learning Management System application. this FirstTimeUser.php will connect to the database and list out the coursecode in .txt take a look at my code, the output is always 0. I already stuck on how to get 1. The connection for the php to mysql is correct. the username also exist in the database. is there any suggestion on how to debug it. i dont know, its like a simple coding, but its not working... please leave some suggestion... Quote Link to comment https://forums.phpfreaks.com/topic/260962-php-to-mysql/ Share on other sites More sharing options...
Sajesh Mohan Posted April 15, 2012 Share Posted April 15, 2012 can u try if(mysql_num_rows($result) == '1') Quote Link to comment https://forums.phpfreaks.com/topic/260962-php-to-mysql/#findComment-1337482 Share on other sites More sharing options...
acap89 Posted April 15, 2012 Author Share Posted April 15, 2012 still got 0... Quote Link to comment https://forums.phpfreaks.com/topic/260962-php-to-mysql/#findComment-1337483 Share on other sites More sharing options...
MMDE Posted April 15, 2012 Share Posted April 15, 2012 Replace: $result = mysql_query($query); With this: $result = mysql_query($query) or die(mysql_error()); echo mysql_num_rows($result); What does it say now? Quote Link to comment https://forums.phpfreaks.com/topic/260962-php-to-mysql/#findComment-1337519 Share on other sites More sharing options...
acap89 Posted April 15, 2012 Author Share Posted April 15, 2012 it shows 40 i think 4 from the total username in database, 0 from the echo 0 Quote Link to comment https://forums.phpfreaks.com/topic/260962-php-to-mysql/#findComment-1337544 Share on other sites More sharing options...
MMDE Posted April 15, 2012 Share Posted April 15, 2012 it shows 40 i think 4 from the total username in database, 0 from the echo 0 Okay, if the 4 is from the echo mysql_num_rows($result); then that means this code: if(mysql_num_rows($result) == 1) { ... } else { echo 0; // for incorrect login response } will of course result in 0. So with other words, your query results in 4 results, which is strange unless there are 4 rows where the column Username has the same value ($un). Quote Link to comment https://forums.phpfreaks.com/topic/260962-php-to-mysql/#findComment-1337556 Share on other sites More sharing options...
acap89 Posted April 15, 2012 Author Share Posted April 15, 2012 $un = $_POST['Username']; how about this one... 'Username' is from the android apps login that going to this FirstTimeUser.php the total user in the databse is 4, so the rows is 4 Quote Link to comment https://forums.phpfreaks.com/topic/260962-php-to-mysql/#findComment-1337570 Share on other sites More sharing options...
MMDE Posted April 15, 2012 Share Posted April 15, 2012 $un = $_POST['Username']; how about this one... 'Username' is from the android apps login that going to this FirstTimeUser.php the total user in the databse is 4, so the rows is 4 No, the mysql_num_rows() function returns the amount of rows in the result, not in the database. It means that your query resulted in 4 rows (4 results WHERE Username=$un). Replace the $result = mysql_query($query) or die(mysql_error()); part of your code with this: echo '$_POST[\'Username\'] = '.$_POST['Username'].'<br/>'; echo '$un = '.$un.'<br/>'; echo '$query = '.$query.'<br/>'; $result = mysql_query($query) or die(mysql_error()); echo mysql_num_rows($result).'<br/>'; while($row = mysql_fetch_assoc($result)){ $result_array[] = $row; } print_r($result_array); Sorry, I added the fetch stuff later on, but it is important. Tell us exactly what it says now? Quote Link to comment https://forums.phpfreaks.com/topic/260962-php-to-mysql/#findComment-1337582 Share on other sites More sharing options...
acap89 Posted April 15, 2012 Author Share Posted April 15, 2012 nothing appear.... just a blank page... Quote Link to comment https://forums.phpfreaks.com/topic/260962-php-to-mysql/#findComment-1337595 Share on other sites More sharing options...
acap89 Posted April 15, 2012 Author Share Posted April 15, 2012 $un = $_POST['Username']; how about this one... 'Username' is from the android apps login that going to this FirstTimeUser.php the total user in the databse is 4, so the rows is 4 No, the mysql_num_rows() function returns the amount of rows in the result, not in the database. It means that your query resulted in 4 rows (4 results WHERE Username=$un). means, there nothing wrong with database right?. Quote Link to comment https://forums.phpfreaks.com/topic/260962-php-to-mysql/#findComment-1337599 Share on other sites More sharing options...
jcbones Posted April 15, 2012 Share Posted April 15, 2012 acapa89, post your current code, a blank page means an error, and there shouldn't be one in the code MMDE provided. We need to verify that it was placed correctly. Better yet, try this. <?php error_reporting(-1); ini_set('display_errors',1); $un = $_POST['Username']; // connect to the db $mysql_host = "localhost"; $mysql_database = "****"; $mysql_user = "*****"; $mysql_password = "*****"; $conn = mysql_connect($mysql_host,$mysql_user,$mysql_password); mysql_select_db($mysql_database,$conn); $query = "SELECT * FROM student WHERE Username='$un'"; echo '$_POST[\'Username\'] = '.$_POST['Username'].'<br/>'; echo '$un = '.$un.'<br/>'; echo '$query = '.$query.'<br/>'; $result = mysql_query($query) or die(mysql_error()); echo mysql_num_rows($result).'<br/>'; while($row = mysql_fetch_assoc($result)){ $result_array[] = $row; } print_r($result_array); //Store all the course code into an array $course_code = mysql_fetch_array($result); //verification if(mysql_num_rows($result) == 1) { echo 1; // for correct login response $format = ".txt"; $output = $un.$format; $fh = fopen($output,'w') or die ("Can't open file"); for($num=1;$num<=5;$num++) { $code = $course_code['Course'.$num]; fwrite($fh,$code."\r\n"); } chmod($output,0777); //chmod the file to 0777 fclose($fh); } else { echo 0; // for incorrect login response } ?> Quote Link to comment https://forums.phpfreaks.com/topic/260962-php-to-mysql/#findComment-1337601 Share on other sites More sharing options...
acap89 Posted April 15, 2012 Author Share Posted April 15, 2012 <?php $un = $_POST['Username']; // connect to the db $mysql_host = "localhost"; $mysql_database = "****"; $mysql_user = "*****"; $mysql_password = "*****"; $conn = mysql_connect($mysql_host,$mysql_user,$mysql_password); mysql_select_db($mysql_database,$conn); $query = "SELECT * FROM student WHERE Username=149090"; //$result = mysql_query($query); /*$result = mysql_query($query) or die(mysql_error()); echo mysql_num_rows($result);*/ //Store all the course code into an array //$course_code = mysql_fetch_array($result); echo '$_POST[\'Username\'] = '.$_POST['Username'].'<br/>'; echo '$un = '.$un.'<br/>'; echo '$query = '.$query.'<br/>'; $result = mysql_query($query) or die(mysql_error()); echo mysql_num_rows($result).'<br/>'; while($row = mysql_fetch_assoc($result)){ $result_array[] = $row; } print_r($result_array); //verification if(mysql_num_rows($result) == 1) { echo 1; // for correct login response $format = ".txt"; $output = $un.$format; $fh = fopen($output,'w') or die ("Can't open file"); for($num=1;$num<=5;$num++) { $code = $course_code['Course'.$num]; fwrite($fh,$code."\r\n"); } chmod($output,0777); //chmod the file to 0777 fclose($fh); } else { echo 0; // for incorrect login response } ?> its appear this in my browser if use this code $_POST['Username'] = $un = $query = SELECT * FROM student WHERE Username=149090 1 Array ( [0] => Array ( [Name] => asyraf [MatricNo] => 149090 [username] => 149090 [Password] => 149090 [Course1] => ECC3302 [Course2] => ECC4205 [Course3] => ECC4204 [Course4] => ECC4201 [Course5] => ) ) 1 Quote Link to comment https://forums.phpfreaks.com/topic/260962-php-to-mysql/#findComment-1337603 Share on other sites More sharing options...
acap89 Posted April 15, 2012 Author Share Posted April 15, 2012 $_POST['Username'] = $un = $query = SELECT * FROM student WHERE Username=149090 1 Array ( [0] => Array ( [Name] => asyraf [MatricNo] => 149090 [username] => 149090 [Password] => 149090 [Course1] => ECC3302 [Course2] => ECC4205 [Course3] => ECC4204 [Course4] => ECC4201 [Course5] => ) ) 1 this output on my browser Quote Link to comment https://forums.phpfreaks.com/topic/260962-php-to-mysql/#findComment-1337604 Share on other sites More sharing options...
acap89 Posted April 15, 2012 Author Share Posted April 15, 2012 acapa89, post your current code, a blank page means an error, and there shouldn't be one in the code MMDE provided. We need to verify that it was placed correctly. Better yet, try this. <?php error_reporting(-1); ini_set('display_errors',1); $un = $_POST['Username']; // connect to the db $mysql_host = "localhost"; $mysql_database = "****"; $mysql_user = "*****"; $mysql_password = "*****"; $conn = mysql_connect($mysql_host,$mysql_user,$mysql_password); mysql_select_db($mysql_database,$conn); $query = "SELECT * FROM student WHERE Username='$un'"; echo '$_POST[\'Username\'] = '.$_POST['Username'].'<br/>'; echo '$un = '.$un.'<br/>'; echo '$query = '.$query.'<br/>'; $result = mysql_query($query) or die(mysql_error()); echo mysql_num_rows($result).'<br/>'; while($row = mysql_fetch_assoc($result)){ $result_array[] = $row; } print_r($result_array); //Store all the course code into an array $course_code = mysql_fetch_array($result); //verification if(mysql_num_rows($result) == 1) { echo 1; // for correct login response $format = ".txt"; $output = $un.$format; $fh = fopen($output,'w') or die ("Can't open file"); for($num=1;$num<=5;$num++) { $code = $course_code['Course'.$num]; fwrite($fh,$code."\r\n"); } chmod($output,0777); //chmod the file to 0777 fclose($fh); } else { echo 0; // for incorrect login response } ?> already do that, the error appear= Server error The website encountered an error while retrieving http://www.pskk.org/LMS/LMSscripts/FirstTimeUser.php?Username=143409. It may be down for maintenance or configured incorrectly. Quote Link to comment https://forums.phpfreaks.com/topic/260962-php-to-mysql/#findComment-1337607 Share on other sites More sharing options...
acap89 Posted April 17, 2012 Author Share Posted April 17, 2012 How about POST in there.. is that right?... i think it should be GET.. i dont know the GET and POST concept...can someone elaborate Quote Link to comment https://forums.phpfreaks.com/topic/260962-php-to-mysql/#findComment-1338126 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.