Я просто написал скрипт для простой страницы регистрации, и когда я пытаюсь использовать его, я получаю
Fatal error: Call to a member function query() on a non-object on line 60
Строка 60
$result = $mysqli->query("SELECT username from users WHERE username = '{$username}' LIMIT 1");
Вот полный сценарий:
<html>
<head>
<title> Register </title>
</head>
<center>
<font color="green">
<body background=background.png no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
>
<h1> Register</h1>
<?php
require_once("db_const.php");
if (!isset($_POST['submit'])) {
?><form action=" <?=$_SERVER['PHP_SELF']?>" method="post">
Username: <input type="text" name="username" /><br />
Password: <input type="password" name="password" /><br />
Age: <select name="Age">
<option value="12-13"> 12-13 </option>
<option value="14-15"> 14-15 </option>
<option value="16-17"> 16-17 </option>
<option value="18+"> 18+ </option>
</select>
<br />
Gender: <select name="Gender">
<option value="male"> male </option>
<option value="female"> female </option>
</select>
<br /><input type="submit" name="submit" value="register" />
</form>
</font>
</center>
<?php
} else {
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_AGE, DB_Gender);
if ($mysqli->connect_errno) {
echo "<p>MySQL error no {$mysqli->connect_errno} : {mysqli->connect_error}</p>";
exit();
}
}
# prepare data for insertion
$username = $_POST['username'];
$password =$_POST['password'];
$age =$_POST['Age'];
$gender =$_POST['Gender'];
$exists = 0;
$result = $mysqli->query("SELECT username from users WHERE username = '{$username}' LIMIT 1");
if ($result->num_rows == 1) {
$exists = 1;
}
if ($exists == 1) echo "<p> Username already exists!</p>";
else {
$sql = "INSERT INTO 'users' ('id', 'username', 'password', 'age', 'gender')
VALUES (NULL, '{$username}', '{$password}', '{$age}'. '{$gender}')";
if ($mysql->query($sql)) {
//echo "New Record has id" .$mysql->insert_id;
echo "<p>Registered successfully!</p>";
} else{
echo "MySQL error no {mysql->errno} : {$mysql->error}</p>";
exit();
}
}?>
</body>
</html>
Прошу прощения за мою нелюбовь. Заранее спасибо!
Заменить ниже строки
if ($mysql->query($sql)) {
С
if ($mysqli->query($sql)) {
Других решений пока нет …