我很难通过列名和单个行号同时引用MySQL数据。
以下代码示例执行以下操作;
成功
但我无法同时成功引用各行(按编号)和列(按名称)。
道歉,如果这是一个荒谬的问题,我是PHP和MySQL的新手。
我认为我的描述很糟糕,所以也许这会有所帮助。在可用的四个选项中(按名称或数字列,以及单独行或循环遍历组),它的工作方式如下;
有人对我做错了什么有任何建议吗?
代码:
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Persons");
echo "<table border='1'>
<tr>
<th>Firstname</th>
<t
h>Lastname</th>
<th>test</th>
<th>order</th>
</tr>";
$ind = 0;
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . ($row['age'] * 1) . "</td>";
echo "<td>" . ($row['a']) . "</td>";
echo "<td>" . $ind. "</td>";
echo "</tr>";
$ind += 1;
}
echo "</table>";
$sql="SELECT * FROM Persons ORDER BY PID";
if ($result=mysqli_query($con,$sql))
{
// Prints single entry AP
print "<br>";
$rowNumber = $id;
mysqli_data_seek($result, $rowNumber);//start row is whatever you need to be the first row
$row = mysqli_fetch_row($result);
print "<br>";
//-----------------------------------------------------
$mathString = $row[1];
$description = $row[4];
echo "math string:" . $mathString . "<br>";
echo "a is said to equal:" . $description . " - " . $row['a'];