hello I am making a login/register system for my website
so far for the register php code i have:

<?php
include ("config.php");
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


$username = $_POST[username];
$password = $_POST[password];
$email = $_POST[email];
$conf_email = $_POST[confirm_email];

$query = "INSERT INTO site_users_info (username, password, email, confirm_email) VALUES('$username', '$password', '$email', '$conf_email')";
$stmt = $mysqli->prepare($query);
if(!$stmt){
    echo "Problem Preparing Query!";
}
$stmt->execute();

$validatequery = "SELECT * FROM site_users_info ";
$result = mysql_query($validatequery)  or die(mysql_error());;

$num_rows = mysql_num_rows($result);



?>

and here is the config file:

<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

$mysqli = new mysqli("localhost", "root", "", "site_users");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
} 
?>

the problem is I want to create an error for someone who is registering with the same username, however i have tried using the mysql_num_rows() function and i used a mysql_error to see if there was an error with the sql, it said that no database had been selected, what is causing this problem and how can i fix it?

Dani AI

Generated

The error comes from mixing two different APIs. The config file creates a mysqli connection ($mysqli = new mysqli(...)) but the register code then calls mysql_query() / mysql_num_rows() (the old mysql extension). Those functions use a different connection mechanism, so there is no active mysql link and you get “no database selected.” Also avoid unquoted array keys like $_POST[username] — use $_POST['username'] and validate inputs.

Use one API consistently (mysqli or PDO). Below is a concise mysqli (OOP) pattern that checks for an existing username, uses prepared statements, and stores a hashed password. It shows the structure you should follow rather than the mixed approach in the original post.

<?php
// assume $mysqli from config.php
$username = trim($_POST['username'] ?? '');
$password = $_POST['password'] ?? '';
$email = trim($_POST['email'] ?? '');

$mysqli->set_charset('utf8mb4');

if ($stmt = $mysqli->prepare('SELECT id FROM site_users_info WHERE username = ? LIMIT 1')) {
    $stmt->bind_param('s', $username);
    $stmt->execute();
    $stmt->store_result();
    if ($stmt->num_rows > 0) {
        // username exists
        $stmt->close();
    } else {
        $stmt->close();
        $hash = password_hash($password, PASSWORD_DEFAULT);
        if ($ins = $mysqli->prepare('INSERT INTO site_users_info (username, password, email) VALUES (?, ?, ?)')) {
            $ins->bind_param('sss', $username, $hash, $email);
            $ins->execute();
            $ins->close();
        } else {
            // handle prepare error ($mysqli->error)
        }
    }
} else {
    // handle prepare error ($mysqli->error)
}
?>

Extra tips: add a UNIQUE index on username so the database enforces uniqueness (and handle duplicate-key errors for race conditions). Do not store plain text passwords — use password_hash() / password_verify(). For debugging enable mysqli error reporting or inspect $mysqli->error and $mysqli->errno. This addresses the root cause in ’s code and is a safer alternative to the older suggestions from and .

you have to select the data-base first to act on any of the tables in that.Then u can use the mysql_select_db() for that.

You didn't used the command to connect to database just use below syntax to connect to db
<?php
$username = "your_name";
$password = "your_password";
$hostname = "localhost";

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
?>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.