我不太确定我在这里做错了什么,我正在尝试检查行数以查看用户名是否存在。
我目前收到此错误:
Notice: Undefined property: mysqli::$num_rows in C:\xampp\htdocs\xampp\oop\ServerSide-Project-Message-Ninja\register.php on line 24
感谢任何帮助:)
register.php
<?php
require_once("config/config.php");
require_once("classes/database_class.php");
require_once("classes/html_class.php");
require_once("classes/security_class.php");
$objHTML = new makeHTML($register_form_array);
// $objHTML->viewArray();
echo $objHTML->makeHeader('Register for an account on Message Ninja', $keywords="Register, Signup", $description="");
if ($objHTML->checkData()) {
$data = $objHTML->getFilteredData();
$db = new database($dsn);
echo $data['username'];
$sql = "SELECT * FROM members WHERE username = '$data[username]'";
$res = $db->checkIfRecordExists($sql);
$row_count = $res->num_rows;
// if ( $row_count > 0) {
// echo "User already exists";
// } else {
// echo "This would normally insert data";
// // $sql = "INSERT INTO Members VALUES (NULL,'$data[username]', '$data[password]',
// // '$data[first_name]', '$data[last_name]', '$data[email]' )";
// // if ($db->insert($sql)) {
// // header("location: thankyou.php");
// // }
// }
}
?>
<div id="form_wrapper">
<?php
echo $objHTML->openForm();
echo $objHTML->makeRegisterFormRows();
echo $objHTML->makeSubmit('Register');
echo $objHTML->closeForm();
?>
</div>
<?php
echo $objHTML->makeFooter();
?>
database_class.php
<?php
class database{
private $objDbConnection;
function __construct($dsn) {
$this->objDbConnection = new mysqli($dsn['host'], $dsn['username'],
$dsn['password'], $dsn['database']);
if (mysqli_connect_errno()) {
die("Database connection failed". mysqli_connect_error());
}
}
function insert($sql) {
mysqli_query($this->objDbConnection, $sql);
if(mysqli_errno($this->objDbConnection)) {
die("Failed query: $sql". $this->objDbConnection->error);
}
$intInsertID = $this->objDbConnection->insert_id;
return $intInsertID;
}
function checkIfRecordExists($sql){
mysqli_query($this->objDbConnection, $sql);
if(mysqli_errno($this->objDbConnection)) {
die("Failed query: $sql". $this->objDbConnection->error);
}
$intInsertID = $this->objDbConnection;
return $intInsertID;
}
}
?>
答案 0 :(得分:1)
你班上没有这样的方法。 即使你有一个方法num_rows 你应该在$ db object
上调用它 $db->num_rows($sql) //you will need a query also as a parameter
行
$res = $db->checkIfRecordExists($sql);
只返回objDbConnection对象
答案 1 :(得分:0)
使用
$sql = "SELECT * FROM tbl_Freelancers WHERE User_Username = '".$username."'";
$result = mysqli_query($sql);
然后你需要使用另一个内置的PHP函数 -
$row_count = mysqli_num_rows($result);
答案 2 :(得分:0)
<?php
$con = mysql_connect("localhost", "peter", "abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db("test_db",$con);
$sql = "SELECT * FROM person";
$result = mysql_query($sql,$con);
echo mysql_num_rows($result);
mysql_close($con);
?>
答案 3 :(得分:0)
您需要在where子句
中更改您缺少squte' '
的查询
$sql = "SELECT * FROM members WHERE username = '".$data['username']."''";//in here