function not working properly, codeigniter - php

The function is fetching data from the database, but the issue is, it is not fetching the next row after printing the first row.
Kindly check and tell me what I'm doing wrong?
Public function Return_Config($P_name, $WW, $KPI) {
log_message('debug', 'hd');
log_message('debug', $P_name . ' ' . $WW . ' ' . $KPI);
$P_name .= '_one_voice_perf_measured';
$select_query = "SELECT config FROM $P_name WHERE KPI = '$KPI' AND Config = '$WW'";
//echo $select_query;
$query = $this->db->query($select_query);
if ($query->row() > '0') {
return $query->row()->config;
} else {
return 'Not Measured';
}
}
It is going directly to the else part. please help

This is because you are using ->row() instead use ->result(), like:
Public function Return_Config($P_name, $WW, $KPI)
{
log_message('debug', 'hd');
log_message('debug', $P_name . ' ' . $WW . ' ' . $KPI);
$P_name.= '_one_voice_perf_measured';
$select_query = "SELECT config FROM $P_name WHERE KPI = '$KPI' AND Config = '$WW'";
// echo $select_query;
$query = $this->db->query($select_query);
if (!empty($query->result()))
{
return $query->result();
}
else
{
return 'Not Measured';
}
}
More details at https://www.codeigniter.com/userguide3/database/results.html

try this
Public function Return_Config($P_name,$WW,$KPI){
log_message('debug','hd');
log_message('debug', $P_name.' '.$WW.' '.$KPI);
$P_name .= '_one_voice_perf_measured';
$select_query = "SELECT config FROM $P_name WHERE KPI = '$KPI' AND Config = '$WW'";
//echo $select_query;
$query = $this->db->query($select_query)->row_array();
if (!empty($query)){
return query['config'];
}
else{
return 'Not Measured' ;
}
}

Related

wait a few seconds before going further with the function php

so i have this deletefile function and i want it to go back to tyhe home page after 3 seconds automaticly
public function deleteFile($id)
{
try {
$query = "SELECT naam, bestand_path FROM bestanden ";
$query .= "WHERE id='$id'";
$result = $this->datahandler->readsData($query);
$results = $result->fetchAll();
foreach ($results as $row) {
$filename = $row['naam'];
}
$file = getcwd() . "/uploads/" . $filename;
//echo "Absolute Path To Directory is: ";
//echo $delfile;
***if (unlink($file)) {
echo $filename . ' was deleted successfully!';
header('Location: ' . SERVER_URL . '/Home/');***
} else {
echo 'There was a error deleting ' . $filename;
}
$query = "DELETE FROM bestanden ";
$query .= "WHERE id=$id";
$result = $this->datahandler->deleteData($query);
} catch (PDOException $e) {
echo "Fout opgetreden";
}
}
its about the one with the stars around it.
thank you

Php - Validations and errors - unique name

I don't want to save duplicate name record. I want to display errors back to the user.
But it doesn't work. I don't know what I'm doing wrong.
protected function has_unique_name($value, $current_id="0") {
$sql = "SELECT * FROM photographs ";
$sql .= "WHERE caption='" . self::$database->escape_string($this->caption) . "' ";
$sql .= "AND id != '" . self::$database->escape_string($current_id) . "'";
echo $sql;
$result = self::$database->query($sql);
$products_count = $result->num_rows;
echo $products_count . "<br />" ;
$result->free();
return $products_count === 0;
}
protected function validate() {
$this->errors = [];
$value = $this->caption;
$current_id = isset($this->id) ? $this->id : '0';
if(!$this->has_unique_name($this->caption, $current_id)) {
$errors[] = "The name must be unique.";
}
return $this->errors;
}
public function create() {
$this->validate();
if(!empty($this->errors)) { return false; }
...

Check for validation from different tables - joomla

I am new to joomla. I am using RSDirectory Component. In this i have to customize validation. i have a file named rsdirectory.php which is located at: administrator>components>com_resdirectory>helpers>rsdirectory.php.
I have a table, table1. In which a unique code is stored. Now i want to fill a form using that code if exist then query will execute otherwise my validation code will be execute. i have done this successfully. Now i have an another table, table2 in which my data is storing when i am filling a form with unique code. i just want to check my unique code whether it is exist in table2 or not. if exist validation will execute.i want to use same function for both.
Here is my code. Thanks in advance.
public static function uniquestacksfield($value, $column,$column1,$id1=null, $id = null){
// Get DBO.
$column = 'uni_code';
$db = JFactory::getDBO();
$query = $db->getQuery(true)
->select($db->qn('id'))
->from($db->qn('#_table1', 'e'))
->where($db->qn($column) . ' = ' . $db->q($value));
$db->setQuery($query);
$entry = $db->loadObject();
if ($id) {
$query->where($db->qn('e.id') . ' = ' . $db->q($id));
}
$db->setQuery($query, 0, 1);
return $db->loadResult();
/------------------another query-----------------------/
$column1 = 'uni_code2';
$db1 = JFactory::getDBO();
$query1 = $db1->getQuery(true)
->select($db1->qn('entry_id'))
->from($db1->qn('#_table2', 'c'))
->where($db1->qn($column1) . ' = ' . $db1->q($value));
$db1->setQuery($query1);
$entry1 = $db1->loadObject();
if ($id1) {
$query1->where($db1->qn('c_entry_id') . ' = ' . $db1->q($id1));
}
$db1->setQuery($query1, 0, 1);
return $db1->loadResult();
}`
I think below code is may be use.you can try it now. it is not good explain more to your criteria
public static function uniquestacksfield($value, $column,$column1,$id1=null, $id = null){
// Get DBO.
$db = JFactory::getDBO();
$column = 'uni_code';
$column1 = 'uni_code2';
if($column == 'uni_code') {
$query = $db->getQuery(true)
->select($db->qn('id'))
->from($db->qn('#_table1', 'e'))
->where($db->qn($column) . ' = ' . $db->q($value));
$db->setQuery($query);
$entry = $db->loadObject();
if ($id) {
$query->where($db->qn('e.id') . ' = ' . $db->q($id));
}
} else {
$query = $db->getQuery(true)
->select($db->qn('entry_id'))
->from($db->qn('#_table2', 'c'))
->where($db->qn($column1) . ' = ' . $db->q($value));
$db->setQuery($query);
$entry = $db1->loadObject();
if ($id) {
$query->where($db->qn('c_entry_id') . ' = ' . $db->q($id1));
}
}
$db->setQuery($query, 0, 1);
$item = $db->loadResult();
return $item;
}

php mysql return wrong boolean result

I have the following query :
'SELECT Active FROM tbUsers WHERE Id=55'
The Id is unique and I need just to know the status of the user if he's active yes or no. The Column Active is set as boolean in Mysql
When I tried to return the result like the following (using another php function) :
$result = $this->selectRow($db,"tbClass","Active","Id='$Id'");
if($result) { return "ok" ; } else { return "nok" ;)
it returns 'ok' on both cases.
Any idea what's wrong with it ?
Here is the other function :
public function selectRow($db,$tableName,$field,$where) {
if($where == "") {
$query = "SELECT $field FROM $tableName";
}
else
{ $query = "SELECT $field FROM $tableName WHERE $where"; }
$result=$db->Qry($query);
if ($result) {
$no_of_rows = $db->TotRows($result);
if($no_of_rows == 1) {
return $result;
}
if($no_of_rows == 0) {
return '';
}
if($no_of_rows < 0) {
die('Invalid query: ' . $sender ."(".$query ."): ".mysql_errno().": ". mysql_error());
return '';
}
}
else {
die('Invalid query: ' . $sender .": " .$query.": ". mysql_errno().": ". mysql_error());
return '';
}
}
Qry Function is the following :
function Qry($sql) {
if($result = mysqli_query($this->con,$sql) ) {
return $result;
}
else
{
$err = "Error: ".$sql. " :: ". mysqli_error;
die("$err");
}
}
I think you need to change this condition .
$result=$db->Qry($query);
always return you query object
Just remove this condition and
You need to count number of affected row
<?php
public function selectRow($db, $tableName, $field, $where) {
if ($where == "") {
$query = "SELECT $field FROM $tableName";
} else {
$query = "SELECT $field FROM $tableName WHERE $where";
}
$result = $db->Qry($query);
$no_of_rows = $db->TotRows($result);
if ($no_of_rows == 1) {
return $result;
}
if ($no_of_rows == 0) {
return FALSE;
}
if ($no_of_rows < 0) {
die('Invalid query: ' . $sender . "(" . $query . "): " . mysql_errno() . ": " . mysql_error());
return FALSE;
}
}
Alright, so $result is a mysqli_result object.
You might want to fetch the first row of that result and return the desired column.
Replace
return $result;
with
return $result->fetch_assoc()[$field];

(not all) Data can't reach database

I want to send data to my database (group_id, user_id and group_name) but only the first two are getting into the database. When I var_dump $groupinvitation->Invitation_group_name = mysql_real_escape_string($groupname); it gives me the correct group_name. What am I doing wrong?
When I replace '" . $db->conn->real_escape_string($this->Invitation_group_name) . "' with a random word it is working well..
PHP
$groupinvitation = new GroupInvitation();
if (isset($_POST["Accept"])) {
try {
$group_id = mysql_real_escape_string($_POST["group_id"]);
$groupinfo = $group->GetGroupInfoByGroupId($group_id);
$groupname = $groupinfo['group_name'];
$requestnumber = mysql_real_escape_string($_POST['acceptID']);
$groupinvitation->AddAsGroupMember($number, $group_id);
$groupinvitation-> AcceptGroupRequest($requestnumber);
$groupinvitation->Invitation_group_name = mysql_real_escape_string($groupname);
$feedback = "Awesome, You just added a friend!";
} catch(Exception $e) {
$feedback = $e -> getMessage();
}
}
DECLARATIONS:
class GroupInvitation
{
private $m_sGroup_invitation_group_name;
public function __set($p_sProperty, $p_vValue)
{
switch($p_sProperty)
{
case "Invitation_group_name":
$this->m_sGroup_invitation_group_name = $p_vValue;
break;
}
}
public function __get($p_sProperty)
{
switch($p_sProperty)
{
case "Invitation_group_name":
return $this->m_sGroup_invitation_group_name ;
break;
}
}
FUNCTION:
public function AddAsGroupMember($number, $group_id)
{
$db = new Db();
$insert = "INSERT INTO tblgroup_member(
group_id,
user_id,
group_name
) VALUES (
'" . $db->conn->real_escape_string($group_id) . "',
'" . $db->conn->real_escape_string($number) . "',
'" . $db->conn->real_escape_string($this->Invitation_group_name) . "'
)";
$db->conn->query($insert);
}
Try changing
$groupinvitation->AddAsGroupMember($number, $group_id);
$groupinvitation-> AcceptGroupRequest($requestnumber);
$groupinvitation->Invitation_group_name = mysql_real_escape_string($groupname);
to
$groupinvitation->Invitation_group_name = mysql_real_escape_string($groupname);
$groupinvitation->AddAsGroupMember($number, $group_id);
$groupinvitation-> AcceptGroupRequest($requestnumber);
You could be setting the property after the insert.

Categories