I am trying to search product from the database and then display in view. But it showing me an error that is this...
A PHP Error was encountered
Severity: Notice
Message: Array to string conversion
Filename: database/DB_active_rec.php
Line Number: 679
Here is the code of Controller
function search_prd()
{
$query = $this->input->post();
$this->load->model('prd/addprd');
$prd_search = $this->addprd->prd_search($query);
$this->load->view('prd_search', ['prd_search' => $prd_search]);
}
And this code is MODEL
function prd_search($query)
{
$q = $this->db->from('purchase')
->like('item_name', $query)
->get();
return $q->num_rows();
}
CI's input->post() is an array containing POST data. The second parameter of CI's active record like() expects a string. If you mean to pass an individual field value, you will need to access that index:
<input name="search_value">
$query = $this->input->post('search_value'); //notate the field being used here
Then you can query where like:
$this->db->like('column', $query); //query is now a string
Alternatively, if you mean to pass the entire array, you can use where_in()
$this->db->where_in('column', $query); //if you assign $query to $this->input->post();
Related
I am upgrading our Codeigniter (v. 3.1.11) site from php 5.6 to php 7.2 (currently running on localhost on my Mac). Slowly I am finding all the instances of count() usage and correcting them. It is my understanding that an array is countable, but I seem to not be able to count arrays returned by Codeigniter's result_array() function after a database call....
the following section of my controller
$reviews = $this->reviews_model->review_details($productname);
echo "Variable is type: ".gettype($reviews);
if (count($reviews >=1)) {
$myreview=$reviews[0];
} else {
$myreview=0;
}
return $myreview;
calls this function in my model (note that I am echoing out the variable type just to be sure!)
function review_details($pagename) {
$r = false;
$sql = "select Reviews.*, ReviewItemLink.Item as Product, ReviewItemLink.* from Reviews LEFT JOIN ReviewItemLink ON Reviews.ReviewItemID=ReviewItemLink.ReviewItemID where pagename=? AND ReviewActive = 1 ORDER BY Date DESC";
$query = $this->db->query($sql, $pagename);
if ($query->num_rows() > 0):
$r = $query->result_array();
endif;
return $r;
}
And even though the variable is an array
Variable is type: array
I still get the by now, oh-so-familiar warning message:
Severity: Warning
Message: count(): Parameter must be an array or an object that implements Countable
Filename: controllers/Software.php
Line Number: 1005
Backtrace:
File: /Users/juner/Sites/html/application/controllers/Software.php
Line: 1005
Function: _error_handler
File: /Users/juner/Sites/html/application/controllers/Software.php
Line: 75
Function: _get_my_review
File: /Users/juner/Sites/html/index.php
Line: 324
Function: require_once
Are there types of arrays that are NOT countable? Any suggestions/ideas would be most helpful!
This part is incorrect (typo?):
if (count($reviews >=1)) {
$myreview=$reviews[0];
} else {
$myreview=0;
}
You are passing a boolean to the count function. count($reviews >=1) turns to count(true); which is why you got your warning.
if (count($reviews >=1)) { should be if (count($reviews) >=1 ) {
So I want to make a function where I can fetch data like the following
$user->userData('username');
Here is my current function
public function userData() {
$username = "username";
$query = $this->db->prepare("SELECT * FROM users WHERE username=:username");
$query->execute(['username' => $username]);
$row = $query->fetch(PDO::FETCH_ASSOC);
return $row;
}
the problem is that It's returning this error when I call the function:
Notice: Array to string conversion in /Applications/XAMPP/xamppfiles/htdocs/php/home.php on line 9 Arrays
Line 9 is
echo $user->userData('username');
echo function prints strings.
Array returned by your function is not a string (definitely).
So, php tries to convert array to string and warns you about it.
If you want to print values that are not string - use printr_r or var_dump.
I am trying to retrieve data that is collected in a function, that contains my query. I am returning the data back to the main page and printing it using a while loop however i am getting this error:
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, string given
try to return your result like
return $query;
cause you are returning string(your mysql_result($query, 0) returning 0th position column value only), need a query result for mysql_fetch_assoc()
try the corrected function:
function get5star(){
$strSQL = "SELECT * FROM5starproducts";
$query = mysql_query($strSQL);
return $query;
}
i got this error message while running my Php codeigniter project :
A PHP Error was encountered
Severity: Warning
Message: Missing argument 1 for mtree::insert_to_right(), called in
C:\AppServ\www\News\application\controllers\bcontroller.php on line 20
and defined
Filename: models/mtree.php
Line Number: 28
this is my model function :
function insert_to_right($row, $direct_id) {
$left_parent = $this->get_right_parent($direct_id);
$row->members_direct_id = $left_parent;
$this->db->insert('table1', $row);
$sql = "INSERT INTO table2 (child, parent)
VALUES (" . $this->db->escape($row->members_id) . ", " . $this->db->escape($left_parent) . ")";
$this->db->query($sql);
}
function get_right_parent($id) {
return $id;
}
Since the function insert_to_right($row, $direct_id) requires two arguments, either you can pass in those two variables when you call them, or you can assign default values for it like insert_to_right($row = '....', $direct_id = '...'). I don't think you can just pass in any default variable as this would insert it into the database, so I would recommend you pass in the argument from the controller.
I insert in database values (array) $row->units with use function serialize()=>[$row->units], how can echo they with unserialize() in json_encode with $row->name? (return send for ajax call in jQuery)
Columns in database:
$row->units =>
a:6:{i:0;s:15:"Coffee";i:1;s:14:"Satellite";i:2;s:11:"Game
Notes";i:3;s:14:"Internet";i:4;s:10:"Pool";i:5;s:0:"";}
$row->name=> George Kurdahi
$query = $this->db->query("SELECT * FROM arraha WHERE name LIKE '%$search%' ORDER BY name asc");
$data = array();
foreach ($query->result() as $row)
{
$data[] = array('name' => $row->name, 'units' => unserialize($row->units)); // Line 22
}
return json_encode($data)
The error for code above is:
A PHP Error was encountered
Severity: Notice
Message: unserialize() [function.unserialize]: Error at offset 277 of
281 bytes
Filename: model.php
Line Number: 22
You have some issues with character encoding:
s:15:"Coffee"
15 means length in bytes. So you have to translate encoding of data fetched from DB into encoding that was used with serialize()
You can use json_encode instead of serialize:
$arr = array('Coffee', 'Satellite', /*...*/);
$row->units = json_encode($arr);