gpt4 book ai didi

php - 连接到 MySQL DB 并检索信息

转载 作者:行者123 更新时间:2023-11-30 00:34:29 28 4
gpt4 key购买 nike

这是我第一次在网站中使用 MySQL,我需要一些帮助来完成所有设置。我已经创建了所有表并设置了主键,但是我对如何连接到数据库和检索信息有点迷失。我添加了 1 个示例相册,其中 albumdID 为 1 和 3 张示例照片。现在虽然我什至不确定我是否正在连接到数据库。我还设置了 config.php 文件。我将在下面发布我的代码,所有这些代码都来 self 的 Index.php 文件。谢谢

       <?php
// create the connection (host, username, pw, dbname)
// and give feedback if that fails
$con = mysqli_connect('localhost', 'tmh233sp14', 'ECHOB8Se', 'info230_SP14_tmh233sp14');

// check connection
if (mysqli_connect_errno()) {
printf('Connect failed: %s\n', mysqli_connect_error());
exit();
}


// construct the query
$albumID =1;
$sql = "SELECT Photos.photoID FROM Photos";
var_dump($sql);

// execute the query
$query = mysqli_query($con, $sql);

// get the result set
$result = mysqli_fetch_array($query);

// iterate over result array to display stuff
var_dump($result); // for debugging use var_dump, to see whats inside

// close the connection
mysqli_close($con);
?>

最佳答案

不要混合 mysql_* 和 mysqli_* 函数。我的建议:坚持使用 mysqli_*

这个例子应该可以帮助你:

<?php
// create the connection (host, username, pw, dbname)
// and give feedback if that fails
$con = mysqli_connect('localhost', 'tmh233sp14', 'password', 'info230_SP14_sjs334sp14');

// check connection
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}

// construct the query
$albumId =1
$sql = "SELECT Photos.photoID, Photos.name FROM photos
LEFT JOIN photoInAlbum ON (photoInAlbum.photoID = Photos.photoID)
WHERE albumID = $albumID";

// execute the query
$query = mysqli_query($con, $sql);

// get the result set
$result = mysqli_fetch_array($query);

// iterate over result array to display stuff
var_dump($result); // for debugging use var_dump, to see whats inside

// close the connection
mysqli_close($con);
?>

关于php - 连接到 MySQL DB 并检索信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22286013/

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