doupi1532 2014-04-20 14:48
浏览 186
已采纳

PHP:连接到数据库,但mysql_query失败

I have very strange problem with PHP which I am starting to learn .. I have created tables in MySQL database with some data, and now I want to show them in webpage.

This is my source where I have this problem:

<?php
    // Here I open connection
    $con = mysql_connect("localhost","root","aaaaaa");
    // set the mysql database
    $db = mysql_select_db("infs", $con);

    // I check the connection
    if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    else {
        // It always goes here
        echo "Connected to database!";
    }

    // I am testing very simple SQL query.. there should be no problem
    $result = mysql_query("SELECT * FROM cathegories", $con, $db);

    if (!$result) {
        // but it always dies
        $message  = 'Invalid query: ' . mysql_error() . "
";
        $message .= 'Whole query: ' . $query;
        die($message);
    }

    mysql_close($con);
?>

What is wrong?

Thanks in advance!

  • 写回答

3条回答 默认 最新

  • dqj29136 2014-04-20 15:03
    关注

    You are mixing mysql and mysqli. Try something like:

    <?php   
        $con= new mysqli("localhost","user","passwd","database");
        if ($con->connect_errno){
          echo "could not connect";
        }
    
        $select = "SELECT * FROM tablename";
    
        if($result = $con->query($select)){
            while($row = $result->fetch_object()){
                echo $row->rowname."<br>";
            }
        }
        else { echo 'no result'; }
        $con->close();  
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(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