1

I know you can´t close connections like in mysqli, but in my code I execute many queries. For some reason after to many queries the page will just keep loading.

So to fix this, I put my $conn variable on null and then make a new PDO connection. But I don´t think that´s the right way to do it.

So my question is: Is there a better way to reconnect or execute many PDO queries?

for example:

$stmt = $conn->prepare("select * from table");
$stmt->execute();
$result = $stmt->fetchAll();

So if I execute this like 10 times or something then it just stops working. Only way to get it to work is by making $conn null and then start a new connection.

Pradeep
  • 9,667
  • 13
  • 27
  • 34
Wouter den Ouden
  • 1,523
  • 2
  • 17
  • 44
  • No, closing and then re-opening the connection isn't any good at all. Can you show some code for this? Perhaps there's some optimization or other mistakes we can help you with. And what do you mean by *"closing connections like in mysqli"*? In PDO, you can set the statements and objects to null, is that what you're asking? – Qirel Mar 20 '17 at 15:17
  • Are you using transactions ? Are you using `prepare` begin executing your request ? what mysql show processlist bring to you during execution ? – Fky Mar 20 '17 at 15:19
  • I've added an example. But it happens for multiple queries, so its hard to put the exact code here. – Wouter den Ouden Mar 20 '17 at 15:22
  • so not good `$stmt = $conn->prepare("select * from table");` – Masivuye Cokile Mar 20 '17 at 15:23
  • it's just an example, what's wrong with it? – Wouter den Ouden Mar 20 '17 at 15:23
  • when u post example u not gonna get proper answer, better post some real data – Masivuye Cokile Mar 20 '17 at 15:24
  • You could possibly reduce the amount of queries with JOINs (just making a guess here, you haven't shown us any relevant DB info), but without seeing the actual code, we can only answer in a general way, and might miss important details you're not showing us ;-) – Qirel Mar 20 '17 at 15:26
  • @WouterdenOuden to answer ur comment see : http://stackoverflow.com/questions/3639861/why-is-select-considered-harmful – Masivuye Cokile Mar 20 '17 at 15:27

1 Answers1

0

There is nothing wrong with "too many queries" as long as they are sane and necessary for the page.
Your page keeps loading for some other reason, which is irrelevant to the number of queries. I'd suppose some network problem when loading some external resource.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345