使用PHP和SQL检索数据时遇到问题

时间:2018-11-12 11:29:47

标签: php sql database

我正在努力从包含基本信息的表中检索数据。我尝试使用odbc_fetch函数,但无法使其正常工作。有人可以告诉我如何从特定行中检索数据

<?php
session_start();
?>
<html>
<head>
    <title>Profile</title>
</head>

<body>
<?php

$employeeNumber = $_SESSION["user"];

$connect=odbc_connect("CoveringSystem", "", "");
$getData="SELECT FirstName, LastName FROM Details WHERE EmployeeNumber ='$employeeNumber'";

$result = odbc_exec($connect, $getData);

## //I've tried to add the odbc_fetch functions here\\ ##

echo $employeeNumber;
echo $firstName;
echo $lastName;

?>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

使用mysqli进行检查:

// First connect to database 
$con = mysqli_connect("localhost","my_user","my_password","my_db");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }


// Than make your query
$getData=mysqli_query($con,"SELECT FirstName, LastName FROM Details WHERE EmployeeNumber ='$employeeNumber'");
//Count row returned 
if(mysqli_num_rows($getData)>0){
while($row=mysqli_fetch_assoc($getData)){
 $firstName=$row['FirstName'];
$lastName=$row['LastName'];

//then you can do whatever you like with your data 
}

}else{

}