lib_my.php

1 개요[ | ]

lib_my.php

2 global[ | ]

$mysqli = new mysqli('localhost', '계정명', '패스워드', '디비명');
if($mysqli->connect_errno) die('Connect failed: '.$mysqli->connect_error);
if(!$mysqli->set_charset('utf8')) die('Error loading character set utf8: '.$mysqli->error);

function query_rows($qry) {
  global $mysqli;
  $result = $mysqli->query($qry);
  if($result === false) die("Query: [[ $qry ]] → Error: ".$mysqli->error);
  $rows= array();
  eval('while(@$row = $result->fetch_assoc()) array_push($rows, $row);');
  return $rows;
}

function query_row($qry) {
  $rows = query_rows($qry);
  if(count($rows)<1) return false;
  return $rows[0];
}

function query_values($qry) {
  global $mysqli;
  $result = $mysqli->query($qry);
  if($result === false) die("Query: [[ $qry ]] → Error: ".$mysqli->error);
  $arr = array();
  while($row = $result->fetch_row()) $arr[] = $row[0];
  return $arr;
}

function query_one($qry) {
  global $mysqli;
  $result = $mysqli->query($qry);
  if($result === false) die("Query: [[ $qry ]] → Error: ".$mysqli->error);
  $row = $result->fetch_row();
  if(count($row)<1) return false;
  return $row[0];
}

function query($qry) {
  global $mysqli;
  $result = $mysqli->query($qry);
  if($result === false) die("Query: [[ $qry ]] → Error: ".$mysqli->error);
  return $result ;
}

function my_escape($str) {
  global $mysqli;
  return $mysqli->real_escape_string($str);
}

function insert_id() {
  global $mysqli;
  return $mysqli->insert_id;
}

3 사용예시[ | ]

# SELECT
$rows = query_rows('SELECT * FROM table WHERE name=?', $name);
$row = query_row('SELECT * FROM table WHERE id=?', $id);
$name = query_one('SELECT name FROM table WHERE id=', $id);
$cnt = query_one('SELECT COUNT(*) FROM table');

# INSERT
query('INSERT INTO table1 (id, name) VALUES (?, ?)', $id, $name);

4 같이 보기[ | ]

5 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}