douweidao3882 2017-08-20 03:12
浏览 58

我的托管公司在这里弄乱我:无法通过套接字'/var/lib/mysql/mysql.sock'(2)连接到本地MySQL服务器?

I use the hosting company aPlus.net, and I can't seem to get past a connection error I'm getting when trying to process some php to write database content to a webpage, and I am curious as to if this is because my database appears to not be on the same server as the entire rest of my hosting account, and if there is a way to resolve this in my code? This is my first attempt at writing PHP, and it would be good to know if my code is wrong, or if my hosting company is messing me up. (and either way, how to fix it)

Here's the code that's failing to pull from the database:

{
  $con = mysql_connect("localhost","2p5dq9vxmy240651","MY_PASSWORD");
  if (!$con)
  {
    die('Could not connect: ' . mysql_error());
  }
  mysql_select_db("felineasthma_2p5dq9vxmy240651", $con);
  $users_name = $_POST['name'];
  $users_comment = $_POST['requests'];
  $users_name = mysql_real_escape_string($users_name);
  $users_comment = mysql_real_escape_string($users_comment);
  $inputid = $_GET['id'];
  $query = "
  INSERT INTO `felineasthma_2p5dq9vxmy240651`.`submissions` (`id`,
   `name`, `requests`, `inputid`) VALUES (NULL, 
   '$users_name', '$users_comment', '$inputid');";
  mysql_query($query);
  echo "<h2>Your request has been processed, reload page.</h2>";
  mysql_close($con);
}

and here's some screen captures from inside my hosting account (links because I don't have enough posts here yet to upload images, sorry):

felineasthma_2p5dq9vxmy240651 doesn't appear in my hosting account

yet it clearly exists in MySQL Manager, but on a different server

I was even more confused while making the user for this database, as the control panel didn't allow me to make a username, it just randomly assigned one. Help? Advice?

  • 写回答

1条回答 默认 最新

  • dongpiao0731 2017-08-22 03:20
    关注

    I found a more modern tutorial to learn PHP with, and now everything works, I just need to add security measures now. Here's the working code snippets, if anyone ever comes here asking the same questions.

    here's the form action that places the entries into the database:

    <?php
    $servername = "sql5c40n.carrierzone.com";
    $username = "my_username";
    $password = "my_password";
    $dbname = "my_database";
    $users_name = $_POST['name'];
    $users_request = $_POST['requests'];
    
    // Create connection
    $conn = mysqli_connect($servername, $username, $password, $dbname);
    // Check connection
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }
    
    $sql = "INSERT INTO submissions (name, requests)
    VALUES ('$users_name', '$users_request')";
    
    if (mysqli_query($conn, $sql)) {
        header("Location: clv.php");
    } else {
        echo "Error: " . $sql . "<br>" . mysqli_error($conn);
    }
    
    mysqli_close($conn);
    ?> 
    

    here's the include that puts the database entries onto the page:

    <?php
    $servername = "sql5c40n.carrierzone.com";
    $username = "my_username";
    $password = "my_password";
    $dbname = "my_database";
    
    // Create connection
    $conn = mysqli_connect($servername, $username, $password, $dbname);
    // Check connection
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }
    
    $sql = "SELECT id, requests, name FROM submissions";
    $result = mysqli_query($conn, $sql);
    
    if (mysqli_num_rows($result) > 0) {
        // output data of each row
        while($row = mysqli_fetch_assoc($result)) {
            echo "" . $row["requests"]. " - by " . $row["name"]. "<br>";
        }
    } else {
        echo "0 results";
    }
    
    mysqli_close($conn);
    ?> 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog