This is a really simple thing, but it's not working for some reason. Heres my code.
I am making function (its part of a class) which checks if a username or email exists:
public function exists ($what, $who)
{
$sql = "SELECT * FROM users WHERE $what = $who";
$query = mysql_query($sql);
if (mysql_num_rows($query) != 0)
{
return true;
}
else
{
return false;
}
}
The function returns nothing. In fact if I run that query through regular PHP it returns nothing also. I don't understand why.
This following piece of code returns news entries perfectly:
function fetch($id = '')
{
if (empty($id))
{
$query = 'SELECT * FROM news ORDER BY id desc';
}
elseif (is_numeric($id))
{
$query = "SELECT * FROM news WHERE id = $id";
}
else
{
$route->to(SITE_URL);
}
$result = mysql_query($query);
if (mysql_num_rows($result) > 0)
{
return $result;
}
}
I am confused.
The problem is that you are missing quotes in your query:
$sql = "SELECT * FROM users WHERE $what = $who";
//SELECT * FROM users WHERE username = Mario is not a valid query
should be:
$sql = "SELECT * FROM users WHERE $what = '$who'";
the other queries are working because you are checking against an id, in this case against a string (and in this case you need quotes)
maybe the query execution failed and you have error turned off on screen in your php.ini
Try to add an intermediate check on the correct execution of the query:
$query = mysql_query($sql);
if ($query === FALSE) {
// log error with mysql_errno($conn) and mysql_error($conn);
} else {
if (mysql_num_rows($query) != 0) {
return true;
etc. etc.
Related
I am new with PHP and maybe it is a stupid question but when I use a function to check if a record exists in my database using the following:
function isGameExisted($con,$name)
{
$query = "SELECT * from Games WHERE NAME ='$name' LIMIT 1";
$check = mysqli_query($con,$query);
// var_dump($check);
if(mysqli_num_rows($check)){
//echo " EXISTS ";
return true;
}else
{
//echo " OFFER DOES NOT EXIST ";
return false;
}
}
So far so good it returns false when name does not exist
but in parent function
$isExisting = isGameExisted($con,$name);
if($isExisting)
{
$json['Result'] = "Fail";
$json['message'] = "Error in registering. Probably the name already exists";
}else
{
}
I am getting true! the message Error in Registering... what I am missing here
thank you
The problem is that isset($check) is always true. If the query finds a match row, $check will be set to an array. If the query doesn't find anything, $check will be set to NULL.
You should just do:
return $check != false;
The whole function should be:
function isGameExisted($con,$name)
{
$query = "SELECT 1 from Games WHERE NAME ='$name' LIMIT 1";
$check = mysqli_fetch_array(mysqli_query($con,$query));
return $check != null;
}
Please try with this
function isGameExisted($con,$name)
{
$query = "SELECT * from Games WHERE NAME ='$name' LIMIT 1";
$excQuery = mysqli_query($con,$query);
if(mysqli_num_rows($excQuery)){
//echo " EXISTS ";
return true;
}else
{
//echo " GAME DOES NOT EXIST ";
return false;
}
}
I was getting the fatal error while trying to run mysql delete transaction so i searched for similar issues and it became obvious using mysqli_fetch_assoc is the way to fix this. Tried a couple of times but am obviously doing something wrong, would appreciate any help.
Here's my code
<?php
$selected_admin = find_admin_by_id($_GET["admin"]);
if (!$selected_admin) {
redirect_to("sadmin.php");
}
while ($admin = mysqli_fetch_assoc($selected_admin)) {
$admin_id = $selected_admin["admin_id"];
}
$sql = "delete from admin where admin_id = {$admin_id} limit 1";
$result = mysqli_query($connection, $sql);
if ($result && mysqli_affected_rows($connection) == 1) {
$SESSION["message"] = "Admin successfully deleted.";
redirect_to("sadmin.php");
} else {
$SESSION["message"] = "Admin was not deleted successfully.";
redirect_to("sadmin.php?admin={$admin_id}");
}
?>
And the function am calling is this
function find_admin_by_id($admin_id) {
global $connection;
$query = "select * from admin where admin_id = {$admin_id} LIMIT 1";
$current_admin = mysqli_query($connection, $query);
confirm_query($current_admin);
return $current_admin;
}
You are using wrong variable in while loop and my other mate also mentioned one hour ago, here is the correct code with minor changes:
<?php
$selected_admin = find_admin_by_id($_GET["admin"]);
if (!$selected_admin) { redirect_to("sadmin.php");
}
while ($admin = mysqli_fetch_assoc($selected_admin))
{
$admin_id = $admin["admin_id"];
}
if( intval($admin_id) > 0 ){
$sql = "delete from admin where admin_id = {$admin_id} limit 1";
$result = mysqli_query($connection, $sql);
if ($result && mysqli_affected_rows($connection) == 1)
{
$SESSION["message"] = "Admin successfully deleted.";
redirect_to("sadmin.php");
}
else {
$SESSION["message"] = "Admin was not deleted successfully."; redirect_to("sadmin.php?admin={$admin_id}");
}
}
?>
What I change:
Change $selected_admin as $admin.
And add if condition before executing DELETE STATEMENT.
I am working on Basketball Referee project. I am trying to update table checking input value.
I am sending the value from my control.php like this:
$check_user = $this->input->post("headreferee");
$check = $this->sql_model->check1($check_user);
Here is my sql_model.php:
function check1($referee_name)
{
$sql = "SELECT * FROM duties WHERE username ='{$referee_name}' ";
$query = $this->db->query($sql);
if($query->num_rows()>0)
{
$this->db->set('count','count'+1);
$this->db->insert('duties');
}
else
{
return 0;
}
}
Actually it is increasing count and adding new row but without any referee_name. It has to find the correct referee_name and increase that row's count.
If you want to update so please modify your check1() function by below script.
function check1($referee_name)
{
$sql = "SELECT * FROM duties WHERE username ='{$referee_name}' ";
$query = $this->db->query($sql);
$result_referee = $query->row();
$countNew = $result_referee->count + 1;
if($query->num_rows()>0)
{
$countArray = array('count'=>$countNew);
$this->db->where('username',$referee_name);
$this->db->update('duties',$countArray);
}
else
{
return 0;
}
}
I hope that will be helpful for you as your solution.
In fact I am working on a small PHP script but I am facing a problem right now.The problem is that i want to check if my query return records this is my mysqli query:
$sql = "select * from specs where btitleid=$id and phoneid=$aydi"
$check = $conn->query($sql)
while($row = $check->fetch_assoc()) {$tocheck = $row['content'];}
I don't want to check the number of rows of this query to see if it is null.I want to check if all $row['content'] are empty.
How about this:
$sql = "select * from specs where btitleid=$id and phoneid=$aydi";
$check = $conn->query($sql);
$contentAllEmpty = true;
while ($row = $check->fetch_assoc()) {
if (strlen($row['content']) > 0) {
$contentAllEmpty = false;
}
}
if ($contentAllEmpty) {
//do something
}
use ==
while ($row = $check->fetch_assoc()) {
if ($row['content'] == '') {
... code here
}
}
To get back only records where the content column is not empty -
$sql = "SELECT * FROM `specs` WHERE `btitleid` = $id AND `phoneid` = $aydi AND (`content` IS NOT NULL OR `content` != '') ";
I am trying to rewrite the following function in different way but it seems that I missed something and I do not get the intended functionality?
Could anyone suggest anything?
Thanks in advance.
Function 1
function user_exists($email) {
$email=mysql_real_escape_string($email);
$query=mysql_query("SELECT COUNT (`user_id`) FROM `users` WHERE `email` = '$email'");
return(mysql_result($query, 0) == 1) ? true : false;
}
Rewritten Function 1
function user_exists($email) {
$email=mysql_real_escape_string($email);
$query=mysql_query("SELECT user_id FROM users WHERE email='$email'");
$count=mysql_num_rows($query);
if ($query !=0) {
return(true);
} else{
return(false)
}
function user_exists($email)
{
$email = mysql_real_escape_string($email);
$query = mysql_query("SELECT user_id FROM users WHERE email='$email'");
if (mysql_num_rows($query) > 0)
{
return true;
}
else
{
return false;
}
}
function user_exists($email) {
// Escape to prevent sql injection
$email = mysql_real_escape_string($email);
// Query to see if the email exists in the DB
if(false === ($query = mysql_query("SELECT user_id FROM users WHERE email='$email'"))){
// handle error
$result = false;
}
else{
// Find a row? Email exists, otherwise does not
$result = (mysql_num_rows($query) > 0);
}
return $result;
}
The DB query itself could be more efficient (select a count, or at least limit to 1 result max), but this is the general idea.
function user_exists($email) {
$email=mysql_real_escape_string($email);
$query=mysql_query("SELECT user_id FROM users WHERE email='$email' LIMIT 1");
return mysql_num_rows($query) == 1;
}
LIMIT 1 is a good practice since the engine stop searching on the first match and does not continue for the next rows
<?php
function user_exists($email)
{
// Perform database query
$email = mysql_query("SELECT user_id FROM users WHERE email='$email'");
if (!$email) {
die("Database query failed: " . mysql_error());
}
// Use returned data
while ($row = mysql_real_escape_string($email)) {
if ($row !=0) {
return(true);
}
}
}
?>