it's been 3 hours that i'm trying to delete a row in mysql based on a id ...
Seems simple right ?
Taking into consideration that the array might contains several value:
$result = Array ( [3] => 4_Couture )
Array ( [3] => 4_Couture )
$sql_delete = "DELETE FROM users_resumes WHERE id_training_key = ? ";
$stmt_delete= $pdo->prepare($sql_delete);
foreach($result as $r) {
$stmt_delete->execute($r);
}
This seems to be right no ?
error : PDOStatement::execute() expects parameter 1 to be array, string given
Any, any, any clue is very welcome ! thanks a lot from France !
Assuming $result is a one-dimensional array like
$result = [ 3 => '4_Couture' ];
That means you're trying to call $stmt->execute() with a single string value where it requires an array.
I suggest you use bindParam instead
$stmt_delete = $pdo->prepare("DELETE FROM users_resumes WHERE id_training_key = ?");
$stmt_delete->bindParam(1, $r);
foreach ($result as $r) {
$stmt_delete->execute();
}
Related
I cannot get the values insight single<......> or double<<....>> symbol from mysql.
It's always return <> or <<>>.
Mysql Data:
Sample Code :
$sql = "SELECT id,html_form_data FROM `user_detail`";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_assoc($result))
{
$getFormData[] = $row;
}
print_r($getFormData);
Getting Result:
Array ( [0] => Array ( [id] => 1 [html_form_data] => ) 1 => Array ( [id] => 2 [html_form_data] => <> ) )
Note : I am geting the values after interchanged the <.....> into $.....$
But I know that any reason for return it? or any ways to getting values?
As I earlier mentioned in the comments try printing your $getFormData like this:
array_map("printHTML", $getFormData);
function printHTML($a) {
echo htmlentities($a)."<br/>";
}
Note: This is for your testing. This handles only one dimensional array for now.
Update1:
This handles multidimensional array:
function printHTML($a) {
if(!is_array($a))
echo htmlentities($a)."<br/>";
else
array_map("printHTML", $a);
}
Try to use codification of utf8_general_ci for that column in you DB.
I need to print/show list of comments for articles using this PHP function:
function _is_comments_($id,$type){
$db = mysqli_access::F("SELECT message FROM " . COMMENTS . " WHERE pid = ? AND type = ? AND approved = 1 ", $id, $type);
foreach($db as $row){
$commentdata['message'] = $row['message'];
}
return $commentdata;
}
In action :
$comments_list = _is_comments_('125','article');
print_r($comments_list);
In result :
Array ( [message] => this is a One comment )
But in MySQL database I have 18 comments for article id 125 and my result is false and show only One result!
how do fix This ?!
Your issue is the assignment in the loop here:
foreach($db as $row){
$commentdata['message'] = $row['message'];
}
Even if you got multiple result $rows, each iteration would overwrite the same $commentdata['message'] key. Which is why you only get to see one entry when returned from the function.
Instead you want to collect multiple entries. I would just drop the ['message'] key, and use an additive indexed array:
foreach($db as $row){
$commentdata[] = $row['message'];
}
This way your result array might contain:
Array (
[0] => this is a One comment
[1] => this is a second comment
[2] => third one
)
You need to make the message index an array.
$commentdata['message'][] = $row['message'];
I am using PDO statement like below
$sql1 = "select food_typename from foodtypes WHERE 1";
$statement1 = $pdo->prepare($sql1);
$statement1->execute();
$results1 = $statement1->fetchAll(PDO::FETCH_ASSOC);
print_r($results1);
I am getting output as below:
Array
(
[0] => Array
(
[food_typename] => Punjabi
)
[1] => Array
(
[food_typename] => Indian
)
)
I want it be like
Array('Punjabi','Indian')
Any suggestions please?
If you're running PHP >= 5.5
$results = array_column($results1, 'food_typename');
If you're running earlier versions of PHP,
$results = array_map(
$results1,
function($value) {
return $value['food_typename'];
}
);
Though I don't really understand why you can't work with the original array in the first place
You can use array_map but foreach works just as well and actually runs faster than array_map for cases like this:
// Set a test array.
$results1 = array();
$results1[] = array('food_typename' => 'Punjabi');
$results1[] = array('food_typename' => 'Indian');
// Set the final reults in an array.
$results_final = array();
foreach ($results1 as $results1_value) {
$results_final[] = $results1_value['food_typename'];
}
// Dump the line array for debugging.
echo '<pre>';
print_r($results_final);
echo '</pre>';
And the output of that would be:
Array
(
[0] => Punjabi
[1] => Indian
)
The query way:
SELECT GROUP_CONCAT(food_typename), 1 AS dummy
FROM foodtypes
GROUP BY dummy
Then you need to retrieve the first field of the only record you will get back from mysql, and turn it into an array: see return group_concat data as array
I have just started using CodeIgniter and want to get data from database using $query->result(), but without a foreach loop. Here is my current code:
$this->db->select('m_name');
$query1 = $this->db->get("marchant_details",1);
$rows1 = $query1->result();
However, I don't want to use a foreach loop like this, to retrieve the data:
foreach($query1->result() as $rows1)
{
$name[] = $rows1->m_name;
}
Can anyone offer an alternative solution?
I just did a quick search. I'm going to sleep, but can you try something like this:
$query->result_array();
If it doesn't work, I'll check tomorrow.
source: user-guide Maybe it will come handy.
There are two assumptions: either I misunderstood the question or the others did.
The point: by passing 1 as second parameter to $this->db->get(); method, it sets LIMIT 1 to the query and you'll get only 1 row as result.
So, why should you need to use a loop on a single row db result?
If you use $query->result(); the result would be something like:
Array
(
[0] => stdClass Object
(
[m_name] => Foo
)
)
To get the m_name value you can do the following:
$result = $query->result();
echo $result[0]->m_name;
By any reason, if you need a numeric array contains the value (as you did it in your loop) you can simply do it by $name[] = $result[0]->m_name;.
And if you use $query->result_array(); the result would be something like:
Array
(
[0] => Array
(
[m_name] => Foo
)
)
To get the m_name value you can do the following:
$result = $query->result_array();
echo $result[0]['m_name'];
But if you stop limiting the query, there are multiple rows in your query result, you can use rows1 = $query1->result_array(); instead. and array_map() to manipulate the elements of the given array:
$rows1 = $query1->result_array();
// `$row1` will be something like this:
Array
(
[0] => Array
(
[m_name] => Foo
)
[1] => Array
(
[m_name] => Bar
)
[2] => Array
(
[m_name] => Baz
)
)
Use array_map() to manipulate the result array:
function getName($array) {
return $array['m_name'];
}
$array = array_map("getName", $rows1);
print_r($array);
If you are using PHP v5.3+ you do the following:
$array = array_map(function($array) {
return $array['m_name'];
}, $rows1);
Well there is the official page in the CodeIgniter User Guide for generating DB query results depicting variations like
$row = $query->first_row()
$row = $query->last_row()
$row = $query->next_row()
$row = $query->previous_row()
If you work with CodeIgniter, its charm is that it effectively quite nicely documented.
I would assume you can go further down the abstraction layers if that's what you want, respectively you can not use the DB class but the class of your choice as custom library or whatever.
Why then are you not happy with the given possibilities, respectively how do you want to generate your query results?
you can do like this
$data=array();
$this->db->select('m_name');
$query1 = $this->db->get("marchant_details",1);
$data['name'] = $query1->result_array();
return $data;
and for more information
result_array(); // For multiple rows
row_array(); // For one row
I have a function that has a query and a foreach loop:
$sql = "SELECT * FROM explore WHERE id = $id";
$object = $this->db->select($sql);
foreach($object as $val){
$results = array('id'=>$val->id, 'from_id'=>$val->from_id);
$this->result[] = $this->notify($results);
}
return $results;
The issue here is that if I return $object I get 2 records:
Array
(
[0] => stdClass Object
(
[from_id] => 6
[id] => 3
)
[1] => stdClass Object
(
[from_id] => 6
[id] => 1
)
)
and return $results has 1 record:
Array
(
[id] => 1
[from_id] => 6
)
Also if I return $this->result;, $this->result[] = $this->notify($results); does run twice but uses the same record twice returned by $results instead of using the 2 records from $object
Hope you guys can understand my issue.
ps: I am using the zend framework
Any ideas?
Edit: notify is a function in another class
$getResults isn't being set anywhere in your code, visibly. It looks like you ought to be returning $this->result instead, as that's where the results are being stored. That's my best guess given the amount of code you've given us. If you can provide more code, I can further update my answer if it doesn't work for you.
Given your comment, update your code to this:
$sql = "SELECT * FROM explore WHERE id = $id";
$object = $this->db->select($sql);
foreach($object as $val){
$results = array('id'=>$val->id, 'from_id'=>$val->from_id);
$this->result[] = $this->notify($results);
}
return $this->result;
If you're only returning $results, it'll be filled with the last item, not every item.
The reason you have a problem is that you redefine the $results array on each iteration of the foreach loop, instead of adding an element to it.
This is where the problem is:
//...
foreach($object as $val){
$results = array('id'=>$val->id, 'from_id'=>$val->from_id);
//... ^ you are reassigning the whole value of $results
Do this instead:
//...
$results = array();
foreach($object as $val){
$results[] = array('id'=>$val->id, 'from_id'=>$val->from_id);
//... ^^ note the array push instead of complete reassign
EDIT as #Cyclone has rightly pointed out, the above answer is in fact wrong. You need to be doing one of two things:
returning $this->result instead of $results
populating the $results variable with the processed data, instead of $this->result.
Essentially, you either need to change:
return $results;
to:
return $this->result;
Or, change the loop to this:
$results = array();
foreach($object as $val){
$results[] = $this->notify(array('id'=>$val->id, 'from_id'=>$val->from_id));
}
Which one you want to do depends on whether you actually need $this->result - i.e. whether you need to keep the results in the object after this code has executed.