我还在学习功能,而且有点不确定。
我这里有这个代码:
<?php
include ('blah.html');
$conn = mysqli_connect("localhost","root","","testing");
// Check connection
if (mysqli_connect_errno())
{
echo "<p>Failed to connect to MySQL: " . mysqli_connect_error() . "</p>";
}
else
{
echo "<p>Connected to mysql and the testing database</p>";
}
/*function produceTable(){
$result = mysqli_query($conn,"SELECT studentid, CONCAT(firstname,' ', lastname) as name,phone, email, courseid FROM students WHERE courseid > 1500 ORDER BY lastname");
return $result;
}*/
$result = mysqli_query($conn,"SELECT studentid, CONCAT(firstname,' ', lastname) as name,phone, email, courseid FROM students WHERE courseid > 1500 ORDER BY lastname");
$numRecords = mysqli_num_rows($result);
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td align = 'center'>".$row['studentid']."</td>";
echo "<td>".$row['name']."</td>";
echo "<td>".$row['phone']."</td>";
echo "<td>".$row['email']."</td>";
echo "<td>".$row['courseid']."</td>";
echo "</tr>";
}
echo "</table>";
echo "<p>".$numRecords." records found with course id larger than 1500</p>";
?>
</body>
</html>
正如您所看到的,我已经注释掉了这个功能。如果忽略该代码的功能并输出我想要的记录,但我需要能够使它与函数一起工作。该函数需要被称为produceTable()并接受一个名为$ result的参数。它还需要使用mysqli_fetch_array()来输出记录。
答案 0 :(得分:0)
你的功能是下一个:
function produceTable(){
$result = mysqli_query($conn, "SELECT ...");
return $result;
}
$conn
没有任何价值。不是吗?尝试将此参数传递给函数
答案 1 :(得分:0)
在produceTable()
$conn
中undefined
。只需将$conn
作为参数传递即可。像这样
function produceTable($conn){
$result = mysqli_query($conn,"SELECT studentid, CONCAT(firstname,' ', lastname) as name,phone, email, courseid FROM students WHERE courseid > 1500 ORDER BY lastname");
return $result;
}
$result = produceTable($conn);
$numRecords = mysqli_num_rows($result);
while($row = mysqli_fetch_array($result)) {
.....
}