大家好,
我只是MySQLi
的新手。但我在MySQL方面有很强的背景。事情是这样的。在MySQL中,这是我如何从客户端数据的DB(数据库)中声明和检索变量的方法:
<table cellspacing="0" width="100%">
<thead>
<tr>
<th><strong>Name:</strong></th>
<th><strong>Date of Birth:</strong></th>
<th><strong>Birthplace:</strong></th>
<th><strong>Gender:</strong></th>
<th><strong>Email Add:</strong></th>
<th><strong>Contact#:</strong></th>
<th><strong>Address:</strong></th>
</tr>
</thead>
<?php
$sql=mysql_query("--Sample Query using Inner Join to retrieve values taken from 3 tables on DB");
$counter=0;
while($row=mysql_fetch_array($sql))
{
$cname = $row['name'];
$cbday = $row['birthday'];
$pob = $row['pob'];
$cgen = $row['gender'];
$email = $row['email'];
$contact= $row['contact'];
$add = $row['address'];
if($counter%2)
{
?>
<tbody>
<?php } else { ?>
<tr>
<?php } ?>
<td><?php echo $cname; ?></td>
<td><?php echo $cbday; ?></td>
<td><?php echo $pob; ?></td>
<td><?php echo $cgen; ?></td>
<td><?php echo $email; ?></td>
<td><?php echo $contract; ?></td>
<td><?php echo $add; ?></td>
</tr>
</tbody>
<?php
$counter++;
}//while-end
?>
</table>
看到我是如何将获取的值放在索引页上的表上的吗?这也是在mysql_fetched_array()
下。我使用echo然后我声明的变量名来调用所有$row['values']
;
但是当我检查MySQLi教程时,这是他们如何使用mysql_fetched_array()
:
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %sn", mysqli_connect_error());
exit();
}
$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5";
if ($result = mysqli_query($link, $query)) {
/* fetch associative array */
while ($row = mysqli_fetch_assoc($result)) {
printf ("%s (%s)n", $row["Name"], $row["CountryCode"]);
}
/* free result set */
mysqli_free_result($result);
}
/* close connection */
mysqli_close($link);
?>
我的问题是,在MySQLi中命名变量是否与MySQL相同?就像我对图1所做的那样吗?[请参阅上面的代码]
另外,使用mysqli_fetch_assoc
的工作原理非常像mysql_fetch_assoc
,只有一个(i)可以添加到它?还是这两个熊不同的东西?
试试下面的代码
<table cellspacing="0" width="100%">
<thead>
<tr>
<th><strong>Name:</strong></th>
<th><strong>Date of Birth:</strong></th>
<th><strong>Birthplace:</strong></th>
<th><strong>Gender:</strong></th>
<th><strong>Email Add:</strong></th>
<th><strong>Contact#:</strong></th>
<th><strong>Address:</strong></th>
</tr>
</thead>
<?php
mysqli_connect("localhost", "my_user", "my_password", "world");
$sql=mysqli_query("--Sample Query using Inner Join to retrieve values taken from 3 tables on DB");
$counter=0;
while($row=mysqli_fetch_assoc($sql))
{
$cname = $row['name'];
$cbday = $row['birthday'];
$pob = $row['pob'];
$cgen = $row['gender'];
$email = $row['email'];
$contact= $row['contact'];
$add = $row['address'];
if($counter%2)
{
?>
<tbody>
<?php } else { ?>
<tr>
<?php } ?>
<td><?php echo $cname; ?></td>
<td><?php echo $cbday; ?></td>
<td><?php echo $pob; ?></td>
<td><?php echo $cgen; ?></td>
<td><?php echo $email; ?></td>
<td><?php echo $contract; ?></td>
<td><?php echo $add; ?></td>
</tr>
</tbody>
<?php
$counter++;
}//while-end
?>
</table>
希望这对你有帮助