Click here to Skip to main content
15,885,032 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends

I getting error in my php code

mysqli_query() expects parameter 1 to be mysqli, resource given in my php code

Quote:
Warning: mysqli_query() expects parameter 1 to be mysqli, resource given in C:\xampp\htdocs\Sites\freak\SignUp.php on line 16

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in C:\xampp\htdocs\Sites\freak\SignUp.php on line 17


my code is here

Quote:
if(isset($_POST["usernamecheck"])){
$db_conx = mysqli_connect('localhost','root');

$username = preg_replace('#[^a-z0-9]#i','',$_POST['usernamecheck']);
$sql = "SELECT id FROM users WHERE username = '$username'LIMIT 1";
$query = mysqli_query($db_conx,$sql);
$unamecheck = mysqli_num_rows($query);
if(strlen($username)< 3 || strlen($username) > 16)
{
echo'3-16 characters please';
exit();
}

if(is_numeric($username[0]))
{
echo' Usernames must begin with a letter';
exit();
}

if($unamecheck < 1) {

echo ''.$username. 'is OK';
exit();

}
else

{
echo''.$username.'is taken ';
exit();
}


}

?>
Posted

1 solution

Your connection attempt failed but you didn't check the mysqli_connect call result, as you should have done (see the documentation: "mysqli_query"[^] (see the code sample).
 
Share this answer
 
Comments
Md Jamaluddin Saiyed 9-Sep-13 5:36am    
Ok thanks for solution first error solved but second still comes
CPallini 9-Sep-13 5:38am    
What is the 'second error'?
Member 12663316 20-Mar-18 6:04am    
<?php
include 'connection.php';
$conn->set_charset('utf8');


?>
<?php

// php code to search data in mysql database and set it in input text

if(isset($_POST['search']))
{
// id to search
$id = $_POST['sino'];

// connect to mysql


// mysql search query
$query = "SELECT `office`, `promoteddate`, `fixeddarbandi` FROM `empjobdetails` WHERE `sino` = $sino LIMIT 1";

$result = mysqli_query($conn, $query);

// if id exist
// show data in inputs
if(mysqli_num_rows($result) > 0)
{
while ($row = mysqli_fetch_array($result))
{
$office = $row['office'];

$promoteddate = $row['promoteddate'];
} $fixeddarbandi = $row['fixeddarbandi'];
}

// if the id not exist
// show a message and clear inputs
else {
echo "Undifined ID";
$office = "";
$promoteddate = "";
$fixeddarbandi = "";
}


mysqli_free_result($result);
mysqli_close($connect);

}

// in the first time inputs are empty
else{
$office = "";
$promoteddate = "";
$fixeddarbandi = "";
}


?>

<!DOCTYPE html>





<title> PHP FIND DATA

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">







सि न:

कार्यलय:


बढुवा मिति :

कायम भएको दरबन्दी :










it shows the error like this


Warning: mysqli_query() expects parameter 1 to be mysqli, resource given in C:\xampp\htdocs\pmis\test2.php on line 20

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in C:\xampp\htdocs\pmis\test2.php on line 22
undefined id
Warning: mysqli_free_result() expects parameter 1 to be mysqli_result, null given in C:\xampp\htdocs\pmis\test2.php on line 37

Warning: mysqli_close() expects parameter 1 to be mysqli, resource given in C:\xampp\htdocs\pmis\test2.php on line 38

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900