Jump to content

Recommended Posts

<?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...

Link to comment
https://forums.phpfreaks.com/topic/260962-php-to-mysql/
Share on other sites

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).

Link to comment
https://forums.phpfreaks.com/topic/260962-php-to-mysql/#findComment-1337556
Share on other sites

$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?

Link to comment
https://forums.phpfreaks.com/topic/260962-php-to-mysql/#findComment-1337582
Share on other sites

$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?.

Link to comment
https://forums.phpfreaks.com/topic/260962-php-to-mysql/#findComment-1337599
Share on other sites

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
  }
?>

Link to comment
https://forums.phpfreaks.com/topic/260962-php-to-mysql/#findComment-1337601
Share on other sites

<?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

Link to comment
https://forums.phpfreaks.com/topic/260962-php-to-mysql/#findComment-1337603
Share on other sites

$_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

Link to comment
https://forums.phpfreaks.com/topic/260962-php-to-mysql/#findComment-1337604
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/260962-php-to-mysql/#findComment-1337607
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.