๐Ÿ’ซ ๋ธ”๋กœ๊ทธ ์นดํ…Œ๊ณ ๋ฆฌ๋ณ„ ๊ธ€ ๊ฐœ์ˆ˜ ์ถ”๊ฐ€ํ•˜๊ธฐ

DB์— ์ด๋ฏธ ์นดํ…Œ๊ณ ๋ฆฌ๋ณ„ count๋ฅผ ๊ณ„์‚ฐํ•œ ์ปฌ๋Ÿผ์ด ์žˆ์–ด์„œ PHP ํŒŒ์ผ์„ ์ƒˆ๋กœ ์ƒ์„ฑํ•ด์„œ Json์œผ๋กœ ๋ฐ˜ํ™˜ ํ›„, ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ด์šฉํ•ด์„œ ์นดํ…Œ๊ณ ๋ฆฌ ์˜†์— ์ˆซ์ž๋ฅผ ์ ์–ด์ค„ ์˜ˆ์ •์ด๋‹ค !
ย 

๐Ÿ’ซ ์ฝ”๋“œ

<?php
host = "";    // MySQL ํ˜ธ์ŠคํŠธ ์ด๋ฆ„user = "";     // MySQL ๊ณ„์ • ์•„์ด๋””
pw = "";       // MySQL ๊ณ„์ • ๋น„๋ฐ€๋ฒˆํ˜ธdbName = ""; // MySQL ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ด๋ฆ„

// MySQL ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๊ฒฐ
db = new mysqli(host, user,pw, dbName);db->set_charset("utf8");

function mq(cat) //cat: ์นดํ…Œ๊ณ ๋ฆฌ ์•„์ด๋””
{
    global db;cat = (int)cat; //cat์€ ์ˆซ์ž๋งŒ ๋ฐ›์„๊ฑฐ๋‹ˆ๊นŒ int๋กœ ํƒ€์ž…์บ์ŠคํŒ…
    query = "SELECT count FROM wp_term_taxonomy WHERE term_taxonomy_id={cat}";
    result =db->query(query); //์ฟผ๋ฆฌ ์‹คํ–‰count = result->fetch_array()['count']; //๋ณ€์ˆ˜์— ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ ์ €์žฅ
    returncount; // ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ ๋ฐ˜ํ™˜
}

//์ฟผ๋ฆฌ ๊ฒฐ๊ณผ ๋ณ€์ˆ˜์— ์ €์žฅ
blog_count = mq(1);php_count = mq(3);
database_count = mq(4);c_count = mq(5);
sql_count = mq(9);javascript_count = mq(30);
web_count = mq(33);results = array(
    'Blog' => blog_count,
    'PHP' =>php_count,
    'Database' => database_count,
    'C#' =>c_count,
    'SQL' => sql_count,
    'JAVASCRIPT' =>javascript_count,
    'WEB' => web_count
);
//json ์ถœ๋ ฅ
echo json_encode(results);
?>

ย 

๐Ÿ’ซ ์ฝ”๋“œ์—์„œ ์ดํ•ด๊ฐ€ ์•ˆ๊ฐ”๋˜ ๋ถ€๋ถ„

$count = $result->fetch_array()['count'];
  • $result: ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ์ €์žฅํ•˜๋Š” ๊ฐ์ฒด
  • fetch_array(): ๊ฒฐ๊ณผ ์ง‘ํ•ฉ์—์„œ ํ•œ ํ–‰(row)์„ ์—ฐ๊ด€ ๋ฐฐ์—ด(associative array) ๋˜๋Š” ์ˆซ์ž ์ธ๋ฑ์Šค ๋ฐฐ์—ด๋กœ ๊ฐ€์ ธ์˜ค๋Š” ๋ฉ”์„œ๋“œ
  • ['count']: ๊ฐ€์ ธ์˜จ ๋ฐฐ์—ด์—์„œ count๋ผ๋Š” ํ‚ค์˜ ๊ฐ’์„ ์ถ”์ถœ
  • ์ตœ์ข…์ ์œผ๋กœ $count ๋ณ€์ˆ˜์—๋Š” ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ์—์„œ ๋ฐ˜ํ™˜๋œ count ๊ฐ’์ด ์ €์žฅ
    ย 

๐Ÿ’ซ fetch_array()

fetch_array() ๋ฉ”์„œ๋“œ๋Š” MySQLi ํ™•์žฅ์—์„œ ์ œ๊ณต, ๊ฒฐ๊ณผ ์ง‘ํ•ฉ์—์„œ ํ•œ ํ–‰์„ ๋ฐฐ์—ด ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜

$row = $result->fetch_array();
๋ฆฌํ„ด ๊ฐ’
  • ์„ฑ๊ณต ์‹œ: ํ˜„์žฌ ํ–‰ ๋ฐ์ดํ„ฐ๋ฅผ ํฌํ•จํ•˜๋Š” ๋ฐฐ์—ด(์—ฐ๊ด€ ๋ฐฐ์—ด ๋ฐ ์ˆซ์ž ๋ฐฐ์—ด์„ ํฌํ•จ ๊ฐ€๋Šฅ)
  • ์‹คํŒจ ์‹œ: null ๋˜๋Š” false ๋ฐ˜ํ™˜ (๋” ์ด์ƒ ๊ฐ€์ ธ์˜ฌ ํ–‰์ด ์—†์„ ๊ฒฝ์šฐ)
์ฃผ์š” ํŠน์ง•
  • ๋ฐฐ์—ด ํ˜•ํƒœ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋ฏ€๋กœ ์—ฐ๊ด€ ํ‚ค($row['column_name'])๋‚˜ ์ˆซ์ž ์ธ๋ฑ์Šค($row[0])๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Œ.
  • ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ํ”Œ๋ž˜๊ทธ ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ ์›ํ•˜๋Š” ํ˜•์‹์˜ ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ์Œ.

๐Ÿ’ซ fetch_array()์˜ ์˜ต์…˜(๋งค๊ฐœ๋ณ€์ˆ˜)

fetch_array() ๋ฉ”์„œ๋“œ๋Š” ํ•˜๋‚˜์˜ ์„ ํƒ์  ์ธ์ˆ˜๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด ์ธ์ˆ˜๋ฅผ ํ†ตํ•ด ๋ฐ˜ํ™˜๋˜๋Š” ๋ฐฐ์—ด ์œ ํ˜•์ง€์ • ๊ฐ€๋Šฅ.

  1. MYSQLI_ASSOC โ€“ ์—ฐ๊ด€ ๋ฐฐ์—ด(์ปฌ๋Ÿผ๋ช…์„ ํ‚ค๋กœ ์‚ฌ์šฉ)
$row = $result->fetch_array(MYSQLI_ASSOC);
echo $row['name'];  // ์—ฐ๊ด€ ํ‚ค๋กœ ์ ‘๊ทผ
  1. MYSQLI_NUM โ€“ ์ˆซ์ž ์ธ๋ฑ์Šค ๋ฐฐ์—ด(0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” ์ธ๋ฑ์Šค ์‚ฌ์šฉ)
$row = $result->fetch_array(MYSQLI_NUM);
echo $row[0];  // ์ˆซ์ž ์ธ๋ฑ์Šค๋กœ ์ ‘๊ทผ
  1. MYSQLI_BOTH (๊ธฐ๋ณธ๊ฐ’) โ€“ ์—ฐ๊ด€ ๋ฐ ์ˆซ์ž ๋ฐฐ์—ด ๋ชจ๋‘ ๋ฐ˜ํ™˜
$row = $result->fetch_array(MYSQLI_BOTH);
echo $row['name'];  // ์—ฐ๊ด€ ํ‚ค๋กœ ์ ‘๊ทผ
echo $row[0];       // ์ˆซ์ž ์ธ๋ฑ์Šค๋กœ ์ ‘๊ทผ

๐Ÿ’ซ fetch_array()์™€ ๋‹ค๋ฅธ ๋ฉ”์„œ๋“œ ๋น„๊ต

๋ฉ”์„œ๋“œ ์„ค๋ช… ๋ฐ˜ํ™˜ ํ˜•์‹
fetch_array() ์—ฐ๊ด€ ๋ฐ ์ˆซ์ž ๋ฐฐ์—ด ๋ฐ˜ํ™˜ ๊ฐ€๋Šฅ ๋ฐฐ์—ด
fetch_assoc() ์—ฐ๊ด€ ๋ฐฐ์—ด๋งŒ ๋ฐ˜ํ™˜(์ปฌ๋Ÿผ๋ช…์„ ํ‚ค๋กœ ์‚ฌ์šฉ) ์—ฐ๊ด€ ๋ฐฐ์—ด(Associative Array)
fetch_row() ์ˆซ์ž ์ธ๋ฑ์Šค ๋ฐฐ์—ด๋งŒ ๋ฐ˜ํ™˜ ์ˆซ์ž ๋ฐฐ์—ด(Numeric Array)
fetch_object() ๊ฐ์ฒด ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜(์†์„ฑ์œผ๋กœ ์ ‘๊ทผ ๊ฐ€๋Šฅ) ๊ฐ์ฒด(Object)

By Dozzing

๋‹ต๊ธ€ ๋‚จ๊ธฐ๊ธฐ

์ด๋ฉ”์ผ ์ฃผ์†Œ๋Š” ๊ณต๊ฐœ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํ•„์ˆ˜ ํ•„๋“œ๋Š” *๋กœ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค