I called my update sql every time run the php file and it return true statement but record cannot update perfectly. I want to know that where my code goes wrong? Please help me and I will appreciate it. Thanks in advance.
This is my php code in event-listing.php:
$update_event_list = $event->updateeventlist($type = 1);
This is my sql statement in Event.inc.php :
function updateeventlist($type){
global $db;
$stmt = "SELECT * FROM "._CONST_TBL_EVENT." WHERE type = ".$type;
if($rs = $db->Execute($stmt)){
while($rsa = $rs->FetchRow())
{
if($rsa['start_date'] < strtotime("now")){
$updateEvent = "UPDATE "._CONST_TBL_EVENT." SET type = 2 WHERE id = ".$rsa['id'];
}
}
}
return true;
}
I have tried to echo out the statement and it return true statement that I want.
You need to add execute function after the update query.
$rs = $db->Execute($updateEvent);
Query execution missing after your update Query
function updateeventlist($type){
global $db;
$stmt = "SELECT * FROM "._CONST_TBL_EVENT." WHERE type = ".$type;
if($rs = $db->Execute($stmt)){
while($rsa = $rs->FetchRow())
{
if($rsa['start_date'] < strtotime("now")){
$updateEvent = "UPDATE "._CONST_TBL_EVENT." SET type = 2 WHERE id = ".$rsa['id'];
$db->Execute($updateEvent);
}
}
}
return true;
}
Below are some points that i observed in your code:-
You are not executing the update query. You are just making the query as string but not executing.
Even if you none of the records is updated or fetched you still get "true", because there is no condition to specify when to return false if it fails.
$stmt = "SELECT * FROM "._CONST_TBL_EVENT." WHERE type = ".$type;
if($rs = $db->Execute($stmt))
{
if( $rs has atleast one row rows )
{
while($rsa = $rs->FetchRow())
{
if($rsa['start_date'] < strtotime("now")){
$updateEvent = "UPDATE "._CONST_TBL_EVENT." SET type = 2 WHERE id = ".$rsa['id'];
$db->Execute($updateEvent); // this line was missing in you code
}
}
}
else
{
return false;
// $rsa has empty rows
}
}
else // execution of query fails for any reason
{
return false;
}
Related
Trying to find if row exists in database. This code always returns TRUE. I want to add the row if it doesn't exist.
See test code below.
// CHECK IF RECORD EXISTS
if (array_key_exists('test', $_GET)) {
if(coupleRowExists($_GET['male'], $_GET['female'])) {
echo 'EXISTS';
} else {
echo 'Does NOT exist';
}
}
// CHECK DB FOR THIS COUPLE
function coupleRowExists($male, $female) {
global $db;
$sp_couple_exists = "SELECT EXISTS(SELECT 1 FROM `couples`
WHERE male = ? AND female = ? )";
return ($db->prepare($sp_couple_exists)->execute([$male, $female]));
}
This code:
return ($db->prepare($sp_couple_exists)->execute([$male, $female]));
Will return true when the database query is executed successfully, not necessarily if there is a result or not. Which, if you wrote the query properly, will be all the time.
If you want to find out if it actually returned a row, then you'll want to check for an actual returned row.
I would modify this a bit so it returns that and is a bit cleaner:
function coupleRowExists($male, $female) {
global $db;
$sql = "SELECT EXISTS(SELECT 1 FROM couples WHERE male = ? AND female = ?)";
$db->prepare ($sql);
$db->execute ([$male, $female]);
if ($db->rowCount()) {
return true;
} else {
return false;
}
}
execute() will return only true or false. Here's a link for a reference.
https://www.php.net/manual/en/pdostatement.execute.php
Here's a modified function for the same.
function coupleRowExists($male, $female) {
global $db;
$sql = "SELECT EXISTS(SELECT 1 FROM couples WHERE male = ? AND female = ?)";
$db->prepare ($sql);
$result = $db->execute ([$male, $female]);
if ($result && $db->rowCount() > 0) {
return true;
} else {
return false;
}
}
I agree with user3783243 in that rowCount does not always work with SELECT (see PHP Manual->PDOStatement::rowCount(). They recommend use of COUNT as follows:
function coupleRowExists($m, $f) {
global $db;
$sp = "SELECT COUNT(*) FROM `couples` WHERE male = ? AND female = ?";
$stmt = $db->prepare($sp);
$stmt->execute([$m, $f]);
$count = $stmt->fetchColumn();
return $count;
}
This has proven reliable.
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.
I have a function query:
function query() {
global $link;
$debug = false;
//get the sql query
$args = func_get_args();
$sql = array_shift($args);
//secure the input
for ($i=0;$i<count($args);$i++) {
$args[$i] = urldecode($args[$i]);
$args[$i] = mysqli_real_escape_string($link, $args[$i]);
}
//build the final query
$sql = vsprintf($sql, $args);
if ($debug) print $sql;
//execute and fetch the results
$result = mysqli_query($link, $sql);
if (mysqli_errno($link)==0 && $result) {
$rows = array();
if ($result!==true)
while ($d = mysqli_fetch_assoc($result)) {
array_push($rows,$d);
}
//return json
return array('result'=>$rows);
} else {
//error
return array('error'=>'Database error');
}
}
The function works just fine when I use it like this:
$g = "05%";
$result = query("SELECT * FROM table_name WHERE table_column LIKE '%s'", $g);
print json_encode($result);
However I am getting no result when $g is a value retrieved from a method. For example lets say I have a method getMonth() from a class Date that returns the current month of May as 05% when echoed. I try the code below and get nothing from the database:
$time = new Date();
//$g = "05%"; this would definitely get results from the db
$h = $time->getMonth();
echo $h; //this displays 05% on the screen
$result = query("SELECT * FROM table_name WHERE table_column LIKE '%s'", $h);
print json_encode($result);
I am pretty sure that I am making a simple mistake, but I can't seem to figure it out. How can I fix this? Any help would be greatly appreciated.
Do something that your method return only 05 part from 05%. and append % after that for example
$h = $time->getMonth();
$h = "$h%";
and then it should work
I am tying to update table status after success run the if condition and my condition is work well. I try to echo something inside if condition and its work. But update sql cannot function after I run through all the code. I have no idea where goes wrong of my code. I had try several time to echo different value inside if condition and there is no problem to print out all the things.
This is php code:
<?
require_once "lib/base.inc.php";
$arrResult = $oAdminEmail->getQueEmail();
for($i=0; $i<count($arrResult); $i++)
{
$email = $arrResult[$i]['contact_email'];
$name = $arrResult[$i]['contact_first']." ".$arrResult[$i]['contact_last'];
$message = $arrResult[$i]['message'];
$subject = $arrResult[$i]['subject'];
$sendMail = $eMailer->sendEDM($email,$name,$subject,$message);
$iId = $arrResult[$i]['id'];
if ($sendMail)
{
$update['status'] = 1;
$update_edm = $oAdminEmail->updateEmailStatus($update,$iId);
}
}
?>
This is sql statement :
function updateEmailStatus($record, $iId)
{
global $db;
$bResult = false;
if(empty($iId)) return $bResult;
$record['send_timestamp'] = date("Y-m-d H:i:s", time());
$sUpdRecordList = $db->cpsFldUpdtSQLSeg($record);
$stmt = "UPDATE "._CONST_TBL_EMAIL_OUTGOING." SET ".$sUpdRecordList." WHERE id =".$iId;
if(!$db->Execute($stmt)) return $bResult;
return true;
}
Just replace your sql statement. If you pass string value to sql and it must be put '' to cover your string value.
This is your sql statement :
$stmt = "UPDATE "._CONST_TBL_EMAIL_OUTGOING." SET ".$sUpdRecordList." WHERE id =".$iId;
Copy this code and replace to your sql :
$stmt = "UPDATE "._CONST_TBL_EMAIL_OUTGOING." SET ".$sUpdRecordList." WHERE id ='".$iId."'";
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.