我正在创建一个数据库来制作我的'PHP'网站,但我无法做到这一点。我的网站cruzapp与rideshare公司相关,并将其更改为php
以获取有关我们用户的详细信息。但是我无法使用以下MYSQL
代码连接PHP
:
?php
$username = "name";
$password = "password";
$hostname = "host";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Not connected to MySQL");
echo "Connected to MySQL<br>";
//select a database to work with
$selected = mysql_select_db("examples",$dbhandle)
or die("Could not select examples");
//execute the SQL query and return records
$result = mysql_query("SELECT id, model,year FROM cars");
//fetch tha data from the database
while ($row = mysql_fetch_array($result)) {
echo "ID:"$row{'id'}." Name:".$row{'model'}."Year: ". //display the results
$row{'year'}.<br>";
}
//close the connection
mysql_close($dbhandle)
?>
有人可以帮我调试这段代码吗? 我将非常感谢你。
答案 0 :(得分:1)
试一试。它使用MySQLi进行错误回显。
<?php
$username = "name";
$password = "password";
$hostname = "host";
$database = "examples";
$con = mysqli_connect($hostname, $username, $password, $database);
if (!$con) {
exit("Connection failed: " . mysqli_connect_error());
}
$result = mysqli_query($con, "SELECT id, model,year FROM cars");
if (mysqli_error($con)) {
exit("Error: " . mysqli_error($con));
}
while ($row = mysqli_fetch_array($result)) {
echo "ID:" . $row['id'] . " Name:" . $row['model'] . "Year: " . $row['year'] . "<br>";
}
mysqli_close($con);
答案 1 :(得分:0)
首先你不应该使用mysql因为PHP 7 mysql扩展不再使用了。所以你必须考虑将其改为mysqli或PDO。建议使用PDO。任何如何快速修复$ selected = mysql_select_db($ dbhandle,“examples”)执行此操作,并检查所有值,如主机名数据库名称表名称,并确保没有错误。