MySQL Fetch Array Each Row Function


MySQL Fetch Array Each Row Function

这是我第一次使用这个网站,我最近开始做一个项目,在这个项目中,网页将显示两个表,其中一个表将从SQL数据库收集所有信息,并将它们放在表中,每行旁边都有一个按钮,允许您将该记录插入到另一个表中,到目前为止,这是我写的代码:

//Include the Following Files
include 'connect.php';
//Select All Price Plans
$query = "SELECT * FROM pricing";
$result = mysql_query($query);
//Print all Plans in Table 
while ($pricing = mysql_fetch_array($result)) 
{
    echo "<table border=1>";
    echo "<tr>";
    echo "<td>" .$pricing['planID'].  "</td>";
    echo "<td>" .$pricing['planTitle']. "</td>";
    echo "<td>" .$pricing['planDescription']. "</td>";
    echo "<td>" .$pricing['planPricing']. "</td>";
    echo "<td>" . **<a href="'. $link .'">BUTTON HERE</a>** . "</td>";
    echo "</tr>";
    echo "<table>";
}  

//Assign $link to Case     
$link = '?run=planA'; 
//Pre-defined Functions
function planA()
{ 
$query = "INSERT into restaurant_plan (`planID`, `planTitle`, `planDescription`, `planPricing`) SELECT * FROM pricing WHERE planID='2' ";
$result = mysql_query($query);
} 
//Setting Case to Run Each Functions
if (isset($_GET['run'])) $linkchoice=$_GET['run']; 
else $linkchoice=''; 
switch($linkchoice)
{       
    case 'planA': 
    planA(); 
    break; 
    case 'planB': 
    planB(); 
    break;              
}     

有人能为我如何为每行分配函数提供任何指南或可能的示例吗?非常感谢!

您的代码为表"pricing"中的每条记录打印一个表,请使用以下代码:

//Select All Price Plans
$mysqli = new mysqli("hostname", "username", "pass", "dbname");
$query = "SELECT * FROM pricing";
$result = $mysqli->query($query);
//Print all Plans in Table    
echo "Available Price Plans";          
echo "<table border=1>";
while ( $pricing = $result->fetch_assoc() ) {
    echo "<tr>";
    echo "<td>" .$pricing['planID'].  "</td>";
    echo "<td>" .$pricing['planTitle']. "</td>";
    echo "<td>" .$pricing['planDescription']. "</td>";
    echo "<td>" .$pricing['planPricing']. "</td>";
    echo "<td>" .'<a href="'. $link .'"><img style="border:none;" src="'. $icon .'" /></a>'. "</td>";
    //print a button as you mentioned
    echo "<td>"."<form action='#' method='get'><input type='hidden' name='id' value='".$pricing['planID']."'/><input type='submit' value='copy'/></form></td>";
    echo "</tr>";
}
echo "</table>";

以及您的功能:

function planA()
{ 
    // get selected plan info
    $query = "SELECT * FROM pricing";
    $result = $mysqli->query($query);
    $row = $res->fetch_assoc();
    //copy info to table 'restaurant_plan'
    $query = "INSERT into restaurant_plan (".$_GET["id"].", ".$row["planTitle"].", ".$row["planDescription"].", ".$row["planPricing"].")";
    $result = $mysqli->query($query);

}

不是一个答案,但如果您希望您的代码更干净&更具可读性/可维护性的是,应该使用插入PHP的HTML,而不是通过PHP:回显HTML

<?php
//Select All Price Plans
$query = "SELECT * FROM pricing";
$result = mysql_query($query);
//Print all Plans in Table     
?>
Available Price Plans
<?php while ($pricing = mysql_fetch_array($result)) : ?>
    <table border=1>
        <tr>
            <td><?= $pricing['planID'] ?></td>
            <td><?= $pricing['planTitle'] ?></td>
            <td><?= $pricing['planDescription'] ?></td>
            <td><?= $pricing['planPricing'] ?></td>
            <td><a href="<?= $link ?>"><img style="border:none;" src="<?= $icon ?>" /></a></td>
        </tr>
    <table>
<?php endforeach; ?>

此外,正如我在上面的评论中提到的,您应该停止使用mysql_*函数。他们被弃用了。相反,使用PDO(从PHP 5.1开始支持)或mysqli(从PHP 4.1开始支持)。

将表输出更改为以下内容:

<table border="1">
<?php
while ($pricing = mysql_fetch_array($result)) { 
?>
       <tr>
       <td><?php echo $pricing['planID']; ?></td>
       <td><?php echo $pricing['planTitle']; ?></td>
       <td><?php echo $pricing['planDescription']; ?></td>
       <td><?php echo $pricing['planPricing']; ?></td>
       <td><a href='<?php echo $link; ?>'><img style="border:none;" src='<?php echo $icon; ?>' /></a></td> 
       </tr>
   <?php     
   }  
   ?>
<table>

还要仔细检查数据库连接,以确保可以检索数据。