duanlei2150 2015-03-27 13:37
浏览 56
已采纳

如何连接到我在HostGator中创建的数据库?

Edit: I have googled and searched, read what I could find of their documentation. Even chatted with them. The chatter could help me. This is why I reach to you.

I am a complete beginner and I am having some troubles getting started with databases on hostgator. I guess my question also is valid using other hosts.

I created a db through the cpanel in hostgator and added a user to it.

I copied this script into a test.php in my /public_html/ folder and ran it on my site.

In the script I used the name, user and password from the database and user I previously created in cpanel. This database I can see using phpMyAdmin.

<?php
  try
  {
    //open the database
    $db = new PDO('sqlite:localhost;dbname=user_db', 'user_username', 'password');

    //create the database
    $db->exec("CREATE TABLE Dogs (Id INTEGER PRIMARY KEY, Breed TEXT, Name TEXT, Age INTEGER)");    

    //insert some data...
    $db->exec("INSERT INTO Dogs (Breed, Name, Age) VALUES ('Labrador', 'Tank', 2);".
               "INSERT INTO Dogs (Breed, Name, Age) VALUES ('Husky', 'Glacier', 7); " .
               "INSERT INTO Dogs (Breed, Name, Age) VALUES ('Golden-Doodle', 'Ellie', 4);");

    //now output the data to a simple html table...
    print "<table border=1>";
    print "<tr><td>Id</td><td>Breed</td><td>Name</td><td>Age</td></tr>";
    $result = $db->query('SELECT * FROM Dogs');
    foreach($result as $row)
    {
      print "<tr><td>".$row['Id']."</td>";
      print "<td>".$row['Breed']."</td>";
      print "<td>".$row['Name']."</td>";
      print "<td>".$row['Age']."</td></tr>";
    }
    print "</table>";

    // close the database connection
    $db = NULL;
  }
  catch(PDOException $e)
  {
    print 'Exception : '.$e->getMessage();
  }
?>

This worked, which is nice, but it created a file in my /public_html/ folder called 'localhost;dbname=user_db'

My issue is that I thought I was connecting to the database I created using cpanel, but when I open phpMyAdmin, that database is empty.

How do I change that script to talk to the database I created using cpanel so that I can reach it using phpMyAdmin?

Edit 2: So I learned that I need to use mysql, not sqlite because phpMyAdmin is based on MySQL.

Also, using the script from http://www.w3schools.com/php/php_mysql_create_table.asp I was able to connect! So, success! Thank you @mituw16 and @Fred -ii- for helping me! :D

  • 写回答

1条回答 默认 最新

  • du8980919 2015-03-27 13:39
    关注

    I am assuming you created a MySQL database if you used PHPMyAdmin even thought the tags say SQL Lite.

    It looks like you're trying to create an SQL Lite database instead of opening a connection to your MySQL database.

    Change this line to the following

    //open the database
    $db = new PDO('mysql:localhost;dbname=user_db', 'user_username', 'password');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择