在开始任何对SQL的学习之前,先让我安装一个MySqL吧。
…………这个网站不错…………
OK,到此为止,安装好了MySql。但是一个表之类的都还没有,先学学MySql的命令吧。
use;
select;
insert;
delete;
update;
drop;
好了,会用这几个命令了,就达到了最低要求了,接下来,试着在网页中使用mySql。
好,那就先链接到数据库:
<div id="main" style="font-size: 30px; color: #7a0d0b">
<?php
$con=mysql_connect("localhost");
echo $con;
?>
</div>
Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'ODBC'@'localhost' (using password: NO)
看样子是验证失败。
想想看,在配置sql时,设置过一个用户,大概用户名是“root”吧:
$con=mysql_connect("localhost","root","catlause");
Resource id #2
删掉
echo $con;
不报错。
那么,数据库是连上了的:
$con=mysql_connect("localhost","root","catlause");
if($con)
{
echo "Connected successfully.";
}
Connected successfully.
果然。
<?php
$con=mysql_connect("localhost","root","catlause");
$db_selected=mysql_select_db("test",$con);
if($db_selected)
{
echo "Connected successfully.";
}
?>
Connected successfully.
<?php
$con=mysql_connect("localhost","root","catlause");
$db_selected=mysql_select_db("test",$con);
$sql="select * from town;";
$result=mysql_query($sql,$con);
echo $result;
?>
Resource id #3
<?php
$con=mysql_connect("localhost","root","catlause");
$db_selected=mysql_select_db("test",$con);
$sql="select * from town;";
$result=mysql_query($sql,$con);
$row=mysql_fetch_array($result);
echo $row["population"];
?>
257
好了。