gpt4 book ai didi

php - 无法从数据库中选择记录

转载 作者:搜寻专家 更新时间:2023-10-30 23:23:05 25 4
gpt4 key购买 nike

我正在学习 PHP 和 SQL,并且正在尝试了解如何从数据库中选择记录。我创建了一个名为 selectById()

的函数

现在在浏览器中显示的是“错误:”但是,没有显示具体的错误。

 // function selectById --------------------------------------------------------------------
function selectById($pUInput) {
$sql = mysql_query("SELECT * FROM tblStudents
WHERE id='$pUInput[0]'");

if (!mysql_query($sql))
{
die('Error: ' . mysql_error());
}
echo "Record Selected";

}

PHP 代码:

//Call function mainline

mainline();

// Declare the function mainline
function mainline() {

$uInput = getUserInput();

$connectDb = openConnect(); // Open Database Connection
selectDb($connectDb); // Select Database
doAction($uInput);
//display();
//closeConnect();

}

//Declare function getUserInput --------------------------------------------------------
function getUserInput() {

echo "In the function getUserInput()" . "<br/>";

// Variables of User Input
$idnum = $_POST["idnum"]; // id (NOTE: auto increments in database)
$fname = $_POST["fname"]; // first name
$lname = $_POST["lname"]; // last name
$major = $_POST["major"]; // major
$year = $_POST["year"]; // year
$action = $_POST["action"]; // action (select, insert, update, delete)

$userInput = array($idnum, $fname, $lname, $major, $year, $action);

return $userInput;
}

function doAction($pUserInput) {
echo "In function doAction()" . "<br/>";

if ($pUserInput[5] == "sel") {
selectById($pUserInput);

} elseif ($pUserInput[5] == "ins") {
insert($pUserInput);
}

}

// Create a database connection --------------------------------------------------------
function openConnect() {
$connection = mysql_connect("localhost", "root_user", "password");
echo "Opened Connection!" . "<br/>";

if(!$connection) {
die("Database connection failed: " . mysql_error());
}

return $connection;
}

// Select a database to ----------------------------------------------------------------
function selectDb($pConnectDb) {
$dbSelect = mysql_select_db("School", $pConnectDb);
if(!$dbSelect) {
die("Database selection failed: " . mysql_error());
} else {
echo "You are in the School database! <br/>";
}

}

// function select ---------------------------------------------------------------------
function selectById($pUInput) {
$sql = mysql_query("SELECT * FROM tblStudents
WHERE id='$pUInput[0]'");

if (!mysql_query($sql))
{
die('Error: ' . mysql_error());
}
echo "Record Selected";

}

// function insert -----------------------------------------------------------------------------
function insert($pUInput) {

$sql="INSERT INTO tblStudents (first_name, last_name, major, year)
VALUES
('$pUInput[1]','$pUInput[2]','$pUInput[3]', '$pUInput[4]')";


if (!mysql_query($sql))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
}



?>

SQL 语法:

CREATE TABLE `tblStudents` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`first_name` varchar(30) NOT NULL,
`last_name` varchar(50) NOT NULL,
`major` varchar(40) NOT NULL,
`year` date NOT NULL,
PRIMARY KEY (`id`)
)

最佳答案

您正在对查询结果运行查询。这是行不通的。您将需要使用类似

的内容
$sql = mysql_query("SELECT * FROM tblStudents 
WHERE id='" . $pUInput[0] . "'");

if (!$row = mysql_fetch_assoc($sql))

如果查询没有失败,它将为 $row 分配一个数组值。您可能还想使用 mysql_real_escape_string 过滤 pUinput,因为您不一定知道它包含什么(或将其静态转换为整数)。

编辑

添加了一些额外的信息。

关于php - 无法从数据库中选择记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3613485/

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