I got the following snippet to get some userrights out of the db:
$tmp = "SELECT REPLACE(group_concat(CAST(".$role." AS CHAR)),',','') AS rights FROM functionrights ORDER BY id ASC";
$query = $this->CI->db->query($tmp);
if($query->num_rows()>0){
$row = $query->row();
return $row->rights;
This returns nothing. If I execute the statement direct everything is ok?! What is wrong here?
Change $this->CI->db->query to $this->db->query
The ->CI is not necessary. Also, you have no } tag (but that could be just a copy/paste thing).
Besides that: ->row() returns one result, while ->result() returns all results.
Related
My query is not working when I use the variable in the WHERE clause. I have tried everything. I echo the variable $res, it shows me the perfect value, when I use the variable in the query the query is not fetching anything thus mysqli_num_rows is giving me the zero value, but when I give the value that the variable contains statically the query executes perfectly. I have used the same kind of code many times and it worked perfectly, but now in this part of module it is not working.
Code:
$res = $_GET['res']; // I have tried both post and get
echo $res; //here it echos the value = mahanta
$query = "SELECT * FROM `seller` WHERE `restaurant` = '$res'"; // Here it contains the problem I have tried everything. Note: restaurant name is same as it is in the database $res contains a value and also when I give the value of $res i.e. mahanta in the query it is then working.
$z = mysqli_query($conn, $query);
$row2 = mysqli_fetch_array($z);
echo var_dump($row2); // It is giving me null
$num = mysqli_num_rows($z); // Gives zero
if ($num > 0) {
while ($row2 = mysqli_fetch_array($z)) {
$no = $row2['orders'];
$id = $res . $no;
}
}
else {
echo "none selected";
}
As discussed in the comment. By printing the query var_dump($query), you will get the exact syntax that you are sending to your database to query.
Debugging Tip: You can also test by pasting the var_dump($query) value in your database and you will see the results if your query is okay.
So update your query syntax and print the query will help you.
$query = "SELECT * FROM `seller` WHERE `restaurant` = '$res'";
var_dump($query);
Hope this will help you and for newbies in future, how to test your queries.
Suggestion: Also see how to write a mysql query syntax for better understanding php variables inside mysql query
The problem is the way you're using $res in your query. Use .$res instead. In PHP (native or framework), injecting variables into queries need a proper syntax.
I'm new in programming and especially with php and MySQL. I have to create dynamic web site for homework. My problem is with the function below. I want it to return int value of tag by given tag name (string). But in my case the function returns every time '1'. Can anyone help me to solve this problem, thanks.
public function getTagIdByName(string $tagName) : int
{
$statement = self::$db->prepare("SELECT tags.id FROM tags WHERE tags.name = ? ");
$statement->bind_param("s", $tagName);
$result = $statement->execute();
return $result;
}
The problem is that you're returning the result of execute(), but that function doesn't actually give you your query result. You need to fetch results after making sure the execution was successful.
//don't forget to error-check before using query results
$statement->execute() or die($statement->error);
$result = $statement->get_result(); //retrieve results for processing
if(!$result->num_rows) return null;//if the id was not found in the DB
else return $result->fetch_assoc()['id'];
You can achieve easily with
$data = $result->fetch_assoc();
return $data['id']; // you can change with which you want to return with field name
And whichever you can use your returned values.
I am trying to write this below query in codeigniter format and getting some problem with the aggregate function:
$stmt = "select sum(subscription_amt) as samt, bill_month,
sum(loan_refund_amt*no_of_loan_installment+error_amt) as lamt
from pf_bill_det
where trim(pf_number)='$pfno'
and fin_year='$fyear'
and aproved='Y' group by bill_month";
$query = $this->db->query($stmt);
This query ending up with an error loan_refund_amt*no_of_loan_installment+error_amt is not a column. Please help me how to write this query using codeigniter query format.
why don't you try this
$this->db->select("COUNT(*) AS MyCount");
$this->db->from("MyTable");
$this->db->where("field", $value);
$this->db->get();
OR
$this->db->select("SUM(field_name) AS MySum");
$this->db->from("MyTable");
$this->db->where("field", $value);
$this->db->get();
OR
$this->db->select("SUM(field_name) AS MySum, username, password");
$this->db->from("MyTable");
$this->db->where("field", $value);
$this->db->get();
BUT
In simple query function you can use
$query = $this->db->query("SELECT COUNT(field_name) AS total_names, fname");
$query->result(); \\ Returns an array of objects
$query->result_array(); \\ Returns result as a pure array
$query->row(); \\ Returns a single result and first row
Try this,
$query=$this->db->query("select sum(subscription_amt) as samt,bill_month,sum(`loan_refund_amt`*`no_of_loan_installment`+`error_amt`) as lamt from pf_bill_det where trim(pf_number)='$pfno' and fin_year='$fyear' and aproved='Y' group by bill_month");
I have this function:
function set_spell() {
global $connection;
$query = "SELECT * FROM Spells";
$result_set = mysqli_query($connection, $query);
$spell_id_list = mysqli_fetch_array($result_set);
return $spell_id_list;
Then I try to output a random ID from the array, but I always get ID 1.
$spell_id_list = set_spell();
echo $spell_id_list[array_rand($spell_id_list)];
When I run this query in MYSQL, I get a list of all ID's as expected. Why doesn't the code above select one at random?
This is probably a stupid questions that I'll smack myself after I see the answer... but I've been stuck for longer than I think I should be on it.
Thanks for the help.
You just returning only single result from your function so either create array of all fetched results & return the same, or simply use order by rand() in your query,
SELECT * FROM Spells ORDER BY RAND();
I have piece of PHP code written in Codeigniter framework that returns nothing (an empty set).
Have a look at it and tell me what is wrong with it.
function sbsn($serial){
$this->db->select('asset_types.name as type_name,asset_brands.name as brand_name');
$this->db->from('asset_types,asset_brands,assets');
$this->db->where('assets.type_code','asset_types.code');
$this->db->where('assets.brand_code','asset_brands.code');
$this->db->where('serial_no',$serial);
$result = $this->db->get();
return $result;
}
hi with db get you are only getting result set not record to get results from result you have to the following thing
$this->db->get()->result(); // will return an array of objects
$this->db->get()->result_array(); //will return result in pure array
$this->db->get()->row() // just single row as object
$this->db->get()->row_array() // just single row as array
you can use $result to perform the same above things
$result->result();
$result->row();
for more information read generating result user guide
change:
$result = $this->db->get();
to
$result = $this->db->get()->row_array(); //to get single row
//OR
$result = $this->db->get()->result_array(); //to get multiple rows
OR try using JOIN, like
$this->db->select('asset_types.name as type_name,asset_brands.name as brand_name');
$this->db->from('assets');
$this->db->join('asset_types', 'asset_types.code = assets.type_code', 'left');
$this->db->join('asset_brands', 'asset_brands.code = assets.brand_code', 'left');
$this->db->where('assets.serial_no',$serial);
$result = $this->db->get()->result_array();
return $result;
The problem can be easily solved by joining the tables in the following way.
$this->db->select('at.name as type_name,ab.name as brand_name');
$this->db->from('asset_types as at,asset_brands as ab');
$this->db->join('assets as a', 'a.type_code = at.code and a.brand_code as ab.code');
$this->db->where('a.serial_no',$serial);
$result = $this->db->get()->result_array();
return $result;