0

Всем привет крайне слабо разбираюсь в связке PHP mySQL есть код который выводит все записи из базы:

<?

if ($page_num == "" && $news == "") {
    $news = mysql_query("SELECT `article_uin`,`article_title`,`article_text`,DATE_FORMAT(article_date,'%d.%m.%Y') AS article_date FROM `articles` WHERE `status`<> 2 AND `status`<> 0 ORDER BY `articles`.`article_date` DESC ") or die(mysql_error());
    
        while ($news_fetch = mysql_fetch_array($news)) {
            $date = $news_fetch["article_date"];
            $today = date('Y-m-d', time());
            $get_date = explode(".", $date);
            $new_date_format = $get_date[1]."/".$get_date[0]."/".$get_date[2];
            $new_date = new DateTime($new_date_format); // format: MM/DD/YYYY
            $date_conv = $new_date->format('U')." ";
        
            if (date('Y-m-d', $date_conv) <= $today) {
                
                $uin = $news_fetch["article_uin"];
                $picture = mysql_query("SELECT * FROM `pictures` WHERE `article_uin` = \"$uin\" ") or die(mysql_error());
                $picture_fetch = mysql_fetch_array($picture);
                print $picture_fetch["picture_file_small"];
                print $news_fetch["article_date"]);
                print $news_fetch["article_title"]);
                print $news_fetch["article_text"]);
                
            }
        }
?>

Как в нем добавить условие и запрос SQL что бы вывести конкретную запись по uin которой приходит по адресу на манер domain.ru/blog/123 (где 123 - это uin или по простому ID записи в базе)? Основные данные которые нужно получить в виде переменных это "article_title" "article_text" "article_date" "picture_file_small"

Не судите строго не силен в PHP/mySQL все методы гуглинга не помогли

1
  • очевидно же в SELECT добавить WHERE условие. 22 мая 2021 в 7:42

1 ответ 1

0

После чтения манов по php, mySQL и такой-то матери сделал вывод по условию которое прилетает в адресной строке добавив к условию if альтернативу else

<?

if ($news == "" ) {
    mysql_query('set names cp1251');
    $today = date('Y-m-d', time());
    $news = mysql_query("SELECT `article_uin`,`article_title`,`article_text`,DATE_FORMAT(article_date,'%d.%m.%Y') AS article_date FROM `articles` WHERE `status`<> 2 AND `status`<> 0 ORDER BY `articles`.`article_date` DESC ") or die(mysql_error());
    while ($news_fetch = mysql_fetch_array($news)) {
        $date = $news_fetch["article_date"];
        $today = date('Y-m-d', time());
        $get_date = explode(".", $date);
        $new_date_format = $get_date[1]."/".$get_date[0]."/".$get_date[2];
        $new_date = new DateTime($new_date_format); // format: MM/DD/YYYY
        $date_conv = $new_date->format('U')." ";

        if (date('Y-m-d', $date_conv) <= $today) {

            $uin = $news_fetch["article_uin"];
            $picture = mysql_query("SELECT * FROM `pictures` WHERE `article_uin` = \"$uin\" ") or die(mysql_error());
            $picture_fetch = mysql_fetch_array($picture);
            $pic = $picture_fetch["picture_file_small"];
            print $pic;
            print $news_fetch["article_date"];
            print $news_fetch["article_title"];
            print $news_fetch["article_text"]
            print $news_fetch["article_uin"];
            }
        }
    } 
    // добавляем к условию если альтернативу else когда в переменной $news есть какое-то значение;
    else {
        mysql_query('set names cp1251');
        $today = date('Y-m-d', time());
        // формируем запрос уже по ID новости в базе
        $query = mysql_query("SELECT `article_uin`,`article_title`,`article_text`,DATE_FORMAT(article_date,'%d.%m.%Y') AS article_date FROM `articles` WHERE `article_uin` = \"$news\" ") or die(mysql_error());
        // не стал удалять цикл а просто скопировл вывод из базы, в полученном масиве всеравно одна строка а не несеолько как в предыдущем выводе (старался не сильно трогать код)
        while ($news_fetch = mysql_fetch_array($query)) {
            $date = $news_fetch["article_date"];
            $today = date('Y-m-d', time());
            $get_date = explode(".", $date);
            $new_date_format = $get_date[1]."/".$get_date[0]."/".$get_date[2];
            $new_date = new DateTime($new_date_format); // format: MM/DD/YYYY
            $date_conv = $new_date->format('U')." ";
            if (date('Y-m-d', $date_conv) <= $today) {
                $uin = $news_fetch["article_uin"];
                $picture = mysql_query("SELECT * FROM `pictures` WHERE `article_uin` = \"$uin\" ") or die(mysql_error());
                $picture_fetch = mysql_fetch_array($picture);
                $pic = $picture_fetch["picture_file_small"];
                print $news_fetch["article_title"];
                print $news_fetch["article_date"];
                print $pic; 
                print $news_fetch["article_text"];
                }
            }
        }
?>

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.