di00di 发表于 2012-08-16 14:39

关于mysql数据库问题

代码如下
<?php
$con = mysql_connect("localhost","root","199324");
if($con)
{
        mysql_select_db("test",$con);
        $sql = "CREATE TABLEtemp
        (
                id int(5) not null auto_increment primary key,
                name char(10) not null default ' ',
                sex char(2) not null default '男',
               age int(3) not null default 0 ,
                work char(10) not null default ' ',
                city char(10) not null default ' ',
                other char(50) not null default ' '
        )";
        $do = mysql_query($sql,$con);
        if($do)
        {
                echo "成功在test数据库中创建表!";
        }
        elseecho "建表错误!";
}else "连接服务器出错!";
?>


<?php
$con = mysql_connect("localhost","root","199324");
mysql_select_db("test");
mysql_query("SET NAMES GB2312");
$sql = 'SELECT COUNT(*) FROM temp';
$result = mysql_query($sql);
$num = mysql_fetch_array($result);
echo "当前表中记录数为:".$num."<p>";
$insert = "INSERT INTO temp('id', 'name', 'sex', 'age', 'work', 'city', 'other') VALUES (1, 'di', 'man', 480, 'west', 'west', 'boss')";

$re = mysql_query($insert);
if($re)
{
        echo "成功插入数据<p>最后插入的类容为:<p>";
        $id = mysql_insert_id();
    $sql = "SELECT * FROM temp WHERE id = $id";
        $result = mysql_query($sql);
        $content = mysql_fetch_row($result);
        print_r($content);
}else
echo "插入失败";
?>


------------------------------------------------------------------------------------------------------------------------------------------------》》》》
第一个程序创建一个表
第二个程序试图在表中添加一个记录
可以成功创建表 但是记录总是添加不成功是什么原因呢?

hbeimf 发表于 2012-08-16 15:35

:em17::em17::em17::em17:

leonzhang2008 发表于 2012-08-16 20:53

查下数据库,表创建成功了吗?
页: [1]
查看完整版本: 关于mysql数据库问题