Home  >  Article  >  Backend Development  >  php建表不成功,但也没有报错,求解解决办法

php建表不成功,但也没有报错,求解解决办法

WBOY
WBOYOriginal
2016-06-13 13:36:13742browse

php建表不成功,但也没有报错,求解

PHP code




页面显示:“没有表”,但执行一次后应该创建表了,我再刷新页面还是显示“没有表”,请各位大虾们帮忙看下神马问题?

------解决方案--------------------
试试:
PHP code
        //连接数据
        $con=mysql_connect("localhost","root","welcome123") or die("无法连接数据库".mysql_error());
        //判断是否有my_project数据库
        $existDB=mysql_select_db("my_project",$con);
        //echo "没有my_project数据库";
        if(!$existDB)
        {
            //创建数据库
            $cdatabseSql="Create DATABASE my_project";
            mysql_query($cdatabseSql,$con);
            mysql_select_db("my_project",$con);  //选择数据库
        }
        else
        {
            echo "有数据库";
        }
        //判断是否有sendmail表
        $row=mysql_query("show tables");
        $database=array();
        $finddatabase="sendmail";
        while ($result=mysql_fetch_array($row,MYSQL_ASSOC))
        {
            $database[]=$result['Tables_in_test'];
        }
        unset($result,$row);
        
        if(!in_array($finddatabase,$database))
        {
            echo "没有表";    
            $ctableSql="Create TABLE sendmail (
                ID INT AUTO_INCREMENT PRIMARY KEY,
                tomail nvarchar(50),
                subject nvarchar(50),
                message text
            )";
            mysql_query($ctableSql,$con);
        }
        else
        {
            echo "有表";    
        }
        die();

------解决方案--------------------
大概这样试试,

PHP code

        if(!$existDB)
        {
            //创建数据库
            $cdatabseSql="Create DATABASE my_project";
            mysql_query($cdatabseSql,$con);
        
            mysql_select_db("my_project",$con);//加一句,选择库
        }
……
……
        //判断是否有sendmail表

        $row = mysql_list_tables("my_project");

        $database=array();
        $finddatabase="sendmail";
        while ($result=mysql_fetch_array($row))
        {
            $database[]=$result[0];
        }
        unset($result,$row); 
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn