简体   繁体   English

使用php连接到数据库服务器

[英]Connecting to database server using php

I have written this piece of code to connect to MySQL server. 我已经编写了这段代码以连接到MySQL服务器。 However it does not seem to be work. 但是,这似乎不起作用。 Could someone enlighten me as to why this is the case? 有人能启发我为什么会这样吗?

    <?php
$username = "user";
$password = "password";
$hostname = "hostname"; 

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password) 
  or die("Unable to connect to MySQL");
  echo "Connected to MySQL<br>";

  $selected = mysql_select_db("asantec",$dbhandle) 
  or die("Could not select asantec");

  $result = mysql_query("SELECT * FROM books");

//fetch tha data from the database 
while ($row = mysql_fetch_array($result)) {
   echo "tile:".$row{'title'}." author:".$row{'author'}."price: ". //display the results
   $row{'price'}."<br>";
}
//close the connection
mysql_close($dbhandle);
?>

This should be basically the mysqli version of what you wrote. 基本上,这应该是您编写的mysqli版本。

<?php
    $username = "user";
    $password = "pass";
    $hostname = "creative.coventry.ac.uk";

    $mysqli = new mysqli($hostname, $username, $password, "asantec");

    $stmt = $mysqli->prepare("SELECT * FROM books;");
    $stmt->execute();
    $stmt->bind_result($id, $title, $author, $price); //Put all your expected variables here

    while ($stmt->fetch()) {
        echo $id.'<br />';
        echo $title.'<br />';
        echo $author.'<br />';
        echo $price.'<br /><br />';
    }

    $mysqli->close();
?>

Also, if you have any issues in the code you've posted, this will work. 另外,如果您发布的代码中有任何问题,也可以使用。 Check your DB credentials and the data you're querying. 检查您的数据库凭据和查询的数据。

Excuse me, I couldn't help but notice the Coventry University server being displayed on here which if I'm right to assume, you are a Coventry University student? 对不起,我不禁注意到这里显示的是考文垂大学的服务器,如果我能正确假设,您是考文垂大学的学生吗?

Do you have permission to be publishing and exposing such information about the university servers on here? 您是否有权在此处发布和公开有关大学服务器的此类信息? This can lead to many security issues and can potentially cost the university a fortune to stop hackers infiltrating our databases. 这可能会导致许多安全问题,并有可能使大学付出巨大的代价来阻止黑客渗透到我们的数据库中。

Furthermore, you shouldn't be taking codes off of people on the internet if it is not stated as open source. 此外,如果未将代码声明为开放源代码,则不应从互联网上夺走代码。 This is an extremely serious case and you can very well be held accountable for level 3 plagiarism. 这是一个非常严重的案例,您很可能要对3级窃负责。

I too came across this, and due to the seriousness of the situation, even though I am no longer part of the Uni staff, I've still gone out of my way to report this to the Coventry university Academic Conduct Officer, who happen to take these issues very seriously. 我也碰到了这一点,由于情况的严重性,即使我不再是Uni员工的一部分,我仍然不遗余力地向考文垂大学学术操守干事报告这件事,非常重视这些问题。

Chris, you seem like a bright student, and if I'm correct you should be in your third and final year now. 克里斯,你看起来像是一个聪明的学生,如果我没错的话,你现在应该是第三年也是最后一年。 It baffles me as to why you'd put everything at jeopardy just to gain some help which you should be academically capable of doing yourself. 这让我感到困惑,因为为什么您会为了获得一些帮助而将一切置于危险之中,而您应该在学术上有能力做到这一点。

All the best, 祝一切顺利,

Mike Morgan 迈克·摩根

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM