So I cant find answer to very simple situation… I dont understand why it happen
Thats code
Line 1: $ca=$db->query('SELECT * FROM
clients'); Line 2: while ($c=$ca->fetch_assoc()) Line 3: { Line 4: $db->query('ALTER TABLE
client_’.$c[‘id’].’_dataADD COLUMN
updated datetime DEFAULT NULL'); Line 5: }
Its simplified code because I use try-catch mechanism to handle error number but basically you can see idea.
So in this case – some clients’ table already have column “updated” and some doesnt.
So when one of tables has already this column I receive exception (and its normal) and I handle it by just ignoring this error. BUT when I ignore this error in line 4 – after exception happen – when cycle tries to do next $c=$ca->fetch_assoc – script crashes with same exception
I receive something like that
Fatal error: Uncaught mysqli_sql_exception: Duplicate column ..... Stack trace: #0 /home/api/www/test.php(2): mysqli_result->fetch_assoc() #1 {main} thrown in /home/api/www/test.php on line 2
I dont understand why it happen and which is right method to handle it? This exception doesnt have nothing with $c=$ca->fetch_assoc() code. Its related to query inside loop and I handle this exception but looks like it somehow is stored in mysqli error stack and next fetch_assoc see it and script crashes.
What must I do? I dont understand….
I really can prevent using fetch_assoc in cycle – its clear – but I dont want because there are many cases when I have such constructions and I dont want to use big arrays to store all the results in them.