I can not understand the use of cycles. All show examples with numbers, this is understandable, even there were no problems, but with information how? Here, for example , as I understand it, the condition is if(!$query){ , etc. else{ then if again, I can not understand the sequence or how to explain it correctly

 <?php function GetDataArt($art_id) { $sel = "SELECT `title`,`page_title`,`meta_d`,`meta_kw`,`content` FROM `articles` WHERE `id` = '$art_id' LIMIT 1"; $query = mysql_query($sel); if (!$query) { echo ('Не удалось взять данные из БД!'); } else { if (mysql_num_rows($query) > 0) { $res = mysql_fetch_array($query); $title = $res['title']; $page_title = $res['page_title']; $meta_kw = $res['meta_kw']; $meta_d = $res['meta_d']; $content = $res['content']; } else { $title = 'К сожалению, такая страница отсутствует на данном сайте!'; $page_title = 'К сожалению, такая страница отсутствует на данном сайте!'; $meta_kw = $meta_d = $content = ''; } $data_arr = array( $title, $page_title, $meta_kw, $meta_d, $content ); return $data_arr; } } ?> 
  • one
    Use the code design, it's hard to read what you wrote. - Barton
  • @Ruta Check the question. - Nicolas Chabanovsky
  • 3
    And what have the cycles? - ReinRaus

2 answers 2

 function GetDataArt($art_id){ $sel = "SELECT `title`,`page_title`,`meta_d`,`meta_kw`,`content` FROM `articles` WHERE `id` = '$art_id' LIMIT 1"; $query = mysql_query($sel); if(!$query){ // если запрос был НЕ удачен то ошибку выводим echo('Не удалось взять данные из БД!'); } else{ // запрос выполнился удачно, что-то делаем // добавляем еще одно условие на проверку того что мы вообще выдернули что-то из БД или нет if(mysql_num_rows($query)>0){ // если кол-во выдернутых строк по запросу больше 0 т.е. там есть хоть одна строка то что-то чему-то присваиваем. $res = mysql_fetch_array($query); $title = $res['title']; $page_title = $res['page_title']; $meta_kw = $res['meta_kw']; $meta_d = $res['meta_d']; $content = $res['content']; } else{ // если нет ни одной строки выдернутой из БД то выводим сообщение с ошибкой $title = 'К сожалению, такая страница отсутствует на данном сайте!'; $page_title = 'К сожалению, такая страница отсутствует на данном сайте!'; $meta_kw = $meta_d = $content = ''; } $data_arr = array($title, $page_title, $meta_kw, $meta_d, $content); return $data_arr; } } ?> 

More questions :)

ps and cycles here and, here function and work with a DB.

pps

I would rewrite my name a little

 function GetDataArt($art_id){ $data_arr=0; $sel = "SELECT `title`,`page_title`,`meta_d`,`meta_kw`,`content` FROM `articles` WHERE `id` = '$art_id' LIMIT 1"; $query = mysql_query($sel); if(!$query){ // если запрос был НЕ удачен то ошибку выводим echo('Не удалось взять данные из БД!'); return $data_arr; // вернуть то что-то по любому нужно, результат работы функции потом проверить на "вшивость" } 

those. the function worked, but in the first case, if the request is not fulfilled, it will not return anything, therefore, where it is called, subsequent errors are possible, and the result of what it returned I would check it anyway.

    Translate the word if - as “if”, and the word else as “otherwise” and read the code, then everything will become clear. Probably, you mean not sequence, but most likely - nesting.

    PS If I'm right, then all the superbrains that gave the answer earlier, too literally understood the question. :) So they are super. :)

    • So they brought the correct code, no? Just corrected that there are no cycles. - Oleg Arkhipov
    • @Construct, I just don’t see a question about a specific code. In my opinion, the question about the basics. Philosophical, as it were. :) - Angelina_Jo