![]() | |
#21
| |||
| |||
|
|
Captain Paralytic: On 30 June, 14:18, "Erick T. Barkhuis" <erick.use-... (AT) ardane (DOT) c.o.m wrote: [...snip valid points against replying...] Points taken, CP. I'll keep them in mind. But... Another point you might notice is that her question is absolutely blatantly obviously a php question, but her understanding is so low that she asks it in a MySQL newsgroup. True. But traffic in this group isn't that high, and her question is not that far off a MySql-topic. We also have questions about network issues and general data modeling issues in this group, which strictly aren't on-topic either. Yet, we tend to discuss them as long as we don't end up talking about Scrabble, Espresso, Politics or Motorbikes. But has that point been reached in this thread, yet? Ahh you think that magically, after 10 years, in this thread she'll suddenly grasp what it is all about! I admire your optimism, but I can't see it happening. :-) One chance a thread, CP. Unless such poster turns out to be a troll. -- Erick |
#22
| |||
| |||
|
|
On Jun 30, 6:56 am, "Erick T. Barkhuis" <erick.use-... (AT) ardane (DOT) c.o.m wrote: Captain Paralytic: On 30 June, 14:18, "Erick T. Barkhuis" <erick.use-... (AT) ardane (DOT) c.o.m wrote: [...snip valid points against replying...] Points taken, CP. I'll keep them in mind. But... Another point you might notice is that her question is absolutely blatantly obviously a php question, but her understanding is so low that she asks it in a MySQL newsgroup. True. But traffic in this group isn't that high, and her question is not that far off a MySql-topic. We also have questions about network issues and general data modeling issues in this group, which strictly aren't on-topic either. Yet, we tend to discuss them as long as we don't end up talking about Scrabble, Espresso, Politics or Motorbikes. But has that point been reached in this thread, yet? Ahh you think that magically, after 10 years, in this thread she'll suddenly grasp what it is all about! I admire your optimism, but I can't see it happening. :-) One chance a thread, CP. Unless such poster turns out to be a troll. -- Erick Okay, the confusing error message yesterday was because I made the correct change on the server with my IDE but they weren't saved on the server even though it was saved in my IDE running on the server because they were doing maintenance I didn't know about.. So I got the error message that didn't make sense. The nrows function is to get a count. The input of the function is the $sql. That result gets assigned to $nRecs so I can use it to display records in a page. I got the error message for the double where clause so I took out all the sqlWhere and concatenated everything to the $sql. Which works but it is harder to see what the sql is doing. I couldn't figure out how to get it concatenated together that way. so I added the first $sqlWhere to the $sql and used the .$sqlWhere for the filters and it removed the extra Where clause. I also noticed there was a space after the FROM in the nrows function strpos line and took it out. and works. $sql="SELECT COUNT(*) AS cnt " . substr($sql, strpos($sql,"FROM"));***** But in order to learn from this I would like to understand the nrows function. I wrote a test page using the function. ?php /* * To change this template, choose Tools | Templates * and open the template in the editor. * */ $dbname= "pharm_cart_order"; $link = mysql_connect('10.237.82.232', 'root', ''); mysql_select_db($dbname); if ( !mysql_select_db($dbname,$link) ) { echo 'Could not select database<br />'; exit; } $sql="SELECT DISTINCT bu, cart_id, nationalDrugCode, error_flag, item_id, description, upc, ndc_uom, std_uom FROM cart_inventory, onelink WHERE nationalDrugCode=ndc AND bu ='08010' ORDER BY bu LIMIT 100,100"; $result=mysql_query($sql); if ( !$result ) {die("<font color='red'>Invalid query:</font>" . mysql_error() . "<br />$sql");} echo nrows($sql); function nrows($sql) { $sql="SELECT COUNT(*) AS cnt " . substr($sql, strpos($sql,"FROM")); $result=mysql_query($sql); if ( !$result ) {die("<font color='red'>Invalid query:</font>" . mysql_error() . "<br>$sql");} $dbarray = mysql_fetch_array($result); $nCnt=$dbarray['cnt']; $cnt=($nCnt==1) ? "1 Record" : "$nCnt"; return $cnt; }//end nrows ? I would like to verify it with a query in the query browser So I see that the substring with strpos replaces part of the select with the COUNT(*). But does that give the count of the DISTINCT rows? I need the count to be verified. Thanks for your patience. |
#23
| ||||||||||
| ||||||||||
|
|
But in order to learn from this I would like to understand the nrows function. I wrote a test page using the function. ?php /* * To change this template, choose Tools | Templates * and open the template in the editor. * */ $dbname= "pharm_cart_order"; $link = mysql_connect('10.237.82.232', 'root', ''); mysql_select_db($dbname); if ( !mysql_select_db($dbname,$link) ) { echo 'Could not select database<br />'; exit; } |
|
$sql="SELECT DISTINCT bu, cart_id, nationalDrugCode, error_flag, item_id, description, upc, ndc_uom, std_uom FROM cart_inventory,onelink WHERE nationalDrugCode=ndc AND bu ='08010' ORDER BY bu LIMIT 100,100"; |
|
echo nrows($sql); |
|
function nrows($sql) { |
|
$sql="SELECT COUNT(*) AS cnt " . substr($sql, strpos($sql,"FROM")); |
|
$result=mysql_query($sql); |
|
$dbarray = mysql_fetch_array($result); $nCnt=$dbarray['cnt']; $cnt=($nCnt==1) ? "1 Record" : "$nCnt"; return $cnt; }//end nrows ? |
|
I would like to verify it with a query in the query browser So I see that the substring with strpos replaces part of the select with the COUNT(*). But does that give the count of the DISTINCT rows? |
|
I need the count to be verified. |
|
The nrows function is to get a count. The input of the function is the $sql. That result gets assigned to $nRecs so I can use it to display records in a page. |
#24
| |||
| |||
|
|
JRough: But in order to learn from this I would like to understand the nrows function. I wrote a test page using the function. ?php /* * To change this template, choose Tools | Templates * and open the template in the editor. * */ $dbname= "pharm_cart_order"; $link = mysql_connect('10.237.82.232', 'root', ''); mysql_select_db($dbname); if ( !mysql_select_db($dbname,$link) ) { * echo 'Could not select database<br */>'; * exit; } In the above, do you see that you select the database twice? $sql="SELECT DISTINCT bu, cart_id, nationalDrugCode, error_flag, item_id, description, upc, ndc_uom, std_uom FROM cart_inventory,onelink WHERE nationalDrugCode=ndc AND bu ='08010' * ORDER BY bu LIMIT 100,100"; [...] echo nrows($sql); function nrows($sql) { The function's only parameter is a string, called $sql, containing a SELECT query. * * * *$sql="SELECT COUNT(*) AS cnt " . substr($sql, strpos($sql,"FROM")); This is very confusing, because you are using a local variable $sql with the same name as the function's parameter $sql. Let's rewrite it as I think you want to use it: * $newSql="SELECT COUNT(*) AS cnt " . substr($sql,strpos($sql,"FROM")); so # $newSql will contain # * *"SELECT COUNT(*) AS cnt * # * * * *FROM cart_inventory,onelink # * * * *WHERE nationalDrugCode=ndc AND bu ='08010' * # * * * *ORDER BY bu # * * * *LIMIT 100,100"; I wouldn't know why you are using ORDER BY here, because it doesn't affect the result. But anyway: * * * *$result=mysql_query($sql); Would now be: * * * * *$result=mysql_query($newSql); * * * $dbarray = mysql_fetch_array($result); * * * *$nCnt=$dbarray['cnt']; * * * *$cnt=($nCnt==1) ? "1 Record" : "$nCnt"; * * * *return $cnt; }//end nrows ? This should indeed return either "1 Record" or a string containing a number. I would like to verify it with a query in the query browser So I see that the substring with strpos replaces part of the select with the COUNT(*). *But does that give the count of the DISTINCT rows? No. If you want to verify that part of the original query is replaced by something else, you should simply print the initial string and the result string. You don't have to really send the query to mysql to see it. You could include into your function, after * $newSql="SELECT COUNT(*) AS cnt " . substr($sql,strpos($sql,"FROM")); the following line: echo "My original query: ".$sql."<br>"; echo "and now it is: ".$newSql; exit; I need the count to be verified. Verified with what? Do you have a reliable number, that you want to match the function's return value? You could take $newSql, feed Query Browser or phpMyAdmin with it, and get the result. Then echo the function's output, and see if the result is the same. I bet it is. ---- Now, you wrote: The nrows function is to get a count. *The input of the function is the $sql. That result gets assigned to $nRecs so I can use it to display records in a page. So, you want the number of returned tuples from the query, which contains a LIMIT clause, right? Have you considered usinghttp://php.net/manual/en/function.mysql-num-rows..php If you want the total number of records, as if there was no LIMIT clause, then seehttp://dev.mysql.com/doc/refman/5.0/en/information-functions.html#fun... In fact, I don't think you need to write functions that do the counting, when MySQL will return that number upon request. -- Erick' Erick |
#25
| |||
| |||
|
|
Which works but it is harder to see what the sql is doing. I couldn't figure out how to get it concatenated together that way. so I added the first $sqlWhere to the $sql and used the .$sqlWhere for the filters and it removed the extra Where clause. I also noticed there was a space after the FROM in the nrows function strpos line and took it out. and works. $sql="SELECT COUNT(*) AS cnt " . substr($sql, strpos($sql,"FROM"));***** |
#26
| |||
| |||
|
|
$sql="SELECT COUNT(*) AS cnt " . substr($sql, strpos($sql,"FROM"));***** Note that this is going to malfunction for certain queries which contain the string "FROM" before the actual FROM keyword, such as: SELECT FROMAGE, EMAIL_ENVELOPE_FROM_ADDRESS, AFROMAN FROM CHEESES WHERE ... ; |
#27
| |||
| |||
|
|
But in order to learn from this I would like to understand the nrows function. |
#28
| |||
| |||
|
|
In article <a88dd019-9719-4ee4-a8cb-52a893149... (AT) k39g2000yqd (DOT) googlegroups.com>, JRough <jlro... (AT) yahoo (DOT) com> wrote: But in order to learn from this I would like to understand the nrows function. This is the most puzzling part of the entire thread. Why do you need help "to understand the nrows function"? It's *your* function, isn't it?? |
#29
| |||
| |||
|
|
Which works but it is harder to see what the sql is doing. *I couldn't figure out how to get it concatenated together that way. so I *added the first $sqlWhere to the $sql and used the .$sqlWhere for the filters and it removed the extra Where clause. I also noticed there was a space after the FROM *in the nrows function strpos line and took it out. and *works. $sql="SELECT COUNT(*) AS cnt " . substr($sql, strpos($sql,"FROM"));***** Note that this is going to malfunction for certain queries which contain the string "FROM" before the actual FROM keyword, such as: SELECT FROMAGE, EMAIL_ENVELOPE_FROM_ADDRESS, AFROMAN FROM CHEESES WHERE ... *; |
#30
| |||||||
| |||||||
|
| I rewrote it with SQL_CALC_FOUND_ROWS and SELECT_FOUND_ROWS() function. |
|
Where do I pull the sql query with the limit? |
|
I did it here with the limit in the first $result. |
|
Then as I understand it the 2nd result is without the limit using FOUND_ROWS()? |
|
The other question is the $sql query gets a syntax error because it has a minus sign displaying at the end of the query where the LIMIT should be ie, LIMIT -100,100 instead of LIMIT 100,100. How to get rid of the minus sign? |
|
$page_limit=100; $page=$_GET['page']; if($_POST['search_bu'] && $page=='' ){$page=1;} |
|
$limit_start= ($page * $page_limit) -$page_limit; |
![]() |
| Thread Tools | |
| Display Modes | |
| |