<body style="background:darkolivegreen;">
<?php
$db_conn = new mysqli('localhost','root','pass_to_db','forumpasjainformatyki');
function delete_article($db_conn, $id_article) {
$del_record = $db_conn->query('delete from articles where id='.$id_article);
if(!$del_record) {
printf("Errorcode: %d\n", $db_conn->errno);
} else {
if($db_conn->affected_rows===0) {
echo 'Not have neither article in DB.';
} else {
echo 'Deleted row.';
}
}
}
delete_article($db_conn, 20);
function get_quantity_articles($db_conn, $id_user) {
$get_article = $db_conn->query('select distinct count(*) as art from articles where id_user=' . $id_user);
$all_articles = $db_conn->query('select distinct count(*) as art from articles');
if(!$get_article || !$all_articles) {
printf("Errorcode: %d\n", $db_conn->errno);
} else {
$temp1 = 'Quantity articles users about id=' . $id_user . ' is ';
$temp2 = $get_article->fetch_assoc()['art'] . '.';
$temp3 = "All articles is: ";
$temp4 = $all_articles->fetch_assoc()['art'] . '.';
return $temp1 . $temp2 . "\n" . $temp3 . $temp4;
}
}
echo '<br />';
echo get_quantity_articles($db_conn, 9);
$db_conn->close();
?>
Ogólnie obsługę błędów itd. na klasach i nie tylko mógłbyś sobie napisać sam. To zostawiam już Tobie (wydzielanie i fragmentowanie kawałków kodu do innej części odpowiadającej za logikę innej za widok, itp.).
Zamiast dwóch zmiennych z dwoma zapytaniami możesz napisać tylko jedną zmienną z jednym zapytaniem w ten sposób:
$get_quantity_articles = $db_conn->query('select (select count(distinct id) from articles) as q_all, (select count(distinct id) from articles where id_user=4) as q_single');
I później (zamiast $temp2 i $temp4):
$temp2 = $get_quantity_articles->fetch_assoc()['q_single'] . '.';
$temp4 = $get_quantity_articles->fetch_assoc()['q_all'] . '.';