The code gives Output like:
1
1
It doesn't give the correct answer.
<?php
global $wpdb;
$sql = "select gemeinde, count(*) as total from gemeinde where gemeinde= 'Barsbek' ";
$result = $wpdb->query($sql);
echo $result;
?>
If you want to use pure sql instead of wp_query then your code would be something like this:
global $wpdb;
$sql = "SELECT gemeinde FROM gemeinde WHERE gemeinde= 'Barsbek' ";
$result = $wpdb->get_results($sql);
echo $result->num_rows;
If you want your results in an array format then you could do something like this:
global $wpdb;
$sql = "SELECT gemeinde FROM gemeinde WHERE gemeinde= 'Barsbek' ";
$result = $wpdb->get_results($sql, ARRAY_A); // Notice you could pass ARRAY_A to get your results in array format
Related
I want to show the count of users which have the status 1 (see code) within PHP MySQL.
<?php
// something like this
$result = mysqli_query($mysqli, "SELECT COUNT(*) FROM users WHERE status = '1'");
echo "$result";
?>
Try this:
$query = "SELECT COUNT(*) as countvar FROM users where status = '1'";
$result = mysqli_query($con,$query);
$row = mysqli_fetch_array($result);
$count= $row['countvar '];
Following is my code,
$result1 = "SELECT emp_id FROM employee where manager_id=".$userID;
$array = mysql_query($result1);
$cnt = 0;
while ($row = mysql_fetch_array($array)) {
"emp_id: " . $row[0];
$myArrayOfemp_id[$cnt] = $row[0];
$cnt++;
}
var_dump($myArrayOfemp_id);
$sql = "SELECT emp_id FROM emp_leaves WHERE emp_id='$myArrayOfemp_id' ORDER BY apply_date DESC";
$result = mysql_query($sql);
$total_results = mysql_num_rows($result);
When I'am trying to use $myArrayOfemp_id variable in $sql query, It shows that error:
Array to string conversion in..
How can I fix it?
You are trying to convert an array into a string in the following line:
$sql = "SELECT emp_id FROM emp_leaves
WHERE emp_id='$myArrayOfemp_id' ORDER BY apply_date DESC";
$myArrayOfemp_id is an array. That previous line of code should be changed to:
$sql = "SELECT emp_id FROM emp_leaves
WHERE emp_id={$myArrayOfemp_id[0]} ORDER BY apply_date DESC";
I placed 0 inside {$myArrayOfemp_id[0]} because I'm not sure what value want to use that is inside the array.
Edited:
After discussing what the user wanted in the question, it seems the user wanted to use all the values inside the array in the sql statement, so here is a solution for that specific case:
$sql = "SELECT emp_id FROM emp_leaves
WHERE ";
foreach ($myArrayOfemp_id as $value)
{
$sql .= " emp_id={$value) || ";
}
$sql .= "1=2";
$result = mysql_query($sql);
$total_results = mysql_num_rows($result);
$sql = "SELECT emp_id FROM emp_leaves WHERE emp_id in
(SELECT GROUP_CONCAT(emp_id) FROM employee where manager_id=".$userID.")
ORDER BY apply_date DESC";
$result = mysql_query($sql);
$total_results = mysql_num_rows($result);
just change your query like above might solve your problem.
you can remove following code now. :)
$result1 = "SELECT emp_id FROM employee where manager_id=".$userID;
$array = mysql_query($result1);
$cnt = 0;
while ($row = mysql_fetch_array($array)) {
"emp_id: " . $row[0];
$myArrayOfemp_id[$cnt] = $row[0];
$cnt++;
}
var_dump($myArrayOfemp_id);
I build a query which i get an assoc array from. But how can I loop through them. this is what I have.
My SQL Query:
public static function album_display() {
global $database;
$sql = "SELECT a.*, ";
$sql .= "(SELECT photographs.filename ";
$sql .= "FROM photographs ";
$sql .= "WHERE photographs.albumid = a.name ";
$sql .= "ORDER BY photographs.creation_date ASC ";
$sql .= "LIMIT 1) ";
$sql .= "AS filename FROM album a ";
$result_set = $database->query($sql);
while($rows = mysqli_fetch_assoc($result_set)){
foreach($rows as $row) {
print_r($row);
}
}
}
If I use print_r() on $row the output is:
15Jaap1Jaap1.JPG16Jaap2Jaap4.JPG17Jaap3Jaap7.JPG18Jaap4Jaap12.JPG19Jaap5
I want to loop through Jaap1, Jaap2, Jaap3, Jaap 4 and put these in $albumid from the function album_path($albumid, $filename). I want to do the same wiht the JPG files. This function is in Class() Album. So is the function album_display()
The function album_path($albumid, $filename) if called needs to output the complete album/images path en echo these to the screen. And display the images
public function album_path($albumid, $filename) {
return $this->upload_dir.DS.$albumid.DS.$filename;
}
How can I use foreach for this. Or is there an other better way to do it?
Kind Regards,
Coos Wolff
Change the select a.*, into a select a.albumid. That should only return you the album id, and the filename, coming from the second SELECT statement.
Perhaps something like this? You can break the sql up onto multiple lines within the quotes which makes it easier to read and also use an alias for each of the tables, again making it slightly easier to read and quicker to write.
<?php
public static function album_display() {
global $database;
$sql = "SELECT a.*,
( SELECT p.`filename`
FROM `photographs` p
WHERE p`.`albumid` = a.name
ORDER BY p.`creation_date` ASC
LIMIT 1
) AS 'filename' FROM `album` a ";
$result_set = $database->query( $sql );
/* using object notation rather than array for ease */
while( $rows = mysqli_fetch_object( $result_set ) ){
$this->album_path( $rows->albumid, $rows->filename );
}
}
?>
$my_array = [];
public static function album_display() {
global $database;
$sql = "SELECT a.*, ";
$sql .= "(SELECT photographs.filename ";
$sql .= "FROM photographs ";
$sql .= "WHERE photographs.albumid = a.name ";
$sql .= "ORDER BY photographs.creation_date ASC ";
$sql .= "LIMIT 1) ";
$sql .= "AS filename FROM album a ";
$result_set = $database->query($sql);
while($rows = mysqli_fetch_assoc($result_set)){
foreach($rows as $row) {
$my_array['albumid'][] = $row->albumid;
$my_array['filename'][] = $row->filename;
}
}
}
foreach ($my_array['albumid'] as $albumid) {
echo $albumid;
}
foreach ($my_array['filepath'] as $filepath) {
echo $filepath;
}
album_path($my_array['albumid'], $my_array['filepath']);
mysqli_fetch_assoc returns one row at a time from the result set built by your query.
Each $row is an associative array and if you look more closely at the output of the var_dump() you will see that the array is a set of key - value pairs.
while($rows = mysqli_fetch_assoc($result_set)){
//echo $row['id'];
//echo $row['filename'];
//echo $row['albumid'];
// just to be sure add this line
// so we know exactly whats in $row
print_r($row);
}
Sorry because you used SELECT * for the other fields I dont know what to call the array elements. I hope you get the basic idea.
I'm having trouble with a mysqli_query from inside a foreach loop, I'm getting a string from a table first, then separating that into an array. Then I try looping through the array and calling a query inside the loop.
$langs_result = mysqli_query($con, "SELECT Languages FROM users WHERE Username = '$username'");
$row = mysqli_fetch_assoc($langs_result);
$langs = $row['Languages'];
$userLangs = str_replace(" ","",$langs);
$userLangs = explode(",",$langs);
print_r($userLangs);
$posts = array();
foreach($userLangs as $lang){
echo "$lang <br>";
$sql = "SELECT * FROM posts WHERE Language = '$lang'";
$getLangPosts = mysqli_query($con, $sql);
array_push($posts, mysqli_fetch_assoc($getLangPosts));
}
print_r($posts);
for this user the langusges are German, Italian, Danish, and English, but the $posts array only contains the first post found from the first language (German), can anybody help? I am trying to get all of the posts for each language in the $userLangs array.
It's going through the foreach loop okay as the $lang variable that's echoed changes each time but the query still isn't working properly.
Thanks for the help!
select posts.* from posts
left join users on users.language=posts.language
where users.username='your_desiredusername'
group by users.language;
Just try to run this as a single query by filling the username
no need of multiple queries
You an avoid multiple queries by doing a JOIN, using FIND_IN_SET to match on your comma separated list. You probably need to use REPLACE to get rid of the extra spaces in the comma separated list as well.
Then you can just loop around to display the data, displaying the language on change of language:-
<?php
$sql = "SELECT a.Languages AS user_languages,
b.*
FROM users a
LEFT OUTER JOIN posts b
ON FIND_IN_SET(b.Language, REPLACE(a.Languages, ' ', ''))
WHERE a.Username = '$username'
ORDER BY b.Languages";
$langs_result = mysqli_query($con, $sql);
if($row = mysqli_fetch_assoc($langs_result))
{
print_r(explode(', ', $row['user_languages']));
$prev_langauge = '';
while($row = mysqli_fetch_assoc($langs_result))
{
if ($prev_langauge != $row['Languages'])
{
if ($prev_langauge != '')
{
print_r($posts);
}
$posts = array();
echo $row['Languages']."<br>";
$prev_langauge = $row['Languages'];
}
array_push($posts, mysqli_fetch_assoc($row));
}
if ($prev_langauge != '')
{
print_r($posts);
}
}
UPDATE
See this code:
<?php
$langs_result = mysqli_query($con, "SELECT Languages FROM users WHERE Username = '$username'");
$row = mysqli_fetch_assoc($langs_result);
$langs = $row['Languages'];
// $langs = 'German, Italian, Danish, French'; added this to run the test
// $userLangs = str_replace(" ","",$langs); this is not needed, see the explode below
$userLangs = explode(", ",$langs);
foreach($userLangs as $lang){
echo $lang;
$sql = "SELECT * FROM posts WHERE Language = '$lang'";
$getLangPosts = mysqli_query($con, $sql); // this is the result of the select *
while($post = mysqli_fetch_assoc($getLangPosts)){ // iterate over all the results
$postField = $post["yourChoiceField..say..Title"]; // get something from each row
array_push($posts, $title); // push into array
}
}
print_r($posts);
Since the initial select is based on username I don't believe the first loop is needed so your code was on the right track.
A second loop was needed to iterate over the posts though and a field to populate the $posts array.
You need to perform mysqli_fetch_assoc in a loop
$langs_result = mysqli_query($con, "SELECT Languages FROM users WHERE Username = '$username'");
while($row = mysqli_fetch_assoc($langs_result)){
$langs = $row['Languages'];
$userLangs = str_replace(" ","",$langs); // i don't get why you are doing this though
$userLangs = explode(",",$langs);
print_r($userLangs);
$posts = array();
foreach($userLangs as $lang){
echo "$lang <br>";
$sql = "SELECT * FROM posts WHERE Language = '$lang'";
$getLangPosts = mysqli_query($con, $sql);
array_push($posts, mysqli_fetch_assoc($getLangPosts));
}
print_r($posts);
}
It would help to know how what the select query actually returns.
mysqli_fetch_assoc only fetches one row on each call you need to use it like this:
while ($row_new = mysqli_fetch_assoc($getLangPosts)){
array_push($posts, $row_new);
}
You need to loop the inner query to get the column data
foreach($userLangs as $lang){
echo "$lang <br>";
$sql = "SELECT * FROM posts WHERE Language = '$lang'";
$getLangPosts = mysqli_query($con, $sql);
while($row1 = mysqli_fetch_assoc($getLangPosts))
array_push($posts, $row1['YOUR_COLUMN_NAME']);
}
OR you should use IN clause instead of loop
$langs_result = mysqli_query($con, "SELECT Languages FROM users WHERE Username = '$username'");
while($row = mysqli_fetch_assoc($langs_result)){
$langs = $row['Languages'];
$userLangs = str_replace(" ","",$langs);
$userLangs = explode(",",$langs);
print_r($userLangs);
$posts = array();
$sql = "SELECT * FROM posts WHERE Language IN ('".implode(',',$userLangs)."')";
$getLangPosts = mysqli_query($con, $sql);
while($row1 = mysqli_fetch_assoc($getLangPosts))
array_push($posts, $row1['YOUR_COLUMN_NAME']);
}
How can I get a single column value from mysqli? The result should be single row with only one column.
This is what I have tried:
$query = "SELECT MAX(`userid`) FROM `user`";
$rlt = mysqli_query($this->db, $query);
echo $rlt['userid'];
You are not fetching the row after executing the query:
$query = "SELECT MAX(`userid`) FROM `user'";
$rlt = mysqli_query($this->db,$query);
$row = mysqli_fetch_row($this->db, $rlt);
echo $row[0];
The alternative would be to use an alias for the computed field and use fetch_assoc:
$query = "SELECT MAX(`userid`) as `maxid` FROM `user'";
$rlt = mysqli_query($this->db,$query);
$row = mysqli_fetch_assoc($this->db, $rlt);
echo $row['maxid'];
try with create alias and fetch result after query execution also your quote of user' looking wrong
$query = "SELECT MAX(`userid`) as userid FROM `user`";
$rlt = mysqli_query($this->db,$query);
$r = mysqli_fetch_assoc($rlt);
echo $r['userid'];
or fetch only one row like:-
$r = mysqli_fetch_row($this->db, $rlt);
echo $r[0];
You should create alias here.
Use this one:
$query = "SELECT MAX(`userid`) as Max_Userid FROM `user'";
$rlt = mysqli_query($this->db,$query);
$row = mysqli_fetch_assoc($this->db, $rlt);
echo $row['Max_Userid'];
Note: Always use alias when you use mysql function in query with fields.