回声MySQL数据库

时间:2017-02-22 08:46:22

标签: php mysql

我正试图从我的数据库中获得一个问题。我只想从数据库中获取一个东西而不是一行。我认为这会有效,但它提出了这个问题:资源ID#4可以解释我所遗漏的内容。

谢谢:)

    <?php 
   $dbhost = 'localhost';
   $dbuser = 'root';
   $dbpass = '';

   $conn = mysql_connect($dbhost, $dbuser, $dbpass);

    mysql_select_db('lotto');   
    $sql = 'SELECT id, vraag, AntwA, AntwB, AntwC, AntwD FROM vraag1';
    $test = mysql_query($sql);
    echo $test;   

?>

5 个答案:

答案 0 :(得分:3)

至少在互联网上说过10000次,永远不要使用MySQL_! (如果您正在尝试通过互联网使用教程来学习新内容,请不要使用旧教程)

我建议使用PDO,它是PHP中的现代API,并且在准备好的语句中使用它时更加安全!但是你也可以使用更类似于MYSQL的MYSQLI!

您必须从返回数组中导出数据:

使用PDO:

$db = new PDO ("mysql:host=".$hostname.";dbname=".$dbname, $username, $password);

$query = $db -> prepare ("SELECT * FROM vraag1");

    $query -> execute (array ());

    $rows = $query -> fetchAll (PDO::FETCH_ASSOC);

    foreach ($rows as $row)
    {
        echo $id = $row["id"];
                    echo $vraag = $row["vraag "];
                    echo $AntwA = $row["AntwA "];
                    echo $AntwB = $row["AntwB "];
                    echo $AntwC = $row["AntwC "];
                    echo $AntwD = $row["AntwD "]; 
    }

使用MYSQLI:

$db = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);

$query = "SELECT * FROM vraag1";
$rows = mysqli_query($db, $query);

while($row = mysqli_fetch_assoc($rows))
{
   echo $row["id"];
   echo $row["vraag"];
   echo $row["AntwA"];
   echo $row["AntwB"];
   echo $row["AntwC"];
   echo $row["AntwD"];
}

答案 1 :(得分:2)

首先,您使用的mysql函数已弃用且不再受支持。你应该使用mysqli或pdo代替预备语句。

<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "lotto";

// Create connection
$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT id, vraag, AntwA, AntwB, AntwC, AntwD FROM vraag1";
$test = mysqli_query($conn, $sql);

if (mysqli_num_rows($test) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($test)) {

       echo "ID : ".$row['id']."<br>";
       echo "vraag :".$row['vraag']."<br>";
       echo "AntwA :".$row['AntwA']."<br>";
       echo "AntwB :".$row['AntwB']."<br>";
       echo "AntwC :".$row['AntwC']."<br>";
       echo "AntwD :".$row['AntwD']."<br>";
    }
} else {
    echo "no results found";
}

mysqli_close($conn);
?>

答案 2 :(得分:0)

对于select函数,mysql_query()在成功时返回资源,如果出错则返回FALSE。 所以你的任务说明

$test = mysql_query($sql);

将资源分配给$ test。

如果您想要资源中的数据,您可以

while($row= mysql_fetch_assoc($test)):
    print_r($row);
endwhile;

您也可以访问$ row [&#39; column_name&#39;]

如果您只想返回一行,可以在查询中执行此限制

$sql = 'SELECT id, vraag, AntwA, AntwB, AntwC, AntwD FROM vraag1 limit 1';

答案 3 :(得分:-1)

您需要添加以下内容:

while($row = mysql_fetch_array($result)){
    echo $row['id'];
    echo $row['vraag'];
    echo $row['AntwA'];
    echo $row['AntwB'];
    echo $row['AntwC'];
    echo $row['AntwD'];
}

答案 4 :(得分:-2)

使用mysqli而不是mysql

<?php 
   $dbhost = 'localhost';
   $dbuser = 'root';
   $dbpass = '';

   $conn = mysqli_connect($dbhost, $dbuser, $dbpass);

    mysqli_select_db('lotto');   
    $sql = 'SELECT id, vraag, AntwA, AntwB, AntwC, AntwD FROM vraag1';
    $test = mysqli_query($sql);
    echo $test;   

?>