1
Well, I know that each connection requires a bit of server memory (the exact amount I don’t know), and I know that several connections can weigh my site, thinking about it I would like to know if it has how to unify the code:
$proximo = $pdo->query("SELECT * FROM postagens WHERE ID > ".$pid." AND ID_user = ".$cid." ORDER BY ID DESC")->fetchAll();
if(!$proximo){
print_r($pdo->errorInfo());
}
foreach ($proximo as $next){};
I use this code in a photo gallery, where I capture the current ID and create a link that redirects to the next post. It would be simple if I repeated the code again and changed the sign ">" to "<" and consequently the name of the variables so I would build a link to the previous post. But this would require two simultaneous connections to the BD, and these two connections even if they are useful also become useless, since they only serve to create two simple links. Could I use one unique connection to create these two links? Or in this case the only way is to keep both connections?
Talking about this same code I would like to know how to fix another "little problem" I’m having with it, as I said, this code creates a link that advances in the ID of the posts, the "little problem" that arose (what was expected), is that when reaching the end of the ID that correspond to the Id_user, an error arose saying that the ID does not exist, and really does not exist. You could then do some command where once you reach the last ID corresponding to the Id_user the ID 'zere' and return to the first ID, thus creating a kind of loop where you can proceed continuously?
You will make two queries, but only one connection. $Pdo is the connection, you will use the same for the two 'query'. Another detail, if it is only for link, you do not need foreach, and can keep LIMIT1 of the other answer, not to make the BD send unnecessary data. If you are going to link to several next and previous at once, then the solution given in the other question could be adjusted to take everything in one query, and split the result in PHP, but for that, you would have to be sure how you want the result.
– Bacco
Suppose then that I create 4 codes similar to that, changing only the variables, but keeping the $Pdo, the server will interpret as only a single connection?
– ivan veloso
The other two were just for show, so I could have a better purpose
– ivan veloso
Well, I tried it for an example of how to do the "loop" in the answer, but it would be nice for you to give more details when asking, because if I just wonder what you want to do, I run the risk of saying some nonsense that does not suit your specific case :)
– Bacco
Oops, messing you up again and still working with the same code, I would like to know how I can 'improve' the code. As I told you this connection there aims to build the link of the next post and the previous post. Only that is the following, all posts are registered in the same table, and not always these posts have media (image or video), which causes the following result "Array ( [0] => 00000 [1] => [2] => )", it would be simple to end it, but I want these posts without the media not to be listed... (CONTINUE)
– ivan veloso
(Continue), I want $next to list only ID’s where the "image" or "video" field is not empty. Could you do that I’m asking you? If you prefer I open another question and put all the codes so you can understand better
– ivan veloso
I think it would be an exaggeration a new question, just add the details in the WHERE (... AND video = "" AND image = """... and so on. Now, if you need a new question, put the code up to date and as clean as possible on the new question, so as not to confuse the staff.
– Bacco