GOAL:
Select from the database to target a team name from one of my five specific columns. the table has a number of columns 5 of which are headed respondent_team_1 to respondent_team_5. In each of those columns there maybe a string value that is repeated so the idea is to look at that columns and know this and just take the 1 distinct value and not all so I can match up the data like so:
CODE:
<?php
$sql = "SELECT distinct respondent_team_1 FROM respondent_data WHERE dashboard_id = $user_dash_id";
$result = $conn->query($sql);
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
echo $row["respondent_team_1"];
}
}
?>
<br>
<?php
$sql = "SELECT distinct respondent_team_2 FROM respondent_data WHERE dashboard_id = $user_dash_id";
$result = $conn->query($sql);
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
echo $row["respondent_team_2"];
}
}
?>
<br>
<?php
$sql = "SELECT distinct respondent_team_3 FROM respondent_data WHERE dashboard_id = $user_dash_id";
$result = $conn->query($sql);
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
echo $row["respondent_team_3"];
}
}
?>
<br>
<?php
$sql = "SELECT distinct respondent_team_4 FROM respondent_data WHERE dashboard_id = $user_dash_id";
$result = $conn->query($sql);
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
echo $row["respondent_team_4"];
}
}
?>
<br>
<?php
$sql = "SELECT distinct respondent_team_5 FROM respondent_data WHERE dashboard_id = $user_dash_id";
$result = $conn->query($sql);
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
echo $row["respondent_team_5"];
}
}
?>
SUMMARY:
What I am trying to do is refine all those code blocks into one code block as A it is messy and B there must be a better way but I am unable to find one?
I have tried various methods, the obvious being take the column names I need and make a select against them, I tried to use the GROUP BY keyword with no luck either. Im not looking for somebody to do this for me but just some wisdom or advice would be excellent.
Related
Is it possible to extract highest id from table (in this case 9) and return it as variable $maximum, which I can use later as integer?
You can use MAX() function. Doc can be found here
Try this, It will must work.
$sql = "SELECT id FROM some_table ORDER BY id DESC LIMIT 1";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "id = " . $row["id"];
}
}
You can try with following snippet:
$sql = "SELECT * FROM some_table where id = (select max(id) from some_table)";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "id = " . $row["id"];
}
}
Try this, It will must work.
$sql = "SELECT id FROM some_table ORDER BY id DESC LIMIT 1";
$result = $conn->query($sql);
$id = 0;
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$id = $row["id"];
}
}
echo $id;
I have been trying to compare value in a table with another value in another table but only the else part is executing.
<?php
$sql = "SELECT * FROM user WHERE user_id=$row[user_id]";
$result = $conn -> query ($sql);
if ($result -> num_rows > 0) {
while ($row = $result ->fetch_assoc()) {
$sql1 = "SELECT * FROM jobpost WHERE jobpost_id=$_GET[id] ";
$result1 = $conn -> query ($sql1);
$row_count = mysqli_num_rows($result);
$row1_count = mysqli_num_rows($result1);
$remaining_rows = min($row_count, $row1_count);
$row = mysqli_fetch_assoc($result);
$row1 = mysqli_fetch_assoc($result1);
if($row["experience"] > $row1["experience"])
{
//some code to display something
echo "1";
}
else
{
echo "2";
}
} }
?>
Welcome to SO, it is hard to read your source code. You can use a simple query like this:
SELECT IF(u.experience > j.experience,1,0) FROM
(
(SELECT experience FROM user WHERE user_id = 1) u
JOIN
(SELECT experience FROM jobpost WHERE jobpost_id = 8) j
)
Try this query here: http://sqlfiddle.com/#!9/1742c0
Please check our datasource. Maybe the else case is correct.
I want to display hospital names from database by two ways:
By Selecting city.
By entering the name of hospital in search bar.
I wrote below php script. The 1st part works fine and shows all hospitals from a selected city, but 2nd part doesn't work for me. There is no error displayed. Need help with this. Did i not put the 'if' conditions in the right place? Or I missed something else?
if (isset($_POST['search'])) {
if (isset($_POST['search-by-city'])) {
$city_id = $_POST['search-by-city'];
$query = "SELECT * FROM `hospitals` WHERE `City_ID` LIKE '%$city_id%'";
$result = mysqli_query($con,$query);
if (mysqli_num_rows($result) == 0) {
echo '<h2>No recod Found</h2>' ;
}
}
if (isset($_POST['search-by-name'])) {
$hospital_name = $_POST['search-by-name'];
$query = "SELECT * FROM `hospitals` WHERE `Name` LIKE '%$hospital_name%'";
$result = filterTable($query); {
if (mysqli_num_rows($result) == 0) {
echo '<h2>No recod Found</h2>' ;
}
}
while($row = mysqli_fetch_array($result)){
$city_id = $row[3];
$query = "SELECT `Name` FROM `cities` WHERE `ID` LIKE '$city_id'";
$result2 = mysqli_query($con,$query);
$row2 = mysqli_fetch_row($result2);
$city_name = $row2[0];
echo '
<h3>'.$row[1].'</h3>
<h4>'.$city_name.'</h4>
';
}
}
How to Count number of rows in a table that matches to the related condition and echo that count out.
Code goes as follows::
<?php
$sql = "SELECT * FROM input ORDER BY date DESC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$myid = $row["id"] ;
$sql3 = "SELECT COUNT question_id FROM output WHERE question_id = $myid";
$result3 = $conn->query($sql3);
?>
<div id="q">
<small><p><?php echo $row["date"]; ?></p></small>
<p id="tag3"><small><?php echo $result3['']; ?></small></p>
</div>
Any Suggestions will be appreciated..
Quotes missing
$sql3 = "SELECT * question_id FROM output WHERE question_id = '".$myid."'";
Secondly i dont see closing braces for while
<?php
$sql = "SELECT * FROM input ORDER BY date DESC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$myid = $row["id"] ;
$sql3 = "SELECT COUNT(*) as rowCount FROM output WHERE question_id = '".$myid."'";
$result3 = $conn->query($sql3);
$rowCount= $result3->fetch_assoc();
?>
<div id="q">
<small><p><?php echo $row["date"]; ?></p></small>
<p id="tag3"><small><?php echo $rowCount['rowCount']; ?></small></p>
</div>
<?php }//while
} //if
?>
You need to use COUNT(question_id) function as below
$sql3 = "SELECT COUNT(question_id ) as TotalQuestions FROM output WHERE question_id = $myid";
$result3 = $conn->query($sql3);
Then to fetch result from above query use fetch_fieled()
while ($info = $result3->fetch_field()) {
$TotalCount = $info->TotalQuestions ;
}
And then display the value
<small><?php echo $TotalCount ; ?></small
try this
$sql3 = "SELECT COUNT(1) as row_count FROM output WHERE question_id = ".$myid."";
$result3 = $conn->query($sql3);
$row_count = $result3->fetch_assoc();
echo $row_count['row_count'];
I have a page that is taking a kind of long time to load, and I'm almost sure that this is caused by too many sql requests (AKA caused by my bad SQL skills). Is there anyway to join these 3 queries into one?
What I want to do with this query is to try to select a specific id from cardapios and, if there is anything there (if $num_rows > 0) the only thing I want to do is select that id. If there is nothing there, then I want to insert something and then select the id of that.
$query = "SELECT id FROM cardapios WHERE nome='$nome'";
$sql = mysqli_query($con,$query);
$num_rows = mysqli_num_rows($sql);
if ($num_rows > 0){
while ($row = mysqli_fetch_array($sql)){
$_SESSION['id_cardapio'] = $row['id'];
$num_rows = 0;
}}else{
$query = "INSERT INTO cardapios (nome, kcal, semana)
VALUES('$nome', '$kcal', '$semana')" or die(mysqli_error($con));
$sql = mysqli_query($con,$query);
$query = "SELECT id FROM cardapios WHERE nome='$nome' ";
$sql = mysqli_query($con, $query);
while ($row = mysqli_fetch_array($sql)){
$_SESSION['id_cardapio'] = $row['id'];
}
}
I am trying to put all of this into one query but getting nowhere. Is there anyway to use just one query for doing all of this?
Thanks in advance!
You can replace the last query by getting the mysqli_insert_id($con); as you already have the insert id available after the insert
$query = "SELECT id FROM cardapios WHERE nome='$nome'";
$sql = mysqli_query($con,$query);
$num_rows = mysqli_num_rows($sql);
if ($num_rows > 0){
while ($row = mysqli_fetch_array($sql)){
$_SESSION['id_cardapio'] = $row['id'];
$num_rows = 0;
}
}else{
$query = "INSERT INTO cardapios (nome, kcal, semana)
VALUES('$nome', '$kcal', '$semana')" or die(mysqli_error($con));
$sql = mysqli_query($con,$query);
if ( $sql !== false) { // did insert work
$_SESSION['id_cardapio'] = mysqli_insert_id($con);
} else {
// insert did nto work??
}
}