douduan1953
2016-03-27 12:57
浏览 11
已采纳

如何在PHP中使用fetch [关闭]

I've written the following code:

        <?php
$db_host="localhost";
$db_user="root";
$db_pass="";
$db_name="formend";
$db_table="jadval";
$con = mysql_connect($db_host,$db_user,$db_pass) or die("خطا در اتصال به پايگاه داده");
$selected=mysql_select_db($db_name, $con) or die("خطا در انتخاب پايگاه داده");
$dbresult=mysql_query("SELECT * FROM `jadval`");
while ($amch = mysql_fetch_assoc($dbresult)) {
    echo $amch["id"];
    echo $amch["name"];
}
?>

But this does not work Only one page was empty plz help me ...

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • douying7289 2016-03-27 13:06
    已采纳

    Use mysqli_* instead as mysql_* has already been deprecated in PHP 5.5 and removed in PHP 7:

    <?php
    $db_host = "localhost";
    $db_user = "root";
    $db_pass = "";
    $db_name = "formend";
    $db_table = "jadval";
    $con = mysqli_connect($db_host, $db_user, $db_pass, $db_name) or die("خطا در اتصال به پايگاه داده");
    
    $dbresult = mysqli_query($con, "SELECT * FROM `jadval`");
    while ($amch = mysqli_fetch_array($dbresult)) {
        echo $amch["id"];
        echo $amch["name"];
    }
    ?>
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • doujuegai8830 2016-03-27 13:12

    PDO will work on 12 different database systems, where as MySQLi will only work with MySQL databases. (from w3schools)

    using PDO:

    <?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    
    try {
    $conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
    }catch(PDOException $e){}
    $sth = $conn->prepare("SELECT * FROM jadval");
    $sth->execute();
    
    /* Fetch all of the remaining rows in the result set */
     print("Fetch all of the remaining rows in the result set:
    ");
     $result = $sth->fetchAll();
     print_r($result);
    

    I hope its helpful!

    评论
    解决 无用
    打赏 举报
  • duanmen2189 2016-03-27 13:26

    As per your code:

        <?php
    $db_host="localhost";
    $db_user="root";
    $db_pass="";
    $db_name="formend";
    $db_table="jadval";
    $con = mysql_connect($db_host,$db_user,$db_pass) or die("خطا در اتصال به پايگاه داده");
    $selected=mysql_select_db($db_name, $con) or die("خطا در انتخاب پايگاه داده");
    $dbresult=mysql_query("SELECT * FROM jadval");
    while ($amch = mysql_fetch_array($dbresult)) {
        echo $amch["id"];
        echo $amch["name"];
    }
    ?>
    

    instead of mysql_fetch_assoc, you should use mysql_fetch_array.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题