PHP MySQL ¸í·É¾î ½ÇÇà ÇÔ¼ö mysql query |
|
|
![](icon/private_name/1.gif) |
13³â Àü |
[PHP MySQL ¸í·É¾î ½ÇÇà ÇÔ¼ö mysql_query]
php¿¡¼ µ¥ÀÌÅͺ£À̽º(MySQL)¿¡°Ô SQL ¸í·É¾î¸¦ ½ÇÇà½ÃÅ°±â À§Çؼ´Â mysql_query ÇÔ¼ö°¡ »ç¿ëµË´Ï´Ù.
sql ¸í·É¾î¶ó µÎ¸®¹¶½ÇÇÏ°Ô ¸»¾¸µå·È´Âµ¥, ÀÌ°ÍÀ» ±â´É¿¡ ÀÇÇؼ ¼¼ºÐÈ Çغ¸¸é ¾Æ·¡¿Í °°½À´Ï´Ù.
DDL(µ¥ÀÌÅÍÁ¤ÀǾî) - CREATE, ALTER, DROP
DML(µ¥ÀÌÅÍÁ¶ÀÛ¾î) - SELECT, INSERT, UPDATE, DELETE
DCL(µ¥ÀÌÅÍÁ¦¾î¾î) - GRANT, REVOKE, CASECADE, RESTRICT
mysql_query ÇÔ¼ö´Â SQL ¸í·É¾î¶ó¸é À§ÀÇ ¼¼°¡ÁöÁß ´ëºÎºÐ(Á¤È®È÷ ÀüºÎ Å×½ºÆ®ÇØ º¸Áö ¸øÇß½¿)À» ¸ÚÁö°Ô ½ÇÇà½Ãŵ´Ï´Ù. ¾Æ·¡ÀÇ ¿¹Á¦´Â mysql_query ÇÔ¼ö¸¦ ÀÌ¿ëÇؼ ¾î¿©»Û Å×À̺í Çϳª¸¦ ¸¸µé°í ÀÖ½À´Ï´Ù.
<?php
include("dbcon.php");
$sql = "CREATE TABLE test_member ( ";
$sql = $sql . " seq int not null auto_increment, ";
$sql = $sql . " memuid varchar(20) NOT NULL, ";
$sql = $sql . " name varchar(20) NOT NULL, ";
$sql = $sql . " addr varchar(255) NOT NULL, ";
$sql = $sql . " PRIMARY KEY(seq), ";
$sql = $sql . " UNIQUE memuid (memuid) );";
$result = mysql_query($sql) or die("SQL¹® Á¦´ë·Î ¾´°Å ¸Â¾Æ???");
if($result) echo "Å×À̺í Create ¼º°ø!!";
?>
´ÙÀ½Àº À§ÀÇ ¿¹Á¦¿¡¼ »ý¼ºµÈ test_member Å×ÀÌºí¿¡ INSERT ¹®À» ÀÌ¿ëÇؼ ÇϳªÀÇ ·¹Äڵ带 Ãß°¡ÇÏ´Â ¿¹Á¦ÀÔ´Ï´Ù.
<?php
include("dbcon.php");
$memuid = "webmadang.net";
$name = "À¥¸¶´ç³Ý";
$addr = "¾î¼½Ã ¾î¼±¸ ¾î¼µ¿ 111-11";
$sql = "INSERT INTO test_member SET ";
$sql = $sql . " memuid ='$memuid', ";
$sql = $sql . " name ='$name', ";
$sql = $sql . " addr ='$addr' ";
$result = mysql_query($sql) or die("¶Ç Ʋ¸®¸é Á״´Ù!!!");
$rows = mysql_affected_rows();
if($result) echo "INSERT·Î ÀÔ·ÂµÈ ·¹ÄÚµåÀÇ °¹¼ö´Â:$rows";
?>
MySQL¿¡°Ô Àü´ÞµÈ SQL ¸í·É¾î°¡ INSERT, UPDATE, DELETE, DROP µîÀ̶ó¸é mysql_query ÇÔ¼öÀÇ ½ÇÇà°á°ú°¡ ÂüÀ϶§ TRUE¸¦ ¹ÝȯÇÏ°Ô µÇ¸ç mysql_affected_rows ÇÔ¼ö¸¦ ÀÌ¿ëÇؼ º¯°æµÈ ·¹ÄÚµåÀÇ °¹¼ö¸¦ ¾Ë¼ö ÀÖ½À´Ï´Ù. À§ÀÇ ¿¹Á¦°¡ ¼º°øÇÏ°Ô µÇ¸é "INSERT·Î ÀÔ·ÂµÈ ·¹ÄÚµåÀÇ °¹¼ö´Â:1" À̶ó´Â ¹®ÀÚ¿ÀÌ Ãâ·ÂµË´Ï´Ù.
¾Æ·¡ ¿¹Á¦´Â SELECT ¹®À» ÀÌ¿ëÇؼ À§ÀÇ ¿¹Á¦¿¡¼ ÀÔ·ÂµÈ ·¹Äڵ带 ȸ鿡 Ãâ·ÂÇÏ°Ô µË´Ï´Ù.
<?php
include("dbcon.php");
$sql = "SELECT memuid, name, addr FROM test_member ";
$result = mysql_query($sql) or die("Á¦´ë·Î Á» ½áºÁ!!!");
while($row = mysql_fetch_row($result))
echo"'$row[0]' '$row[1]' '$row[2]'<br>\n";
?>
select ¹®ÀÇ ½ÇÇà°á°ú·Î ¹ÝȯµÈ ·¹ÄÚµå¼Â(data set)À» °¡Á®¿À±â À§Çؼ´Â mysql_fetch ½Ã¸®Áî·Î ½ÃÀ۵Ǵ ÇÔ¼öµéÁß Çϳª¸¦ »ç¿ëÇØ¾ß ÇÕ´Ï´Ù. À§¿¡ »ç¿ëµÈ mysql_fetch_row ÇÔ¼ö´Â ·¹ÄÚµå¼ÂÀ» ¹è¿·Î ¹ÝȯÇϴµ¥ °³º° Ä÷³ÀÇ °ªÀ» °¡Á®¿À±â À§Çؼ´Â À§¿Í °°ÀÌ Ä÷³ÀÇ ¼ø¼¸¦ ³ªÅ¸³»´Â ¹øÈ£¸¦ »ç¿ëÇØ¾ß ÇÕ´Ï´Ù.
echo"'$row[0]' '$row[1]' '$row[2]'<br>\n";
¹ÝȯµÈ ·¹ÄÚµå¼ÂÀ» Ä÷³¸íÀ» ÀÌ¿ëÇؼ ÂüÁ¶Çϱâ À§Çؼ´Â mysql_fetch_array ÇÔ¼ö¸¦ »ç¿ëÇÕ´Ï´Ù.
¾Æ·¡ ¿¹Á¦´Â À§ÀÇ ¿¹Á¦¿¡¼ »ç¿ëµÈ mysql_fetch_row ÇÔ¼ö¸¦ mysql_fetch_array ÇÔ¼ö·Î ¹Ù²ã »ç¿ëÇß½À´Ï´Ù.
<?php
include("dbcon.php");
$sql = "SELECT memuid, name, addr FROM test_member ";
$result = mysql_query($sql) or die("Á¦´ë·Î Á» ½áºÁ!!!");
while($row = mysql_fetch_array($result)){
echo"'$row[memuid]' '$row[name]' '$row[addr]'<br>\n";
echo"'$row[0]' '$row[1]' '$row[2]'<br>\n";
}
?>
À§ÀÇ ¿¹Á¦¿¡¼ º¸À̵íÀÌ mysql_fetch_array ÇÔ¼ö´Â Ä÷³ÀÇ °ªÀ» °¡Á®¿Ã¶§ Ä÷³¸íÀ̵ç Ä÷³ÀÇ ¹øÈ£µç »ó°ü¾ø½À´Ï´Ù.
¸¶Áö¸·À¸·Î MySQL Å×ÀÌºí¿¡¼ ·¹ÄÚµå¼ÂÀÌ ¾Æ´Ñ ´Ü ÇÑ°³ÀÇ µ¥ÀÌÅ͸¸ °¡Á®¿Ã¼ö ÀÖ´Â mysql_result ÇÔ¼ö°¡ ÀÖ½À´Ï´Ù.
¾Æ·¡´Â mysql_result ÇÔ¼öÀÇ »ç¿ë¿¹Á¦ÀÔ´Ï´Ù.
<?php
include("dbcon.php");
$sql = "SELECT memuid, name, addr FROM test_member ";
$result = mysql_query($sql) or die("Á¦´ë·Î Á» ½áºÁ!!!");
echo mysql_result($result,0,0)."<br>\n";
echo mysql_result($result,0,1)."<br>\n";;
echo mysql_result($result,0,2)."<br>\n";;
?>
mysql_result ÇÔ¼ö´Â mysql_fetch_row ÇÔ¼ö³ª mysql_fetch_array ÇÔ¼öº¸´Ù µÎ°³ÀÇ ÀÎÀÚ¸¦ ´õ ÃëÇϴµ¥ ù¹ø° ÀÎÀÚ´Â ·¹ÄÚµå(Çà)¹øÈ£ÀÌ°í, µÎ¹ø° ÀÎÀÚ´Â Ä÷³ÀÇ ¹øÈ£ÀÔ´Ï´Ù. ƯÀÌÇÑ Á¡Àº ·¹ÄÚµå ¹øÈ£³ª Ä÷³ÀÇ ¹øÈ£°¡ 1ºÎÅÍ ½ÃÀÛÇÏ´Â°Ô ¾Æ´Ï°í 0 ºÎÅÍ ½ÃÀÛÇÑ´Ù´Â °ÍÀÔ´Ï´Ù.
mysql_result($result,0,0) Àº ù¹ø° ·¹ÄÚµå, ù¹ø° Ä÷³À» ÀǹÌÇÕ´Ï´Ù.
mysql_result($result,0,1) Àº ù¹ø° ·¹ÄÚµå, µÎ¹ø° Ä÷³À» ÀǹÌÇÕ´Ï´Ù.
mysql_result($result,0,2) Àº ù¹ø° ·¹ÄÚµå, ¼¼¹ø° Ä÷³À» ÀǹÌÇÕ´Ï´Ù.
mysql_result($result,1,1) Àº µÎ¹ø° ·¹ÄÚµå, µÎ¹ø° Ä÷³À» ÀǹÌÇÕ´Ï´Ù.
mysql_result($result,1,2) Àº µÎ¹ø° ·¹ÄÚµå, ¼¼¹ø° Ä÷³À» ÀǹÌÇÕ´Ï´Ù.
mysql_result($result,1,3) Àº µÎ¹ø° ·¹ÄÚµå, ³×¹ø° Ä÷³À» ÀǹÌÇÕ´Ï´Ù.
php4 ÀÌÀü¹öÀü¿¡¼´Â mysql_fetch_row ÇÔ¼ö°¡, mysql_fetch_array ÇÔ¼öº¸´Ù ¼Óµµ¸é¿¡¼ ºü¸¥°ÍÀ¸·Î ¼Ò°³µÇ¾úÀ¸³ª, ÇöÀç´Â ¼ÓµµÂ÷ÀÌ°¡ °ÅÀÇ ¾ø´Ù°í ÇÕ´Ï´Ù.
À§ÀÇ include("dbcon.php") ´Â MySQLÀÇ Á¢¼Ó ¸í·É¾î°¡ »ðÀԵǾî ÀÖ´Â dbcon.php ÆÄÀÏÀ» Æ÷ÇÔÇ϶ó´Â
¶æÀÔ´Ï´Ù. dbcon.php ÆÄÀÏÀÇ ³»¿ëÀº ´ÙÀ½°ú °°À» °ÍÀÔ´Ï´Ù.
<?php
$hostname = "localhost"; // È£½ºÆ®¸¦ ½Äº°ÇÒ ¼ö Àִ ȣ½ºÆ®¸íÀ̳ª ip
$user = "myuser"; // MySQL¿¡¼ »ç¿ëÇÒ µ¥ÀÌÅͺ£À̽º »ç¿ëÀÚ¸í
$password = "********"; // MySQL¿¡¼ »ç¿ëÇÒ µ¥ÀÌÅͺ£À̽º¿¡ Á¢¼ÓÇϱ⠺ñ¹Ð¹øÈ£
$database = "testdb"; // MySQL¿¡¼ »ç¿ëÇÒ µ¥ÀÌŸº£À̽º¸í
$connect = mysql_connect ($hostname, $user, $password) or die ("µ¥ÀÌÅͺ£À̽º Á¢¼Ó½ÇÆÐ");;
mysql_select_db ($database, $connect);
?>
|
|
̵̧ : 276 |
̵̧
¸ñ·Ï
|
|