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

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!

图片转代码服务由CSDN问答提供 功能建议

我对PHP有一个非常奇怪的问题,我开始学习..我在MySQL数据库中创建了一些表 数据,现在我想在网页上显示它们。

这是我遇到此问题的来源:

 &lt;?  php 
 //这里我打开连接
 $ con = mysql_connect(“localhost”,“root”,“aaaaaa”); 
 //设置mysql数据库
 $ db = mysql_select_db(“infs”,$ con  ); 
 
 //我检查连接
 if(mysqli_connect_errno())
 {
 echo“无法连接到MySQL:”。  mysqli_connect_error(); 
} 
 else {
 //它总是在这里
 echo“连接到数据库!”; 
} 
 
 //我正在测试非常简单的SQL查询..应该有 没问题
 $ result = mysql_query(“SELECT * FROM cathegories”,$ con,$ db); 
 
 if if(!$ result){
 //但它总是死掉
 $ message ='无效的查询 :'。  mysql_error()。  “
”; 
 $ message。='整个查询:'。  $ query; 
 die($ message); 
} 
 
 mysql_close($ con); 
?&gt; 
   
 
 

有什么问题?

提前致谢!

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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();  
    ?>
    
    已采纳该答案
    打赏 评论
  • douqin0676 2014-04-20 14:51

    mysql_query only takes two parameters - the actual SQL and then the link identifier (I assume in your case that's stored in $con; therefore remove $db from the third parameter).

    You don't even need the second $con parameter really.

    Where's the actual logic to connect to the database initially? Just because mysqli_connect_errno() doesn't return an error it doesn't mean the connection actually exists and that $con is available in the current scope.

    I'd var_dump($con) before the mysql query to make sure it's a valid connection.

    打赏 评论
  • drsfgwuw61488 2014-04-20 14:52
    // Here I open connection
    $con = mysql_connect("localhost","root","aaaaaa");
    // set the mysql database
    $db = mysql_select_db("infs", $connection);
    

    change to

    // Here I open connection
    $con = mysql_connect("localhost","root","aaaaaa");
    // set the mysql database
    $db = mysql_select_db("infs", $con);
    
    打赏 评论

相关推荐 更多相似问题