Jump to content

MySQL query row count


adam525

Recommended Posts

I have a problem getting the number of rows from a mysql query.  I'm working with code that was written by someone else and there are multiple queries inside a while loop.  Here is the code...

 

__SNIP__

 

      if ($x == TRANSSOL){

 

          $closefile = TRUE;

          $data = fgetcsv($myFile, 2048, $delimiter);

          $codate = convert_date($data[$fieldArray[1][1]]);

          $udprofit1="SELECT TRANSSOL.mid, TRANSSOL.period, SUM(profit) AS tsprofit, SUM(comm) AS tscomm FROM TRANSSOL WHERE TRANSSOL.period = '{$codate}' GROUP BY TRANSSOL.mid";

          $sql="SELECT PROFIT.ReportDate, PROFIT.mid FROM PROFIT, TRANSSOL WHERE PROFIT.ReportDate = '{$codate}' AND PROFIT.mid = TRANSSOL.mid GROUP BY PROFIT.mid";

                                $sqlpft = mysql_query($sql);

                                $sqldte = mysql_fetch_array($sqlpft);

                                $sqltsdte = $sqldte[0];

                                $sqltsmid = $sqldte[1];

 

                                $udprofit2 = mysql_query($udprofit1);

                                        while ($udrow = mysql_fetch_array($udprofit2)){

                                            $tsmid = $udrow[0];

                                            $tsdte = $udrow[1];

                                            $profit = $udrow['ccprofit'];

                                            $comm = $udrow['cccomm'];

                                                                //$udprofit = mysql_query("INSERT INTO PROFIT (mid,ImportDate,ReportDate) VALUES ('{$tsmid}','{$now}','{$codate}')");

                                mysql_query("INSERT INTO PROFIT (mid, ReportDate) SELECT '{$tsmid}', '{$tsdte}' FROM dual

                                                        WHERE not exists (select * from PROFIT where PROFIT.mid = '{$tsmid}' and PROFIT.ReportDate = '{$tsdte}')");

 

 

                                                        $gresult = mysql_query("UPDATE PROFIT SET PROFIT.ccprofit = '{$profit}' WHERE PROFIT.mid = '{$tsmid}' AND PROFIT.ReportDate = '{$tsdte}' ");

                                                        $gresult1 = mysql_query("UPDATE PROFIT SET PROFIT.cccomm = '{$comm}' WHERE PROFIT.mid = '{$tsmid}' AND PROFIT.ReportDate = '{$tsdte}'");

//ADAM

//$updatetstype = mysql_query("UPDATE PROFIT SET PROFIT.type = 'CC' WHERE PROFIT.mid = '{$tsmid}' AND PROFIT.ReportDate = '{$tsdte}'");

$updatetstype = mysql_query("UPDATE PROFIT SET PROFIT.type = 'TS' WHERE PROFIT.mid = '{$tsmid}' AND PROFIT.ReportDate = '{$tsdte}'");

                                          }

            //mysql_query("DELETE FROM PROFIT WHERE mid = '{$sqltsmid}' AND ReportDate = '{$codate}' LIMIT 1");

 

        $tsresultprofit = mysql_query("UPDATE PROFIT,MERCHANT SET PROFIT.mname = MERCHANT.mname WHERE PROFIT.mid = MERCHANT.mid AND PROFIT.ReportDate = '{$codate}'");

 

}

 

__SNIP__

 

I want to return the number of records updated from this query...

 

$updatetstype = mysql_query("UPDATE PROFIT SET PROFIT.type = 'TS' WHERE PROFIT.mid = '{$tsmid}' AND PROFIT.ReportDate = '{$tsdte}'");

 

and I've tried every way that I can imagine.

 

If I use mysql_affected_rows under that query, like so...

 

$updatetstype = mysql_query("UPDATE PROFIT SET PROFIT.type = 'TS' WHERE PROFIT.mid = '{$tsmid}' AND PROFIT.ReportDate = '{$tsdte}'");

$tempval = mysql_affected_rows();

 

I get that it's not a valid mysql resource 20 times (which is the number of rows that I'm looking for).

 

I've also tried counting the number of elements in the array and I get 20 1's returned. 

 

What would be the best way to get the number of records from that query?

 

TIA,

 

Adam

Link to comment
Share on other sites

To the little guy:

 

This is an update statement though and mysql_affected_rows is not working. 

 

If I use this code :

 

$updatetstype = mysql_query("UPDATE PROFIT SET PROFIT.type = 'TS' WHERE PROFIT.mid = '{$tsmid}' AND PROFIT.ReportDate = '{$tsdte}'");

$tempval = mysql_affected_rows($updatetstype);

echo $tempval;

 

I get :

 

Warning: mysql_affected_rows(): supplied argument is not a valid MySQL-Link resource in /var/www/csv_importer.php on line 2254

 

20 times.  I get that 20 times (I know) because I have the echo inside the while loop, but I still dont know why mysql_affected_rows isn't working.

 

I would even settle for being able to count the number of elements in the returned array and that's not working.

 

 

 

 

Link to comment
Share on other sites

revraz:

 

Here's the output I get using

 

printf("Records Updated: %d\n", mysql_affected_rows());

echo $updatetstype;

 

Records Updated: 1 1Records Updated: 1 1Records Updated: 1 1Records Updated: 1 1Records Updated: 1 1Records Updated: 1 1Records Updated: 1 1Records Updated: 1 1Records Updated: 1 1Records Updated: 1 1Records Updated: 1 1Records Updated: 1 1Records Updated: 1 1Records Updated: 1 1Records Updated: 1 1Records Updated: 1 1Records Updated: 1 1Records Updated: 1 1Records Updated: 1 1Records Updated: 1 1

Link to comment
Share on other sites

I would then need to pass the mysql_affected_rows function the resource id like...

 

$updatetstype = mysql_query("UPDATE PROFIT SET PROFIT.type = 'TS' WHERE PROFIT.mid = '{$tsmid}' AND PROFIT.ReportDate = '{$tsdte}'");

$tempval = mysql_affected_rows($updatetstype);

} //(end of loop)

echo $tempval

 

I then get..

 

Warning: mysql_affected_rows(): supplied argument is not a valid MySQL-Link resource in /var/www/csv_importer.php on line 2254

 

Link to comment
Share on other sites

It looks like it's out of scope after the loop.  If I do this (just to see if there's still anything there)...

 

$updatetstype = mysql_query("UPDATE PROFIT SET PROFIT.type = 'TS' WHERE PROFIT.mid = '{$tsmid}' AND PROFIT.ReportDate = '{$tsdte}',$dbcon");

$tsupdate = mysql_affected_rows();

}

echo $tsupdate;

 

I get:

 

-1

Link to comment
Share on other sites

Because you reset $countit to 0 each time the loop passes.  Set it once prior to the loop.

 

Also, you dont want to use $countit++ because that just increments that the loop went through, not if it was actually updated.  You want to capture the printf output, not increment it with ++.

Link to comment
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.