donglefu6195 2009-02-12 00:29
浏览 79
已采纳

如何在php中选择mysql数据库?

I have this code:

               if(!mysql_connect($host,$user,$passwd)){
                    die("Hoops, error! ".mysql_error());
                }

...no error from here.

                if(!mysql_select_db($db,$connect)){
                    $create_db = "CREATE DATABASE {$db}";
                    mysql_query($create_db,$connect);
                    mysql_query("USE DATABASE {$db}",$connect);
                }

..."no database selected" error from here. I would like to select database if it exists and if doesn't then create it and select it.

Why is my code not right?

Thank you in advance

  • 写回答

7条回答 默认 最新

  • dpg76975 2009-02-12 00:38
    关注

    Where are you saving the value returned by mysql_connect()? Don't see it here. I assume $host, $user, $password and $db are properly set ahead of time. But you're passing a param to mysql_select_db that may not be properly set.

    $connect = mysql_connect($host,$user,$passwd);
    if (!$connect) {
        die('Could not connect: ' . mysql_error());
    }
    if(!mysql_select_db($db,$connect)) ...
    

    Start by checking to see if you can select without the CREATE query first. Try a simple SELECT query to start. If you can connect, select the db, and execute a SELECT query, that's one step. Then try the CREATE query. If that doesn't work, it's almost certainly a matter of permissions.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀