trying to check if user exists with php - php

hi i am trying to make a php script that checks if a user is in the database only it gives me the error
mysqli_num_rows() expects parameter 1 to be mysqli_result, bool given in C:\xampp\htdocs\stage\userExist.php on line 22
below is the code
<?php
$link = mysqli_connect('localhost', 'root', '', 'k3462_top-tree');
$query = "SELECT * FROM users";
$userName = $_GET['userName'];
$result = mysqli_query($link, $query);
echo "<table>";
while($row = mysqli_fetch_array($result)){
echo "<tr><td>" . $row['userName'] . "</td><td>" . $row['email'] . "</td></tr>";
}
echo "</table>";
$query1 = "SELECT * FROM users WHERE userName = $userName";
$result1 = mysqli_query($link, $query1);
if(mysqli_num_rows($result1)>=1){
echo "user exists";
}
else {
echo "user doesnt exist";
}
mysqli_close($link);
?>

You have a syntax error in your query that fill the variable $result1 with false. That occurs your error. Try my example below. I just added ' around $userName in your query and an if statement around your mysqli_num_rows function.
$link = mysqli_connect('localhost', 'root', '', 'k3462_top-tree');
$query = "SELECT * FROM users";
$userName = $_GET['userName'];
$result = mysqli_query($link, $query);
echo "<table>";
while($row = mysqli_fetch_array($result)){
echo "<tr><td>" . $row['userName'] . "</td><td>" . $row['email'] . "</td></tr>";
}
echo "</table>";
$query1 = "SELECT * FROM users WHERE userName = '$userName'";
$result1 = mysqli_query($link, $query1);
if( $result1 ) {
if(mysqli_num_rows($result1)>=1){
echo "user exists";
}
else {
echo "user doesnt exist";
}
} else {
echo "query or db error";
}
mysqli_close($link);

$query1 = "SELECT * FROM users WHERE userName = '$userName'";

Related

Button is not disabled even one of the field bool value is TRUE

I have this php code where it checks if the client_number or client_email present in the database. If present it'll disable the submit button. But when one of the field data is not present in database it enables the button which it shouldn't.
function checkDataExistence($connection){
if(!empty($_POST["clientEmailID"])) {
$query = "SELECT * FROM clients_table WHERE client_email='" . $_POST["clientEmailID"] . "'";
$result = mysqli_query($connection, $query);
$count = mysqli_num_rows($result);
if($count>0) {
echo "<span style='color:red'> This Email is already registered!.</span>";
echo "<script>$('#submit').prop('disabled',true);</script>";
} else{
echo "<script>$('#submit').prop('disabled',false);</script>";
}
}
if(!empty($_POST["clientPhoneNumber"])) {
$query = "SELECT * FROM clients_table WHERE client_phone_number='" . $_POST["clientPhoneNumber"] . "'";
$result = mysqli_query($connection, $query);
$count = mysqli_num_rows($result);
if($count>0) {
echo "<span style='color:red'> This Phone number is already registered.</span>";
echo "<script>$('#submit').prop('disabled',true);</script>";
}else{
echo "<script>$('#submit').prop('disabled',false);</script>";
}
}
}
How to make it work for both input fields?

PHP Check if string is in table

So i've been trying to sort this issue out for a while but been unable to figure out if i have to do this trough SQL side or the PHP side.
My code i have so far:
<?php
$servername="";
$username = "";
$password = "";
$dbname = "alphacre_kingsland";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM `bm_player_bans` ORDER BY id";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result)>0){
while($row = mysqli_fetch_assoc($result)){
$playerId = bin2hex($row['player_id']);
$storedname= getPlayerName($playerId);
$sql = "INSERT INTO bm_onlinedata (uuid, playername) VALUES ('".$playerId."', '".$storedname."')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
}else{
echo "0 results";
}
mysqli_close($conn);
function getPlayerName($uuid){
$json_response = file_get_contents("https://sessionserver.mojang.com/session/minecraft/profile/".$uuid);
$data = json_decode($json_response);
return $data->name;
}
?>
So this stores "UUID" and playername into a seperate table. But i want to check if the UUID is already in there before it adds a new one. Basicly updating it.
This is because playernames can change. And i don't want multiple rows of the same data.
But thats ONLY if its there ofc.
Background information:
I'm fetching the username from a user trough Mojangs API, wich only allows a single lookup of that UUID every minute. So i want to get the data every 5 minutes or so using a scheduled task running this script and store it in a table.
You can easily check with select
$sql = "SELECT * FROM `bm_player_bans` ORDER BY id";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result)>0){
while($row = mysqli_fetch_assoc($result)){
$playerId = bin2hex($row['player_id']);
$storedname= getPlayerName($playerId);
$sql = "SELECT * FROM `bm_onlinedata` where uuid = '" . $playerId ."';";
$result = mysqli_query($link, $sql);
$num_rows = mysqli_num_rows($result);
if ($num_rows> 0) {
$sql = "UPDATE bm_onlinedata
set playername = '". $storedname."'
where uuid = '" .$playerId ."';"; )";
} else {
$sql = "INSERT INTO bm_onlinedata (uuid, playername) VALUES ('".$playerId."', '".$storedname."')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
}
}else{
echo "0 results";
}

learning how to read data from MYSQL

I'm trying to learn php and mysql, I was trying to read data from my database where i was following something online and encounter an error(s) here is my code
<?php
include 'includes/conn.php';
$query =sprintf("Select * from customer");
$result = mysql_query($query);
if (!$result)
{
$message ='from you see this then it's not connecting!'.mysql_error() ."\n";
$message .= 'everything' .$query;
die($message);
}
while ($customer = mysql_fetch_assoc($result))
{
echo $row['cust_id'];
echo $row['fname'];
echo $row['lname'];
echo $row['gender'];
echo $row['dob'];
}
mysql_free_result($result);
?>
//this is where i was trying to make a connection with the database
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$db ='telmar_php';
$con = mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($db);
?>
Replace
$message ='from you see this then it's not connecting!'.mysql_error() ."\n";
here your string is single quotation marks in it's is causing the problem
with
$message ="from you see this then it's not connecting!".mysql_error() ."\n";
...and to display your results change this:
while ($customer = mysql_fetch_assoc($result))
to this:
while ($row = mysql_fetch_assoc($result))
Please review the below example for reading data my mysql using php.
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}

If 0 items in DB table, return error

I am looking for a way to display an error message if there is nothing listed in the table.
I have a photos table.
If this tables is empty, id like to echo something.
else, show the pictures.
inside of that table I have
id, name, url
id = id
name = name of image
url = url of image.
If there are no rows, we have an error.
$query1 = mysql_query("SELECT COUNT(*) FROM photos;");
mysql_fetch_array($query1);
if(empty($query1)) {
echo "nothing";
} else {
echo "good";
}
Try this,
$query = "SELECT * FROM photos";
$result= mysql_query($query);
$length= mysql_num_rows($result);
if($length>0)
{
while($rows = mysql_fetch_array($result))
{
echo $rows['name'];
echo "<img src='$rows[url]' />";
}
}
else
{
echo "Nothing to display";
}
Hope this will work
What about something like...
$sql = "SELECT COUNT(*) AS amountPhotos FROM photos";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
if ($row["amountPhotos"] == 0) {
echo "There are no photos in the photo table.";
}
or
$sql = "SELECT * FROM photos LIMIT 1";
$result = mysql_query($sql);
if (mysql_num_rows($result) == 0) {
echo "There are no photos in the photo table.";
}
Try this
$query1 = mysql_query("SELECT COUNT(*) FROM photos;");
$result = mysql_fetch_array($query1);
if(empty($result)) {
echo "nothing";
} else {
echo "good";
}
This pretty much sums up the answer for this question: http://www.w3schools.com/php/php_mysql_select.asp
They even provided a sample code:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);
if ($result->num_rows > 0) { //<--- here they check if number of rows returned is greater than 0 (so there is data to display)
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results"; //<----- nothing found
}
$conn->close();
?>
Just modify this and you'll be good to go.

mysql can not compare data and parse it with php

i'm getting back to make some php code to make a login/register form.
Account data is stored in mysql database and I've used that code to validate the md5 and parse it trough the browser. But there's something wrong 'cause it doesn't work.
<?php
if (!$x) {
echo "<script type='text/javascript'>document.location.href='index.php';</script>";
} else {
$con = mysql_connect('localhost', 'userdb', 'pwd') or die(mysql_error());
$db = mysql_select_db('dbname', $con) or die(mysql_error());
$result = mysql_query("SELECT * FROM `users` WHERE `md5pwd` = '". $x ."'");
$num = $result1->num_rows;
if($num == 0){
//if not display an error message
echo "<script> alert('Usuario inexistente') </script>";
echo "<script type='text/javascript'>document.location.href='index.php';</script>";
}else{
while($row=mysql_fetch_object($result1)){
$userName=$row->username;
echo "<script> alert('BIENVENIDO " . $userName . "') </script>";
echo "[ " . $userName . " ]\n\n";
}
}
}
?>
I hope you can understand and I hope you can help me. I'm unhappy.
thanks
Hope it helps you, $num = mysql_num_rows($result); instead of $num = $result1->num_rows;
mysql_fetch_object($result) instead of mysql_fetch_object($result1)
<?php
if (!$x) {
echo "<script type='text/javascript'>document.location.href='index.php';</script>";
} else {
$con = mysql_connect('localhost', 'userdb', 'pwd') or die(mysql_error());
$db = mysql_select_db('dbname', $con) or die(mysql_error());
$result = mysql_query("SELECT * FROM `users` WHERE `md5pwd` = '". $x ."'");
$num = mysql_num_rows($result);
if($num == 0){
//if not display an error message
echo "<script> alert('Usuario inexistente');document.location.href='index.php';</script>";
}else{
while($row=mysql_fetch_object($result)){
$userName=$row->username;
echo "<script> alert('BIENVENIDO " . $userName . "') </script>";
echo "[ " . $userName . " ]\n\n";
}
}
}
?>
Note: mysql_* functions deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used.
Working code:
<?php
if (!isset($x)) {
echo "<script type='text/javascript'>document.location.href='index.php';</script>";
} else {
$con = mysql_connect('localhost', 'userdb', 'pwd') or die(mysql_error());
$db = mysql_select_db('dbname', $con) or die(mysql_error());
$result = mysql_query("SELECT * FROM `users` WHERE `md5pwd` = '". $x ."'");
$num = mysql_num_rows($result);
if($num == 0){
//if not display an error message
echo "<script> alert('Usuario inexistente') </script>";
echo "<script type='text/javascript'>document.location.href='index.php';</script>";
}else{
while($row=mysql_fetch_object($result)){
$userName=$row->username;
echo "<script> alert('BIENVENIDO " . $userName . "') </script>";
echo "[ " . $userName . " ]\n\n";
}
}
}
?>
PS: mysql_extension will be removed in the future, use mysqli or PDO...btw mysqli in procedural mode it's very similar to mysql extension.

Categories