简单的php + mysql从数据库打印变量

时间:2014-04-16 15:55:09

标签: php mysql

很抱歉打扰了你们,但我真的很挣扎这个:

我很好地连接到我的数据库然后尝试以下mysql语句:

$query1 = "select row1 from mydatabase where row2 = $Name ";
$answer1 = mysql_query($query1);

然而,我尝试后几行:

echo $answer1;

我只给出了空值:(

有人可以给我任何建议吗?

编辑: SQL登录:

mysql_connect("correct", "username", "password");

mysql_select_db("dbname") or die(mysql_error());

3 个答案:

答案 0 :(得分:4)

你所做的一切都是对的,你只需要获取这样的数据:

$query1 = "select row1 from mydatabase where row2 = $Name ";
$answer1 = mysql_query($query1);

while($data= mysql_fetch_array($answer1)){
    echo $data['row1'];
}

这是一个完整的答案,我会根据你的需要进行调整;)

<?php
    //Connect to your database
    $con=mysqli_connect("db_hostname","db_user","db_password","db_name");

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

    //Value of the row to select
    $row2 = 'some value';

    //Make select query
    $result = mysqli_query($con, "SELECT row1 FROM MyTable WHERE row2='$row2'");

    //Fetch datas
    while($row = mysqli_fetch_array($result))
    {
        echo $row['row1'];
        echo "<br>";
    }

    //Close database
    mysqli_close($con);
?>

祝你好运:)

答案 1 :(得分:1)

尝试使用MySQLi_*代替MySQL_*函数,并将连接变量传递给函数调用。

如果这不起作用,那么您可能希望通过启用所有错误报告并转储全局范围来尝试进一步调试。

<?php
error_reporting(E_ALL); // Show all errors & warnings

$conn = mysqli_connect("server", "username", "password");
mysqli_select_db($conn, "dbname") or die(mysql_error());

$sql1 = "SELECT `row1` FROM `mydatabase` WHERE `row2` = '".$Name."';";
$query1 = mysqli_query($conn, $sql1);
$answer1 = mysqli_fetch_assoc($query1);

var_dump($GLOBALS); // Dumps all variables in the global scope
?>

答案 2 :(得分:-1)

$answer1= mysql_query($query1);

之后添加此内容
while ($row = mysql_fetch_assoc($answer1)) {
   // echo data
   echo $row['row1'];
}