PHP/Mysql: Why only one row returned?

I have the following code

<script>
myArray = new Array();
<?
$whereStmt = ' ID="'.$ID.'" AND idx="'.addslashes($passed).'"'; // ID=4, idx = 310;
$selectSql = 'SELECT * FROM myTable WHERE '.$whereStmt ;
$Result    = $MyDb->f_ExecuteSql($selectSql);
$Resultset = $MyDb->f_GetRecord($Result);

$ResultRowNr    = $MyDb->f_GetSelectedRows($Result);

$counter=0;

echo "\n// sql:".$selectSql."\n"; // shows // sql:SELECT * FROM myTable WHERE  ID="4" AND idx="310"

while ($Resultset = $MyDb->f_GetRecord($Result)) {
?>
  myArray[<? echo $counter; ?>] = '<? echo $Resultset['title']; ?>';
<?
  $counter++;
}
?>


If I in a separate program I have


$whereStmt = ' ID = "'.&ID.'"; // id = 4

$selectSql = 'SELECT * FROM myTable WHERE '.$whereStmt ;

$Result    = $MyDb->f_ExecuteSql($selectSql);
$recordcount = $MyDb->f_GetSelectedRows();
if ($recordcount==0) {
  echo "After update/insert, I cannot find a record where ".$whereStmt;
  die;
}

echo "<br>Looked up the records after insert/update: ID: ".$ID;

while($row = mysql_fetch_array($Result)) {
  echo "<br>title:".$row['title']." idx: ".$row['idx'];
}



title:Test310Updated idx: 310

title:Test310Extra_updated idx: 310

In the first prgrams I only get to see the second title. E.g. the result is

  myArray[0] = 'Test310Extra_updated';

and not

  myArray[0] = 'Test310Extra';
  myArray[1] = 'Test310Extra_updated';


So it skips the first.

Does
$ResultRowNr    = $MyDb->f_GetSelectedRows($Result);

have something to do with it? - I do not use it

Also what is the better method? resultSet or fetch?

thanks
LVL 75
Michel PlungjanIT ExpertAsked:
Who is Participating?
 
elfe69Commented:
In the first program, you call f_GetRecord() a first time just after f_ExecuteSql(), which advances to the second row before you start the while loop, remove that line.


0
 
elfe69Commented:
I think you should use $MyDb->f_getRecord() as you are using a class for database connection, it will make your code more readable and avoid the mix between object-oriented and function-oriented code, but both would be OK, it is just a question of estheticism.
0
 
theevilwormCommented:
i don't know what kind of object  $MyDb is but the f_GetRecord() method might be returning only one row.
i asume f_ExecuteSql() returns the result resource so you can change your while block into:

while ($Resultset = mysql_fetch_assoc($Result)) {
//do stuff
}

i would also recommend you change the name of $Resultset as it implies it contains the entire result but it will only contain one row at a time.
0
 
Michel PlungjanIT ExpertAuthor Commented:
Thanks

I in the meantime changed the code to use
while($row = mysql_fetch_array($Result)) {

and that solved it.

Too many ways to skin a cat in php I think...

the code has been copied and recopied by the maker a few times and some extra stuff was never removed

Thanks for helping

Michel

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.