我需要从MySQL数据库中获取一些信息并打印出来

时间:2014-04-04 10:27:37

标签: php mysql arrays select

我正在尝试从我的MySQL数据库中获取一些名为“opskriftreg”的信息,我希望它能将其打印出来,基本上它必须从我的数据库中检索已经创建的2x食谱,并相应地使用食谱列出它们“标题“首先”描述“

这是我的代码,如果这段代码是bollocks,如果有人能给我一个更好的例子,我会很高兴:

<?php

$mysqli = new mysqli ("localhost","root","","opskriftreg");
// Make a MySQL Connection

if (isset($_POST['title']) && isset($_POST['description']))
{
    $username= $_POST['title'];
    $password= $_POST['description'];
    $query = "SELECT *
                      FROM opskriftreg
                      WHERE title = '".mysql_real_escape_string($title)."'
                      AND description = '".mysql_real_escape_string($description)."'";
    $result = mysql_fetch_array(mysql_query($query));
    # ...
}
else
{
    return NULL;
}

?>

提前谢谢: - )

编辑:

代码更改为以下内容 - 收到空白页面,其中不返回任何内容或打印:

<?php
$mysqli = new mysqli("localhost", "root", "", "opskriftreg");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$query = "SELECT title, description FROM opskriftreg ORDER by id DESC LIMIT 50,5";

if ($result = $mysqli->query($query)) {

    /* fetch object array */
    while ($row = $result->fetch_row()) {
        printf ("%s (%s)\n", $row[0], $row[1]);
    }

    /* free result set */
    $result->close();
}

/* close connection */
$mysqli->close();
?>

2 个答案:

答案 0 :(得分:0)

echo $result['column_name'];

并且mysql_*函数已弃用,请改用mysqli

http://nl3.php.net/mysqli_real_escape_string

答案 1 :(得分:0)

试试这个,寻找我所做的改变。

<?php
$mysqli = new mysqli("localhost", "root", "", "opskriftreg");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$query = "SELECT title, description FROM opskriftreg ORDER by id DESC";

$statement = $mysqli->prepare($query);
$statement->execute();

$results = $statement->get_result();

while ($result = $results->fetch_assoc()) {
    printf ("%s (%s)\n", $result['title'], $result['description']);
}

/* close connection */
$mysqli->close();
?>