False result SQL request in a code in PHP - php

The SQL's request give three results in PHPMyAdmin and in my code give only one result.
The SQL request must extract three lessons (id,title,year) of school and the PHP script must show all lessons with a link in another page.
Could you help me please ?
Thanks in advance
<?php
$requete_cours="SELECT ID_COURS, COURS.SIGLE, ANNEE FROM COURS, MODULE WHERE COURS.ID_MODULE = MODULE.ID_MODULE AND ID_PERSONNE = $userid";
//echo $requete_cours;
$res = mysqli_query($cxn, $requete_cours);
echo (mysqli_error ($cxn));
$tabLine = array();
while($ligne = mysqli_fetch_array($res)){
echo '<a href="professeur_absences.php?classe=\''.$ligne['ANNEE'].'\'cours=\''.$ligne['SIGLE'].'\'>';
echo $ligne['ANNEE'].' - '.$ligne['SIGLE'].'</a> <br/>';
//$tabLien[$ligne['ID_COURS']] = $ligne['ANNEE'];
}
//var_dump($tabLien);
$_SESSION['tabLien'] = $tabLien;
?
>

Related

SQL query not working but works in PHPMyAdmin

I have a web application and I'm trying to modify one of the queries. The query fetches information (from a table named voyage_list) and returns various fields.
I want to modify the query so that it is based on certain filters the user applies (which will be placed in the URL).
I can't get the query to work in the web application, but if I copy the query and execute it directly within PHPMyAdmin, it works fine.
$vesselFilter = $_GET['vesselFilter'];
$vesselArray = explode(',', $vesselFilter);
$arrayCount = count($vesselArray);
$sqlExtend = ' status = 1 AND';
foreach ($vesselArray as $value) {
$i = $i + 1;
$sqlExtend .= " vesselID = '$value'";
if ($i < $arrayCount){
$sqlExtend .= " OR";
}
}
$newQuery = "SELECT * FROM voyage_list WHERE" . $sqlExtend;
echo $newQuery;
$query = $db->query($newQuery)->fetchAll();
I appreciate the above is pretty messy, but it's just so I can try and figure out how to get the query to work.
Any help would be greatly appreciated!
Thanks
That query probably doesn't return what you think it does. AND takes precedence over OR, so it will return the first vessel in the list if the status is 1, and also any other vessel in the list, regardless of status.
You'd do better to create a query with an IN clause like this:
SELECT * FROM voyage_list WHERE status = 1 AND vesselID IN(8,9,10)
Here's some code to do just that:
$vesselFilter = $_GET['vesselFilter'];
// Validate data. Since we're expecting a string containing only integers and commas, reject anything else
// This throws out bad data and also protects against SQL injection.
if (preg_match('/[^0-9,]/', $vesselFilter)) {
echo "Bad data in input";
exit;
}
// filter out any empty entries.
$vesselArray = array_filter(explode(',', $vesselFilter));
// Now create the WHERE clause using IN
$sqlExtend = 'status = 1 AND vesselID IN ('.join(',', $vesselArray).')';
$newQuery = "SELECT * FROM voyage_list WHERE " . $sqlExtend;
echo $newQuery;
$query = $db->query($newQuery)->fetchAll();
var_dump($query);

How to fetch value from this MySQL query response in PHP

Below is my query. I have commented that I tried in code
$query = "SELECT SUM(paymentAmount) From salepayment WHERE adminUserId ='$adminUserId' AND invoiceId = '$salesInvoiceId' ";
$this->res = $this->db->prepare($query);
$this->result = $this->res->execute();
$countAmount = $this->res->rowCount();
if ($countAmount > 0) {
$sumOfPaymentAmount = $this->res->fetch(PDO::FETCH_OBJ);
echo json_encode($sumOfPaymentAmount);
// $totalPaid = $sumOfPaymentAmount->"SUM(paymentAmount)";
// echo json_encode($totalPaid);
}else{
$paid = 0;
}
Here is my response
{
"SUM(paymentAmount)": "350"
}
I want to get this value into the $paid variable.
Thanks for your help. I am a beginner at PHP and server-side coding so pardon me for my mistakes.
This should be able to be done by adding as paid after SUM(paymentAmount) in the query like so:
SELECT SUM(paymentAmount) as paid From...

Mysqli query doesn't work with id from another table

I have this php script.
$cwZ = count($wiegen_zutat);
$cwM = count($wiegen_menge);
$cwS = count($wiegen_schritt);
if($cwM == $cwS and $cwM == $cwZ and $cwZ == $cwS){
for($x = 0; $x < $cwZ; $x++){
$aktZuat = $wiegenZutat[$x];
$qr = "SELECT ID_Zutat FROM Zutaten WHERE Name='$aktZutat' LIMIT 1";
$id_get = mysqli_query($verbindung,$qr );
$id = mysqli_fetch_array($id_get);
$zuatenID = $id['ID_Zutat'];
echo $id['ID_Zutat'];
echo $zutatenID;
$sql3 = "INSERT INTO Wiegen (ID_Zutat, Menge) VALUES ('$zutatenID', '$wiegenMenge[$x]')";
$wiegenEintragen = mysqli_query($verbindung, $sql3);
}
}
$wiegen_zutat, _menge, _schritt are all three arrays which contain the information from my form.
I go through the first array, and check the variable against a table which contains the ingredients for my website. I want to get the id of a ingredient which was added some steps before and add it into another table.
The problem is that neither the echos or the query are working.
What am I missing?
Please don't get confused by the name of the variables, I'm german :)
Best regards

Issues with Multiple PHP variables in an SQL statement

My problem is i am trying to get 3 variables from the URL, they echo the correct information so i know that my $_GET's are working fine, The first $SC1 and $SC2 both work fine but the 3rd one in the SQL statement Dosnt.
If i replace
$thestatement = ("SELECT * FROM asset_records WHERE a_catagory = '".$SC1."' AND ".$SC2." = '". $SC3 ."' ");
with
$thestatement = ("SELECT * FROM asset_records WHERE a_catagory = '".$SC1."' AND ".$SC2." = 'apple' ");
it works fine but i know $SC3 = apple because i can echo the variable and its apple, please help im pulling my hair out. Heres the rest of the code regarding this.
Note: This is a search function that basis itself on which link people click in my websites menu and takes into consideration when people first click on the website so it displays all items in it.
require ('..\connect_db.php') ;
$SC1 = mysql_real_escape_string($_GET['sc1']); echo $SC1;
$SC2 = mysql_real_escape_string($_GET['sc2']); echo $SC2;
$SC3 = mysql_real_escape_string($_GET['sc3']); echo $SC3;
if ($SC1 && $SC2 && $SC3 = '') {
$thestatement = ('SELECT * FROM asset_records');}
else {;
$thestatement = ("SELECT * FROM asset_records WHERE a_catagory = '".$SC1."' AND ".$SC2." = '". $SC3 ."' ");
}
$result = mysql_query('' .$thestatement. '') or die(mysql_error());
You are emptying $SC3 in the if condition.

get data from sql and show on specific location

I m using the following query to get data from sql :
$query = mysql_query($sql);
while($row = mysql_fetch_array($q))
{
$url = ''.$row['a'].' '.$row['b'].' '.$row['c'].' '.$row['d'].'<br>';
}
now i want to do replace {URL} (that is writen in html file ) with data from sql
$mtheme = str_replace("{URL}",$url,$mtheme);
echo $mtheme;
the problem i that it shows only last row
$url .= ''.$row['a'].' '.$row['b'].' '.$row['c'].' '.$row['d'].'<br>';

Categories