doujiao1814 2014-06-11 05:12
浏览 27
已采纳

我怎样才能使这个PHP代码工作? [关闭]

Warning: mysql_select_db() expects parameter 1 to be string, resource given in C:\xampp\htdocs\my_php_files\php4.php on line 25

Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\xampp\htdocs\my_php_files\php4.php on line 26 Could not create table:

The following code is to create a simple database and a table. I'm not able to execute this properly .I've searched many sites to fix this,nothing worked. I'm assuming there are quite a few mistakes .

 <?php
   //initialization
   $dbhost = 'localhost';
   $dbuser = 'root';
   $dbpass = '**********';
   $dbname = 'data_base';
   //check connection
   $conn = mysql_connect($dbhost, $dbuser, $dbpass,$dbname);

   if(! $conn )
      {
       die('Could not connect: '. mysql_error()); 
      }
   echo 'Connected successfully';

    //create table
    $sql = 'CREATE TABLE employee( '.
       'emp_id INT NOT NULL AUTO_INCREMENT, '.
       'emp_name VARCHAR(20) NOT NULL, '.
       'emp_address  VARCHAR(20) NOT NULL, '.
       'emp_salary   INT NOT NULL, '.
       'join_date    timestamp(14) NOT NULL, '.
       'primary key ( emp_id ))';

    mysql_select_db($conn,$sql);
    $retval = mysqli_query("SELECT * FROM employee");

       if(! $retval )
           {
            die('Could not create table: ' . mysql_error());
           }
    echo "Table  employee created successfully
";
    mysql_close($conn);
?>

I'm getting errors in the line 25 and line 26 of the program.

  • 写回答

2条回答 默认 最新

  • douyin6188 2014-06-11 05:15
    关注

    It looks like you're using the Database select command with incorrect information, this commands allows you to choose the database:

    mysql_select_db('my_database',$conn); // or the name of the DB you want to use
    

    Also you are mixing mysqli and mysql, you'll need to fix that.

    $retval = mysql_query("SELECT * FROM employee");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度