Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please help me, I am having trouble seeing if a value exists within my database

ID: 3586484 • Letter: P

Question

Please help me, I am having trouble seeing if a value exists within my database named I think the problem is rooting from mysql_result. How do I make this mysqli compatible ?

---------------------------------------------------------------

<?php

$connect_error = 'Error message';
global $con = mysqli_connect('localhost','root',' ');
mysqli_select_db($con,'loginandreg') or die($connect_error);


?>

-----------------------------------------------------------------

<?php

function sanitize($data)
{
   $username = sanitize($username);
   return mysqli_real_escape_string($data); //whatever data we insert, we will sanitize it by using this and return the data
  
}
?>

--------------------------------------

<?php

function user_exists($username)
{
   $query = mysqli_query("SELECT COUNT(`user_ID`) FROM `users` WHERE `username` = '$username'");
   return (mysql_result($query, 0)==1,) ? true : false;
  
  
}


?>

-------------------------------------------------------------------------------------------

<?php

//using this to check if there is a connection.to database

include 'core/init.php';

if(user_exists('george')===true) //geroge is the name of ther username within my database loginandreg
{
   echo 'exists';
}
die();

Explanation / Answer

<?php

$con = mysqli_connect("localhost","root","","user");

if (mysqli_connect_errno())

{

echo "Failed to connect to MySQL: " . mysqli_connect_error();

} else {

echo "Connected Successfully";

}

function sanitize($data) {

return mysql_real_escape_string($data);

}

function user_exists($username)

{

global $con;

$sql = "SELECT * FROM users WHERE username = '".$username."'";

$result = mysqli_query($con,$sql);

if(mysqli_num_rows($result)>=1)//You are mixing the mysql and mysqli, change this line of code

{

echo"name already exists";

}

}

?>

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote