dongpixi2648 2014-04-16 15:55
浏览 35
已采纳

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

sorry to bother you all but I'm really struggling with this one:

I connect to my database fine and then I try the following mysql statements:

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

However, a few lines later when I try :

echo $answer1;

I'm given only nulls :(

Can anyone give me any suggestions please?

edit: SQL logins:

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

mysql_select_db("dbname") or die(mysql_error());
  • 写回答

3条回答 默认 最新

  • doure5236 2014-04-16 16:05
    关注

    everything you did is right you have just to fetch the data like this:

    $query1 = "select row1 from mydatabase where row2 = $Name ";
    $answer1 = mysql_query($query1);
    
    while($data= mysql_fetch_array($answer1)){
        echo $data['row1'];
    }
    

    And this is a complet answer, i adjust it as you need ;)

    <?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);
    ?>
    

    Good Luck :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog