I'm getting data out of my database with a query that selects Klantvraag numbers.
The output at the moment shows 5x the same output Klantvraag number.
The problem there is that those 5 numbers each have same address but different house numbers.
So basically if there are 5 Klantvraag numbers I want it to only select the number once and output it once in a selectbox.
Can you help me with that ?
<?php
include("config/instellingen.php");
$query = "SELECT Klantvraag FROM `DWA` WHERE `Status DWA` = 'DBAA' AND `Kenmerk` > ''";
if ($result = mysqli_query($connect, $query)) {
while ($get = mysqli_fetch_assoc($result)){
echo '<option value="' . $get['Klantvraag'] . '">' . $get['Klantvraag'] . '</option>';
}
}
?>
Change your query to:
$query = "SELECT DISTINCT Klantvraag FROM `DWA` WHERE `Status DWA` = 'DBAA' AND `Kenmerk` > ''";
This will only add any occuring value once to your result.
use DISTINCT keyword and the query will be
SELECT DISTINCT Klantvraag FROM DWA WHERE Status DWA = 'DBAA' AND Kenmerk > ''
Hope it will help you
use group by and query something like this
$query = "SELECT Klantvraag FROM `DWA` WHERE `Status DWA` = 'DBAA' AND `Kenmerk` > '' group by `Klantvraag`";
Related
I`m trying to echo a single string from mysql database. There are columns in database (en, es, de, it...) representing different languages and I want to select value from one row and from column with current language ($language="en", or $language="es"...). I have tried:
<?php
$result = mysqli_query($con, 'SELECT "' .$language. '" FROM page WHERE
title="findInstructor" LIMIT 1');
$row = mysqli_fetch_row($result);
print_r($row[0]);
?>
The problem is, I am getting on screen value of $language variable, not a value from database. If I try it f.e. for english language, everything if fine:
$result = mysqli_query($con, 'SELECT en FROM page WHERE
title="findInstructor" LIMIT 1');
Or if I could do something like:
$result = mysqli_query($con, 'SELECT * FROM page WHERE
title="findInstructor" LIMIT 1');
....
print_r($row[$language]);
Thanks for an answer!
Because wrap $language with double quotes and SELECT treat it as a string and not column name. Try this:
<?php
$result = mysqli_query($con, 'SELECT ' .$language. ' FROM page WHERE
title="findInstructor" LIMIT 1');
$row = mysqli_fetch_row($result);
print_r($row[0]);
?>
As shown in below code if you put variable name in double quote,it's consider it as a static value.
'SELECT "' .$language. '" FROM page WHERE
title="findInstructor" LIMIT 1'
So instead of this you have to try below code
'SELECT ' .$language. ' FROM page WHERE
title="findInstructor" LIMIT 1'
Hope this will help you.
I'm going to count the sum of the value in rmNum column and with the same value of rmType.
From the table as shown in image above, can someone please teach how to to sum up the value of rmNum by referring to the same "keyword" of rmType?
i am new to PHP and i still get confusing about this even i went through the teaching part in other websites and questions that asked in Stackoverflow.
Edit 2:
<?php
require_once 'dbconnect.php';
$sql = "SELECT * FROM room";
$result = mysqli_query($conn, $sql);
echo $conn->error;
while($row = mysqli_fetch_array($result)) {
$typeRow = $row;
}
$sql1 = "SELECT rmType, SUM(rmNum) as total FROM room GROUP BY rmType";
$result = mysqli_query($conn,$sql1);
while ($row1 = mysqli_fetch_assoc($result)) {
echo $row1['rmType'] . ' had ' . $row1['total'] . ' room reservations';
}
mysqli_close($conn);
?>
this is the coding i used after all, it still give me Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean
You can use a GROUP BY statement:
$sql = "SELECT rmType, SUM(rmNum) as total FROM table GROUP BY rmType";
where table is the name of the table. This will return a table with two columns: the rmType and the total sum of rmNum for that type.
You can then process it like:
$sql = "SELECT rmType, SUM(rmNum) as total FROM table GROUP BY rmType";
$result = $mysqli->query($sql);
while ($row = $result->fetch_assoc()) {
echo $row['rmType'] . ' had ' . $row['total'] . ' room reservations';
}
EDIT: In case you want the total sum, you do not need to group, you can simply use:
$sql = "SELECT SUM(rmNum) as total FROM table";
$result = $mysqli->query($sql);
if ($row = $result->fetch_assoc()) {
echo 'The total number of reservation rooms is ' . $row['total'];
}
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("students");
$id = $_POST['id'];
$grade = $_POST['grade'];
$query = "INSERT INTO `st_table` (`St_id`,`Grade`) VALUES ('$id','$grade')";
$result = mysql_query($query);
$query = "SELECT * from `st_table`";
$result = mysql_query($query);
echo "<table>";
echo "<th>St_id</th><th>Grade</th>";
while($row = mysql_fetch_array($result)){
echo "<tr><td>" . $row['St_id'] . "</td><td>" . $row['Grade'] . "</td></tr>";
}
This code adds values into a table both ID and Grade. I want another query that will be able to count how many As, Bs, Cs, etc. and OUTPUT it on an html table.
Here, Your query is ok just group by Grade not Grades
"SELECT `Grade`, COUNT(*) AS count FROM `st_table` GROUP BY `Grade`";
Here is sqlfiddle
After edit
The query i am mentioning should work for you, you can check fiddle for that as for as you modified code is concerned you have to change your table a bit since you are going to include St_id as well so make it 3 column and correspondingly change query too.
I am using the Select query as
SELECT id, ordering FROM `jos_menu` WHERE ordering='".$rec['ordering'] -'1' ."' AND parent = '0'
Here I need all the records whose ordering is less than 1 of the selected record's order($rec['ordering'] = getting from other select query ) when I am trying to echo the query I am not getting complete statement but getting only this -1' AND parent = '0'
here is the whole snippet
$where = ' WHERE (id = ' . implode( ' OR id = ', $cid ) . ')';//Pranav Dave Coded
echo $selquery = "SELECT id, ordering FROM `jos_menu`".$where; //Pranav Dave Coded
$db->setQuery( $selquery );//Pranav Dave Coded
$record = $db->loadAssocList(); //Pranav Dave Coded
if ($model->orderItem($id, -1)) {
echo "<pre>";
print_r($model);
/*exit;*/
//echo $updorderup = mysql_escape_string($model->_db->_sql);//Pranav Dave Coded
foreach($record as $rec)//Pranav Dave Coded
{
echo $aboverow = "SELECT id, ordering FROM `jos_menu` WHERE ordering='".$rec['ordering'] -'1' ."' AND parent = '0'";
$db->setQuery( $aboverow );
$above = $db->loadAssoc();
echo "<pre>";
print_r($above);
}//end of foreach
}//end of if
Please suggest me where I am getting wrong.....
It looks like you may need to unwrap the -1 from the quotes:
WHERE ordering='".($rec['ordering'] - 1)."' AND parent = '0'";
Why do you trying to put everything inline?
Why not to make some preparations first?
Why not to compare resulting query with sample one?
Why don't you check every step if it return proper result?
$val = $rec['ordering'] - 1;
//let's see if $cal has proper value:
echo $val."<br>";
$sql = "SELECT id, ordering FROM `jos_menu` WHERE ordering = $val AND parent = 0";
//let's see if query looks good:
echo $sql;
//let's print sampe query to compare:
echo "<br>" ;
echo "SELECT id, ordering FROM `jos_menu` WHERE ordering = 1 AND parent = 0";
As Daniel said, you need to remove the quotes around the -1. Currently its trying to minus a string, which it wouldn't be happy with at all ;)
So I have a list of CSVs in one table. (EG: 1,3,19 )
I want to search out all of the usernames from the other table where the ids match any of those.
I feel like I should be able to do something like:
<?
$query = "SELECT player_ids FROM cast_list WHERE game='".$gameid."' ";
$result = mysql_query($query) or die(mysql_error());
$playerquery = "SELECT username,id FROM players WHERE id IN (".$result.") ORDER BY username;
$player_result = mysql_query($playerquery) or die(mysql_error());
echo "<ul>";
while ($row = mysql_fetch_array($player_result) ) {
echo "<li>".$row['username']."</li>";
}
echo "</ul>";
?>
but I can't get it to work. What am I doing wrong?
You can also use a subquery (which will be faster):
$playerquery = "SELECT username,id
FROM players
WHERE id IN (SELECT player_ids FROM cast_list WHERE game='".$gameid."')
ORDER BY username";
Btw if game is an integer field you don't have put quotes (' ') around the value.
The idea is correct, but you need to transfer the $result to an actual string array:
$game_ids = array();
while ($row = mysql_fetch_array($result) ) {
$game_ids[] = .$row[1];
}
Now using implode to convert the array to a comma separated values with a comma:
$playerquery = "SELECT username,id FROM players WHERE id IN (" . implode(",",$result) . ") ORDER BY username;