PHP mySQL 기초
mySQL에 접속하기
<?php
$link = mysqli_connect("localhost","username" ,"password","dbname");
?>
mySQL 쿼리문
insert
- true, false 리턴
$query = "INSERT INTO `users` (`email`, `password`) VALUES('Pin@cutie.com','1cutie00pie4')";
update
- true, false 리턴
$query = "UPDATE `users` SET email = 'Robstar@cutie.com' WHERE id=1 LIMIT 1"; mysqli_query($link,$query);
select
- 결과 object들 리턴
$query="SELECT * FROM users";
결과 출력하기
$query="SELECT * FROM users"; if($result = mysqli_query($link,$query)){ while($row=mysqli_fetch_array($result)){ print_r($row); } }
기타
$query="SELECT * FROM users" WHERE email LIKE 'Pin@cutie.com';
$query="SELECT * FROM users" WHERE email LIKE '%Pin@cutie.com';
$query="SELECT * FROM users" WHERE email LIKE '%P%';
$query="SELECT * FROM users" WHERE id >=2;
$query="SELECT * FROM users" WHERE id >=2 AND email LIKE '%g%';
$name = "Rob O'Grady";
$query="SELECT * FROM users" WHERE name = '".mysqli_real_escape_string($link,$name)."'";