mysql基本操作


Conn.php    //连接mysql数据库

<?php
$id=mysql_connect("127.0.0.1","root","") or die('连接失败:'.mysql_error());
if(mysql_select_db("test_moonlitshiny ",$id))
echo"";
else
echo('数据库连接失败:'.mysql_error());
mysql_query("set names utf8");
?>
Select.php    //搜索数据库

<?php
$id=mysql_connect("127.0.0.1","root","") or die('连接失败:'.mysql_error());
if(mysql_select_db("test_moonlitshiny ",$id))
echo"";
else
echo('数据库连接失败:'.mysql_error());
mysql_query("set names utf8");
$lineid=1;
$query=mysql_query("select * from test_moonlitshiny where ID='$lineid'");
if($query==true)
{
while($myrow=mysql_fetch_array($query))
{
$helloworld=$myrow[q1];
$hello=$myrow[q2];
Echo $helloworld;
Echo $hello;
}
}

insert.php    //插入数据

<?php
$id=mysql_connect("127.0.0.1","root","") or die('连接失败:'.mysql_error());
if(mysql_select_db("test_moonlitshiny ",$id))
echo"";
else
echo('数据库连接失败:'.mysql_error());
mysql_query("set names utf8");

$n1=”hello”;
$n2=”world”;
$n3=”helloworld”;
$query="insert into test_moonlitshiny(q1,q2,q3)values('$n1','$n2','$n3')";
$result=mysql_query($query);
if($result==true)
{
Echo “HELLO ~WORLD!”;
}
Else
{
Echo “oh~~thx~~~”;
}
?>

Update   //更新
<?php
$id=mysql_connect("127.0.0.1","root","") or die('连接失败:'.mysql_error());
if(mysql_select_db("test_moonlitshiny ",$id))
echo"";
else
echo('数据库连接失败:'.mysql_error());
mysql_query("set names utf8");
$id=1;
$n1=”hello”;
$n2=”world”;
$n3=”helloworld”;
$query=mysql_query("update test_moonlitshiny set q1='$n1',q2='$n2',q3='$n3'  where ID='$id'");
if($query==true)
{
Echo “HELLO ~WORLD!”;
}
Else
{
Echo “oh~~thx~~~”;
}
?>

Delete.php      //删除
<?php
$id=mysql_connect("127.0.0.1","root","") or die('连接失败:'.mysql_error());
if(mysql_select_db("test_moonlitshiny",$id))
echo"";
else
echo('数据库连接失败:'.mysql_error());
mysql_query("set names utf8");
$id=1;
$query=mysql_query(“delete from test_moonlitshiny where ID=’$id’”);
If($query==true)
{
Echo “删除成功”;
}
Else
{
Echo “删除失败”;
}

原文地址:https://www.cnblogs.com/love1226/p/4475142.html