我对mysql数据库的请求无法正常工作

问题描述:

我是PHP + MySQL的新手,并且自己学习它。我遇到了一些问题(我想这可能非常简单和愚蠢),但我无法修复它。好。我想从我的数据库中选择一些记录:我使用该功能连接到数据库:我对mysql数据库的请求无法正常工作

function connect_bd() { 
    $result = new mysqli('localhost', 'root', '*****', 'book_kz'); 
    if (!$result) { 
     return false; 
    } else{ 
    $result->autocommit(TRUE); 
    return $result; 
} 
} 

它工作正常。然后我写验证功能:

function log_in($user_name, $passwrd) { 
// verification of user's name and password in database 
// if yes - return true 
// otherwise - false 

    // connect to database 
    $connect = connect_bd(); 
    if (!$connect) { 
    return 0; 
    } 

    // verification procedure 
    $result = $connect->query("select * from admin 
         where user_name='".$user_name."' 
         and passwrd = sha1('".$passwrd."')"); 

    if (!$result) { 
    echo "Incorrect password!".$connect->mysqli_errno; 
// return to logging in menu 
create_html_url("logo_in.php", "Return to logging menu"); 
return 0; 
    } 

    if ($result->num_rows>0) { 

    return 1; 
    } else { 

    return 0; 
    } 
$connect->close(); 
} 

它不起作用。特别是$ connect-> query有问题。 我把查询"select * from admin where user_name =...."直接进入MYSQL环境 - 它工作正常。但是$result = $connect->query("select...什么也没有显示。我插入了下一个命令echo "</br> print something</br>";echo "</br>print something".$result."</br>";。第一个命令显示我字符串print something,下一个显示什么都没有!这看起来像$结果块打印失败。我检查了一些关于mysqli :: query的示例,但没有发现任何错误。任何人愿意帮助我,我将非常感激。预先感谢您......

+0

'$ result'不能直接打印,它不是一个简单的价值。除了不正确地尝试*打印*之外,还有其他迹象表明这实际上是失败的吗? – David

+0

这不是打印问题。我无法打印,因为命令$ connect-> query无法正常工作。 –

+0

你有什么迹象表明它不能正常工作?根据你的问题,该指示是你试图打印查询结果。该测试是无效的,所以并不表示任何实际问题。如果您有其他*有关问题的有用信息,包括该信息将会很有帮助。 – David

当您调用一个类构造函数与new它基本上总是会返回一个类。所以这段代码是错误的:

$result = new mysqli('localhost', 'root', '*****', 'book_kz'); 
if (!$result) echo "Oh, there's an error!"; 

而是做:

$mysqli = new mysqli('localhost', 'root', '*****', 'book_kz'); 

// a connect_errno exists so the connection attempt failed! 
if ($mysqli->connect_errno) { 
    echo "Error: Failed to make a MySQL connection, here is why: \n"; 
    echo "Errno: " . $mysqli->connect_errno . "\n"; 
    echo "Error: " . $mysqli->connect_error . "\n"; 
    exit; 
} 
+0

不要直接向浏览器输出错误消息 –

+0

我没有与构造函数new(msqli)和连接号与打印没有问题。我遇到了执行命令的问题:$ result = $ connect-> query(“select * from admin ...它不返回数据库记录... –

+0

@George:我只是想在代码中指出我们的问题。显然这是问题,即使你不认为它是这样的,“你的常识”是对的,当然,你应该在内部处理真正的错误,并向访问者显示一条消息,如“出错了。 “ –