gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:24:47 25 4
gpt4 key购买 nike

我写了这段代码来连接MySQL服务器。然而这似乎不起作用。有人可以告诉我为什么会出现这种情况吗?

    <?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);
?>

最佳答案

这基本上应该是您编写的 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();
?>

此外,如果您发布的代码有任何问题,这也可以。检查您的数据库凭据和您正在查询的数据。

关于php - 使用php连接数据库服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18131019/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com