gpt4 book ai didi

php - num_rows 在面向对象的方法中不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 12:01:52 26 4
gpt4 key购买 nike

我是 php 和 mysql 的初学者。 num_rows 在下面的 basic 示例中不起作用。整个脚本的灵感来自 w3schools 中的示例。 w3schools example .

浏览器显示错误信息如下。

Notice: Trying to get property of non-object in C:\wamp\www\test3\index.php on line 17

代码

<?php
require 'connect.inc.php';
require 'core.inc.php';

//check username & password is set.

if(isset($_POST['username']) && isset($_POST['psw']))
{
$username = $_POST['username'];
$password = $_POST['psw'];
$pass_md5 = md5($password);
if(!empty($username) && !empty($password))
{
$queryy = "SELECT ID FROM user WHERE email= $username AND password= $pass_md5";
$result = $conn->query($queryy);

echo $result->num_rows; //<---------------NOT WORKING..! -----<<
}
else echo "incorrect username-password combination";
}
?>

<html>
<form action="<?php echo $current_file ?>" method="POST">
User name: <input type="text" name="username">
password: <input type="password" name="psw">
<input type="submit" value="Login"><br>
</form>
<html>

其中 connect.inc.php 有一些简单的代码来连接到本地主机和数据库。如下:

<?php
//this sript connects to DB->mytest.

$servername="localhost";
$username="root";
$password="";
$dbname="mytest";

//create connection
@$conn=new mysqli($servername, $username, $password, $dbname);

//check connection
if($conn->connect_error)
{
die("connection faild");
}
?>

并且,core.inc.php 返回当前文件位置。如下:

<?php
$current_file = $_SERVER['SCRIPT_NAME'];
?>

请帮忙..

最佳答案

问题是您没有在查询中引用字符串:

$queryy = "SELECT ID FROM user WHERE email= '$username' AND password= '$pass_md5'";

但是,最好使用准备好的查询和 bind_param 而不是替换变量。

$queryy = "SELECT ID FROM user where email = ? AND password = ?";
$stmt = $conn->prepare($queryy);
$stmt->bind_param("ss", $username, $pass_md5);
$stmt->execute();
$stmt->store_result();
echo $stmt->num_rows;

关于php - num_rows 在面向对象的方法中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28836045/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com