无法从数据库获取URL链接

时间:2016-03-26 16:52:44

标签: php mysql html5

我正在做一个项目,并将永远感谢帮助我的URl链接。我试过四处寻找无济于事。我有一个数据库(4columns)。最后一个(link1)应该链接到具有指定URL的视频。当表格出现时,URL不可点击(有没有办法简化这个说“点击我”?)。这是我的代码。我还附上了桌子的图像。这真的是我的大脑,谢谢。

<?php
    $con = mysqli_connect("localhost","feedb933_charles","pass100","feedb933_test");

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

    $sql = "SELECT * FROM videos";
    $result = mysqli_query($con, $sql); 

    echo "<table>"; 
    echo "<tr> 
    <th>topic1</th> 
    <th>subject1</th> 
    <th>link1</th> 
    </tr>"; 
    while( $row = mysqli_fetch_array( $result)) { 
        $topic1 = $row["topic1"]; 
        $subject1 = $row["subject1"]; 
        $link1 = $row["link1"]; 

        echo "<tr> 
        <td>$topic1</td>
        <td>$subject1</td>
        <td>$link1</td>
        </tr>";
    }
    echo "</table>";
    mysqli_close($con);
?>

Table output

3 个答案:

答案 0 :(得分:0)

试试这个:

<?php
    $sql = "SELECT * FROM `videos`";
    $result = mysqli_query($con, $sql);
?> 
<table>
<?php 
   while($row = mysqli_fetch_assoc( $result)) { 
 ?>       
    <tr> 
        <td><?php echo $row['topic1'];?></td>
        <td><?Php echo $row['subject1'];?></td>
        <td><a href="<?php echo $row['link1']; ?>" target="_blank">Click me</td>
  </tr>
  <?php } ?>
<table>

或者你也可以使用do while循环:

    do{
        echo '<tr>'; 
        echo      '<td>'.$row['topic1'].'</td>';
        echo      '<td>'.$row['subject1'].'</td>';
        echo      '<td><a href="'.$row['link1'].'" target="_blank">Click me</td>';
        echo '</tr>';
 } while($row = mysqli_fetch_assoc( $result);

我添加了target属性以在新窗口中打开链接。

答案 1 :(得分:0)

我查看了你的代码,发现了一些错误。

$con = mysqli_connect("localhost","feedb933_charles","pass100","feedb933_test");更改为$con = new mysqli("localhost", "feedb933_charles", "pass100", "feedb933_test");

然后将if (mysqli_connect_errno())更改为if (mysqli_connect_error()) {

然后改变

$sql = "SELECT * FROM videos";

$sql = "SELECT topic1, subject1, link1 FROM videos";

或者如果您想选择一行

$differentvalue = ""; // value to run
$sql = "SELECT topic1, subject1, link1 FROM videos WHERE difvalue = ?";

difvalue是其他值不同的值,因此php代码知道要抓取什么。

使用stmt意味着没有sql注入

添加:

$stmt = $con->stmt_init();
if (!$stmt->prepare($sql))
{
    print "Failed to prepare statement\n";
}
else
{

}

然后在if (something) { } else { IN HERE }

(如果您有WHERE diffvalue)添加:

$stmt->bind_param("s", $differentvalue); // $stmt->bind_param("s", $differentvalue); if text, $stmt->bind_param("i", $differentvalue); if integer

然后

添加:

$stmt->execute();
$list = $stmt->fetchAll();

然后在if (something) { code } else { code }

之外

添加:

echo "<table><tr><th>topic1</th><th>subject1</th><th>link1</th></tr><tr>";

foreach ($list as $row => $new) {
    $html = '<td>' . $new['topic1'] . '</td>';
    $html .= '<td>' . $new['subject1'] . '</td>';
    $html .= '<td>' . $new['link1'] . '</td>';
    echo $html;
}

echo "</tr></table>";

如果您仍然遇到问题,请转到列出的链接

http://php.net/manual/en/pdostatement.fetchall.php

http://php.net/manual/en/mysqli-stmt.get-result.php

http://php.net/manual/en/mysqli-result.fetch-all.php

希望这有帮助

答案 2 :(得分:0)

<?php
$con=mysqli_connect("localhost","feedb933_charles","pass100","feedb933_test");

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

    $sql = "SELECT * FROM videos";
    $result = mysqli_query($con, $sql); 

    echo "<table>"; 
    echo "<tr> 
    <th>topic1</th> 
    <th>subject1</th> 
    <th>link1</th> 
    </tr>"; 
    while( $row = mysqli_fetch_array( $result)) { 
        $topic1 = $row["topic1"]; 
        $subject1 = $row["subject1"]; 
        $link1 = $row["link1"]; 

        echo "<tr> 
        <td>$topic1</td>
        <td>$subject1</td>
        <a href="'.$link.'"><td>$link1</td></a>
*//this href will give u the link as u asked. //be sure you store proper url. In case of exact video name saved in column thn can make the url for your web output. //ex:<a href="http://example.com/'.$link.'.extension">*

        </tr>";
    }
    echo "</table>";
    mysqli_close($con);
?>