I don't know what's wrong with this code. I think this code should work. I'm about to store number of each query in array

$antecedent=array();

for($i=0;$i<=$index;$i++){
	if(isset($period[$i])|| isset($gpa[$i]) || isset($antecedent[$i])){
		echo $period[$i]."&nbsp;";
		echo $gpa[$i]."<br/>";
  $queryAntecedent=mysql_query("SELECT * FROM mytable WHERE  study_period='$period[$i]' AND ipk='$gpa[$i]'") or die (mysql_error());	
			
		$antecedent[$i]=mysql_num_rows($queryAntecedent);
		}//endif
}//endfor

print_r ($antecedent);

when i print_r $antecedent, its output is all zero for every element. i've tried to run the query in phpmyadmin and the result is not zero..i got some rows of result.

i've echo $period[$i] and $gpa[$i], it printed the results.So, i think there's no problem with the the values of those two array.

So, why can't i get the total rows of query? why all element in antecedent array only gives me 0 number?Please help!

Thank you :)

Dani AI

Generated

: the loop and mysql_num_rows behavior you described usually means the SQL you think you sent is not the SQL the server actually received. and were right to look at interpolation and to test single values in phpMyAdmin β€” the next step is to prove what PHP is sending and to reveal any hidden characters or type/format differences.

A compact debugging checklist to run inside the loop:

  • Dump raw values and types so trailing spaces or non-printables are obvious.
  • Show the hex bytes of suspect values to catch invisible characters.
  • Log the exact SQL string you would run in phpMyAdmin (build a safe debug string, not the production one).

Example debug snippets:

var_dump($period[$i], $gpa[$i]);
error_log('DELIMITED: [' . $period[$i] . '] [' . $gpa[$i] . ']');
error_log('GPA HEX: ' . bin2hex($gpa[$i]));

If the debug shows values that match what you paste into phpMyAdmin, but the result still differs when run from PHP, normalize the inputs before querying (trim, remove NBSP, normalize decimal separators) and prefer a prepared statement so types are handled predictably.

A simple, safer approach using PDO and getting the count directly:

$stmt = $pdo->prepare('SELECT COUNT(*) FROM mytable WHERE study_period = :p AND ipk = :g');
$stmt->execute(['p' => trim($period[$i]), 'g' => trim($gpa[$i])]);
$antecedent[$i] = (int) $stmt->fetchColumn();

Notes: migrating off the old mysql extension avoids subtle quoting/encoding issues and is future-proof. For reference, see the PDO prepare docs and var_dump/bin2hex for debugging: PDO::prepare var_dump bin2hex.

Recommended Answers

All 5 Replies

This happens because you can't access an array's members directly inside a double-quoted string.

You can either try like this

study_period='{$period[$i]}'

that might work, I'm not sure.

Or just do it like this

study_period='".$period[$i]."'"

which will work for sure.

: yes, first example will work.

Thanks for the suggestion, Insensus.i don't know.. but those two ways didn't work :(

must check the code over and over again.

another helps and suggestions are well-appreciated ;)

I think this is a bit strange..

when i only put one array in query sintax,like this...

$queryAntecedent=mysql_query("SELECT * FROM mytable WHERE  study_period='$period[$i]'") or die (mysql_error());

the rest of code works.

but, when i put two arrays in query (like previous post), the rest of code doesn't work. How to solve this?

Thank you

Try taking one specific case of your query (choose a $period[$i] and $gpa[$i]) and run that query in phpmyadmin.
If it doesn't return anything there's obviously something wrong with your query.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.