Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

PHP/Mysql: Why only one row returned?

Posted on 2007-07-20
4
Medium Priority
?
459 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:Michel Plungjan
  • 2
4 Comments
 
LVL 11

Accepted Solution

by:
elfe69 earned 1600 total points
ID: 19529586
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
 
LVL 11

Assisted Solution

by:elfe69
elfe69 earned 1600 total points
ID: 19529649
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
 
LVL 3

Assisted Solution

by:theevilworm
theevilworm earned 400 total points
ID: 19530107
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
 
LVL 75

Author Comment

by:Michel Plungjan
ID: 19530398
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question