簡體   English   中英

從SQL數據庫顯示隨機行

[英]Displaying Random Row from SQL Database

我已經工作了好幾天了,我已經死了。 與GoDaddy支持人員交談后,我確信我在運行腳本時擁有正確的主機名,用戶名/密碼,但仍然無法通過die()

最終,我試圖從數據庫中提取一個問題。 我已經梳理了這個網站,但沒有發現似乎可以回答我的問題。 請幫忙。

<?php
    $hostname='localhost';
    $username='username';
    $password='password';
    $dbname='qod';
    $usertable='Questions';
    $userfield='question';

    mysql_connect($hostname,$username, $password) or die ("<html><script language='JavaScript'>alert('Unable to access the Question of the Day! Please try again later.'),history.go(-1)</script></html>");
    mysql_select_db($dbname);
    # Check If Record Exists

    $query = 'SELECT $.userfield FROM $.usertable ORDER BY RAND() LIMIT 1';
    $result = mysql_query($query);

    if($result){
        while($row = mysql_fetch_array($result)){
            $name = $row[$yourfield];
        echo "Name: ".$name;}
    }
?>

您在SELECT變量中使用了點,不應有任何點。

SELECT $.userfield FROM $.usertable ,然后調用它們:

$usertable='Questions';
$userfield='question';

從SELECT中將其刪除 ,並使用適當的錯誤報告技術,例如:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of code

or die('Could not connect: ' . mysql_error()); 

在列名[$yourfield]也加一個美元符號; 也將其刪除。

您應該使用引號:即: ['yourfield'] - yourfield是您要顯示的表中的列名。


腳注:

mysql_*函數棄用通知:

http://www.php.net/manual/zh/intro.mysql.php

自PHP 5.5.0起不推薦使用該擴展,不建議編寫新代碼,因為將來會刪除該擴展。 相反,應使用mysqliPDO_MySQL擴展名。 另請參見MySQL API概述,以獲取選擇MySQL API時的更多幫助。

這些功能使您可以訪問MySQL數據庫服務器。 有關MySQL的更多信息,請參見» http://www.mysql.com/

可以在» http://dev.mysql.com/doc/中找到MySQL的文檔。

mysqli_*與預處理語句一起使用 ,或將PDO預處理語句一起使用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM