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 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 UE5#if WITH_EDITOR导致打包的功能不可用
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面
  • ¥15 算法题:数的划分,用记忆化DFS做WA求调