Crazy php script behaivor

See attached php script.

Note on line 16, I am restricting the range variable $cid to between 15000 & 16000.

What I really want to do is not restrict it AT ALL let it run from the first value to the last.

By trial & error, I learned that if I restrict the range to 10000, it works.

If I up it to 2000 (say 15000 to 17000), the script just spins a while and then gives NO result.

The file Customer.txt is a tab delimited file saved from a spreadsheet.

Why can't I do more than 1000 at a time?
chk-miss-cust5.php
Richard KortsBusiness Owner / Chief DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
Some comments on the code

1. You are using the mysql library which has been deprecated - it is advised you move on to mysqli or PDO

2. Any reason you cannot use LOAD DATA INFILE?

https://dev.mysql.com/doc/refman/5.1/en/load-data.html

To clarify your question are you saying that if you remove the if statement then the script does not work?
Ray PaseurCommented:
Here is how to do the required conversion from MySQL.  You really want to get this done sooner rather than later when it becomes a "my site is down" emergency!
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html

Here are some suggestions worth trying.  Please see the comments in the code.  This is just a guess - untested code.  If all you want are counts of identical cid values, you might be able to get MySQL to count for you with SELECT COUNT().  Running one query to count is likely to be a lot faster than running thousands of queries and counting in PHP.  A query like this might do the trick:
SELECT cid, COUNT(cid) AS count FROM customer GROUP BY cid ORDER BY cid
<?php // demo/temp_rkorts.php

// ALWAYS RAISE THE ERROR REPORTING LEVEL TO THE HIGHEST POSSIBLE
error_reporting(E_ALL);
ini_set('display_errors', TRUE);

// connect database
// CHANGE THE CONNECTION SCRIPT TO ADD A MYSQLI CONNECTION
include "db_connect.php";

$thefile = "upload/Customers.txt";

// NO NEED FOR FOPEN WHEN USING THIS FUNCTION
$data = file($thefile);
$nl = count($data);
    
// SLICE OUT THE PART WE WANT - SET WHATEVER VALUES MAKE SENSE
$data = array_slice($data, 15000, 1000); 

echo "MAX records = $nl <br>";
echo "NUM PROCESSED RECORDS = " . count($data) . '<br>';

// PROCESS THE RECORDS
foreach ($data as $str)
{
    $gl  = explode("\t", $str);
    $cid = $gl[5];

    // RUN QUERY HERE USING MySQLI, NOT MySQL
    $qry = "SELECT cid from customer where cid = " . $cid;
    
    $res = mysql_db_query ($qry);
    $nr  = mysql_num_rows($res);
    echo PHP_EOL . "cid = $cid, nr = $nr <br>";
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Richard KortsBusiness Owner / Chief DeveloperAuthor Commented:
Ray,

Thanks, I'll try that.

There cannot be any cid duplicates; that is a unique key on that table.
Active Protection takes the fight to cryptojacking

While there were several headline-grabbing ransomware attacks during in 2017, another big threat started appearing at the same time that didn’t get the same coverage – illicit cryptomining.

Ray PaseurCommented:
OK, let's take a step back... What is the expected output from this script, ie, what are you trying to do here?
Richard KortsBusiness Owner / Chief DeveloperAuthor Commented:
Ray,

It's kind of a long story.

In March, the table customer in the mysql database was created basically by uploading ALL the records in customer.txt; there are some axillary tables, one called comments. For reasons not relevant here, that is a separate table on the MySQL database.

Today the customer informed me of an anomaly. There was a record in the comments table for which there is no customer in the customer table.

So my first idea was just read through the .txt file & do a sql query on the customer table & echo the ones not there.

I guessed it would be a handful; I know there are only 2 between customers #'s 15000 and 17000.

FYI, the customer number start at 10000 and go to 23xxx.

Then I would deal with the handful somehow (tbd).
Julian HansenCommented:
Rather read the CSV into a separate table and then use SQL to do the check - much easier than in code.

You can load the file directly with PHPMyAdmin / Workbench / SQLYog / Heidi or whatever Query tool you are using.

Once imported it is a simple task of

select * from tblcustomer where custid not in (select custid from temp_customer)

Open in new window

hieloCommented:
>> Today the customer informed me of an anomaly. There was a record in the comments table for which there is no customer in the customer table.
That implies that these tables are related a common field.   Let's say you have

Customers
=========
cid -- this is the primary key

Comments
=========
id   -- this is the primary field
cid -- this is the "link" between these two tables.

You can try executing:
SELECT A.cid FROM Comments A WHERE NOT EXISTS( SELECT B.cid FROM Customers B WHERE B.cid=A.cid)

You should get the cid values in Comments that don't have a matching/related record in Customers.
Richard KortsBusiness Owner / Chief DeveloperAuthor Commented:
The problem was caused by GoDaddy server taking too long to execute; they (GoDaddy) seemed to have fixed the problem, my originally posted query works fine now.

I suspected this because of other issues going on with other parts of our system.

Points awarded for effort.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.