mysql查询不起作用,但没有错误

时间:2013-12-11 07:32:50

标签: php mysql database

这是我的代码

    <?php
    $con=mysqli_connect("localhost","boursdfdswmat","PfdXfdsfkd");
    mysqli_select_db("dbanme",$con);
    // Check connection
    if (mysqli_connect_errno())
    {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
     }
    else {echo "connected";}

    $qry=mysqli_query("SELECT `title`, `created_date`, `user_id` FROM    `invoices` WHERE `user_id` = 123 ORDER BY created_date DESC LIMIT 0 , 1");
    echo $res1=mysqli_num_rows($qry);
    $result=mysqli_fetch_array($qry);
    echo $result['0'];
    ///echo "hi";
    ?>

这些代码有什么问题。我对这些查询没有任何结果。请帮我。感谢

4 个答案:

答案 0 :(得分:2)

代码中出现两个错误:无效使用 mysqli_select_db 并无效使用 mysqli_query for * mysqli_select_db *:参数错误,* mysqli_query *:错过连接。

将您的代码更改为:

 <?php
    $con=mysqli_connect("localhost","boursdfdswmat","PfdXfdsfkd");
    mysqli_select_db($con,"ghese_new");
    // Check connection
    if (mysqli_connect_errno())
    {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
     }
    else {echo "connected";}

    $qry=mysqli_query($con,"SELECT `title`, `created_date`, `user_id` FROM    `invoices` WHERE `user_id` = 123 ORDER BY created_date DESC LIMIT 0 , 1");
    echo $res1=mysqli_num_rows($qry);
    $result=mysqli_fetch_array($qry);
    echo $result['1'];
    ///echo "hi";
    ?>

答案 1 :(得分:1)

您要在此处执行的基本调试方法是确定查询字符串是否有错误,或者您是否实际连接到数据库。尝试在代码上添加mysql_error(),例如

$con=mysqli_connect("localhost","bouwmat","PXk(0")or die(mysql_error());
mysqli_select_db("bouwmat_jos1",$con) or die(mysql_error());

$qry=mysqli_query("SELECT `title`, `created_date`, `user_id` FROM    `jos_listbingo_cart_cart_invoices` WHERE `user_id` = 896 ORDER BY created_date DESC LIMIT 0 , 1")or die(mysql_error());

答案 2 :(得分:0)

看起来您已经使用旧的已弃用的mysql_ *函数并将其替换为mysqli,但这并不总是有效。
大多数程序mysqli调用都需要$ link param(你的连接),如:

 mysqli_query($connection, $query);

或:      mysqli_error($连接);

我认为没有mysqli_connect_errno()功能,我至少在文档中找不到一个(但我可能错了,请纠正我,如果是的话)。
有一个$mysqli->connect_errno变量,但应该在$con变量上调用它。但是您使用的是mysqli的过程版本,因此您可能只想使用mysqli_error($con);调用。

您还可以在mysqli_select_db调用中选择数据库。这不是真的错,但建议在连接调用中选择数据库:

 $con=mysqli_connect("localhost","boursdfdswmat","PfdXfdsfkd", "dbname");

查看文档:{​​{3}}了解详情。

答案 3 :(得分:0)

<?php
    $con=mysqli_connect("localhost","boursdfdswmat","PfdXfdsfkd","dbanme");
    // Check connection
    if (mysqli_connect_errno())
    {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
     }
    else {echo "connected";}
    $qry=mysqli_query($con,"SELECT `title`, `created_date`, `user_id` FROM    `invoices` WHERE `user_id` = 123 ORDER BY created_date DESC LIMIT 0 , 1");
    echo $res1=mysqli_num_rows($qry);
    $result=mysqli_fetch_array($qry);
    echo $result['0'];
 ?>

试试这段代码,mysqli_connect直接接受数据库并查询需要连接对象