This one is working fine #1 but when I try to relate other tables on the second one, it gives an error
Notice: Undefined index: recipe.id in C:\xampp\htdocs\hyukies\public\samples.php on line 30
Notice: Undefined index: recipe.ingredientid in C:\xampp\htdocs\hyukies\public\samples.php on line 31
RecipeID:
IngredientID:
Notice: Undefined index: recipe.id in C:\xampp\htdocs\hyukies\public\samples.php on line 30
Notice: Undefined index: recipe.ingredientid in C:\xampp\htdocs\hyukies\public\samples.php on line 31
RecipeID:
IngredientID:
Notice: Undefined index: recipe.id in C:\xampp\htdocs\hyukies\public\samples.php on line 30
Notice: Undefined index: recipe.ingredientid in C:\xampp\htdocs\hyukies\public\samples.php on line 31
RecipeID:
IngredientID:
<?php
$sql = mysqli_query($connection, "SELECT id, location FROM location");
$userinfo = array();
while ($row_user = mysqli_fetch_assoc($sql))
$userinfo[] = $row_user;
foreach ($userinfo as $user) {
echo "ID: {$user['id']}<br />"
. "Location: {$user['location']}<br /><br />";
}
?>
This one is not working error problem please help......
<?php
$sql_2 = mysqli_query($connection, "SELECT recipe.id, recipe.ingredientid, ingredients.id, ingredients.quantity, ingredients.`name` FROM recipe INNER JOIN ingredients ON ingredients.id = recipe.ingredientid");
$userinfo_2 = array();
while ($row_user_2 = mysqli_fetch_assoc($sql_2))
$userinfo_2[] = $row_user_2;
foreach ($userinfo_2 as $user_2) {
echo "RecipeID: {$user_2['recipe.id']}<br />"
. "IngredientID: {$user_2['recipe.ingredientid']}<br /><br />";
}
?>
Remove the back ticks from 'name' in your query
$sql_2 = mysqli_query($connection, "SELECT recipe.id, recipe.ingredientid, ingredients.id, ingredients.quantity, ingredients.`name` FROM recipe INNER JOIN ingredients ON ingredients.id = recipe.ingredientid");
Should be
$sql_2 = mysqli_query($connection, "SELECT recipe.id, recipe.ingredientid, ingredients.id, ingredients.quantity, ingredients.name FROM recipe INNER JOIN ingredients ON ingredients.id = recipe.ingredientid");
Also, remove 'recipe' from $user_2['recipe.id']. It should be $user2['id'], but it won't be clear which id you are getting. To selected them independently you can use 'AS' in your query to give them a unique identifier.
SELECT recipe.id AS id1, ingedients.id AS id2
The keys in the associative array just contain the column names, not the table names. So it should be:
echo "RecipeID: {$user_2['id']}<br />"
. "IngredientID: {$user_2['ingredientid']}<br /><br />";
The table names are not included in the result set, so the columns are only named id, ingredientid etc. You really should use as for naming them, like recipe.id as recipeid. There is no warning about there being to id fields and PHP will map them together.
You don't really need both id fields anyway, since you also have the ingredientid, but most probably you will get ingredients.id in the id field also since it's latter.
So update your query to something like
SELECT recipe.id AS recipeid,
recipe.ingredientid,
ingredients.quantity,
ingredients.name
FROM recipe
INNER JOIN ingredients ON ingredients.id = recipe.ingredientid
and then use recipeid to get the recipe.id. This will always be better, since it's clear what id you are getting.
Related
Cheers,
I want to concat multiple rows to one column
Right now, my output is
CaseID Name Number Register date filename Actions
2 John Doe 3/2020 2020-01-20 test1.pdf delete
2 John Doe 3/2020 2020-01-20 test2.pdf delete
3 Jane Doe 5/2020 2020-01-20 test3.pdf delete
I want something like this:
CaseID Name Number Register date filename Actions
2 John Doe 3/2020 2020-01-20 test1.pdf delete
test2.pdf
3 Jane Doe 5/2020 2020-01-20 test3.pdf delete
All-day I have tried different SQL statements, but nothing works.
I'm stuck, I have no idea how to resolve it because I need to use LEFT JOIN for 2 tables:
$sql = "SELECT * FROM files as f
LEFT JOIN cases AS c on f.id_case_f = c.id_case
LEFT JOIN customers as cs on c.id_customer = cs.id_cust
WHERE case_number LIKE '%$case_number%'";
$result = $db -> query($sql);
I have tried with GROUP_CONCAT
I have used the statement below, but it doesn't work.
<?php
$db = mysqli_connect("localhost", "root", "", "testdb");
if ($db -> connect_error){
if($_REQUEST['submit']){
$case_number = $_POST['case_number'];
if(empty($case_number)){
$make = '<h4>You must type a word to search!</h4>';
}else{
$make = '<h4></h4>';
$sql = "SELECT id_case_f, GROUP_CONCAT(filename SEPARATOR ', ') FROM files
LEFT JOIN cases AS c on f.id_case_f = c.id_case
LEFT JOIN customers as cs on c.id_customer = cs.id_cust
WHERE case_number LIKE '%$case_number%'
GROUP BY id_case_f;";
$result = $db -> query($sql);
if($make = mysqli_num_rows($result) > 0)
{ echo '<table>
<tr><th>CaseID</th>
<th>Name </th>
<th>Number </th>
<th>Register date</th>
<th>filename</th>
<th>Actions</th></tr>';
while($row = mysqli_fetch_assoc($result))
{
echo "<tr><td>". $row["id_case"] ." </td><td>". $row["name"] . " </td>
<td>". $row["number"] ."</td> <td> " . $row["register_date"] ." </td>
<td> ". $row["filename"] ." </td><td><a href=#>delete</a></td></tr> ";
}
echo "</table>";
}
else
{
echo'<table "><tr><th>Try again! </th></tr></table>';
print ($make);
}
mysqli_free_result($result);
mysqli_close($db);
}
}
?>
Also, I have used a loooot of other combinations, but unfortunately, nothing worked. :(
If it is necessary I can add the entire PHP code, but this issue is regarding the select statement
Can someone help me with this?
Thank you
LE:
Errors:
Notice: Undefined index: id_case in /// on line 196
Notice: Undefined index: name
Notice: Undefined index: number
Notice: Undefined index: register_date
Notice: Undefined index: filename
SQL GROUP_CONCAT with LEFT JOIN to multiple relative rows
You can try with multiple SELECT statements.
Should work.
Also try to use WHERE EXISTS - https://www.w3schools.com/sql/sql_exists.asp
I have 3 tables tbl_user,tbl_supplier,tbl_subcontractor
I want to select this rows from
tbl_user (db_fname,db_lname),
tbl_supplier(db_CompanyName),
tbl_subcontractor(db_CompanyName)
I'm using this query
SELECT concat(db_fname,' ',db_lname) as fname from tbl_user)
UNION
(SELECT db_CompanyName as scn from tbl_supplier)
UNION
(SELECT db_CompanyName as sucn from tbl_subcontractor)
It give me the correct result but also give me this error
( ! ) Notice: Undefined index: scn in
C:\wamp\www\order\projectmanagment\transferred.php on line 48 Call
Stack #TimeMemoryFunctionLocation 10.0021260912{main}(
)..\transferred.php:0 ( ! ) Notice: Undefined index: sucm in
C:\wamp\www\order\projectmanagment\transferred.php on line 49 Call
Stack #TimeMemoryFunctionLocation 10.0021260912{main}(
)..\transferred.php:0
the result of this query will be display on a select menu like this:
echo'<select name="txt_transferredto" class="states">';
while($row=mysqli_fetch_array($q)){
$fname=$row['fname'];
$companyname=$row['scn'];
$subcompanyname=$row['sucm'];
if($fname!=""){
echo"<option value='$fname'>";echo $fname;echo"</option>";}
else if($subcompanyname!=""){
echo"<option value='$subcompanyname'>";echo $subcompanyname;echo"</option>";}
else if($companyname!=""){
echo"<option value='$companyname'>";echo $companyname;echo"</option>";}
}
echo'</select>';
in this menu appear the result but also the error
i can select from this menu the data take it from tbl_user and do what ever i want but also if i select data take it from tbl_supplier or tbl_subcontractor i can't do anything(update or select or ...)
i test on sql and give me result but i don't know what is this problem
How to solve this Problem
$q=mysqli_query($conn,"SELECT concat(db_fname,' ' , db_lname) as fname from tbl_user ") or die(mysqli_error($conn));
$qq= mysqli_query($conn,"SELECT db_CompanyName as scn from tbl_supplier") or die(mysqli_error($conn));
$qqq= mysqli_query($conn,"SELECT db_CompanyNamee as sucn from tbl_subcontractor") or die(mysqli_error($conn));
echo'<select name="txt_transferred" class="form-control inpu-md">';
echo'<option value="">--SELECT--</option>';
while($row=mysqli_fetch_array($q) and $roww=mysqli_fetch_array($qq) and $rowww=mysqli_fetch_array($qqq)){
$fname=$row['fname'];
$companyname=$roww['scn'];
$subcompanyname=$rowww['sucn'];
if($fname!=""){
echo"<option value='$fname'>";echo $fname;echo"</option>";}
else if($subcompanyname!=""){
echo"<option value='$subcompanyname'>";echo $subcompanyname;echo"</option>";}
else if($companyname!=""){
echo"<option value='$companyname'>";echo $companyname;echo"</option>";}
}
echo'</select>';
I think your query should be:
SELECT concat(db_fname,' ',db_lname) as fname ,
S.db_CompanyName as scn, SC.db_CompanyNam as sucn
FROM tbl_user U, tbl_supplier S, tbl_subcontractor SC
I am trying to left join to tables in PHP. I am a total noob to left join and I can't figure out what I'm doing wrong!
$value=$_GET['value'];
$storeid=$_GET['store'];
$id=$_GET['id'];
$latitude=$_GET['lat'];
$longitude=$_GET['long'];
$result = mysqli_query($con,"SELECT carlist.id, carlist.vin, link_qr.qr, link_qr.vin
FROM link_qr, carlist LEFT JOIN link_qr.vin ON carlist.vin
WHERE qr="$value";");
while($row = mysqli_fetch_array($result)) {
echo $row['id'];
echo $row['vin'];
echo $row['qr'];
}
Here is the table structure
Table: link_qr
id------vin---------qr------webid---------other
Table: carlist
id---stknum---vin----vt----stat---other---store_id---web_code---qrcode
When all done I would like to have the following.
I would like to join the carlist and the link_qr where the vins are equal to each other and then I need it to return the carlist id where that vin is equal to qr.
Here are the errors I'm getting:
**Notice: Undefined index: store in /api/app_request/left_join.php on line 13
Notice: Undefined index: id in /api/app_request/left_join.php on line 14
Notice: Undefined index: lat in /api/app_request/left_join.php on line 15
Notice: Undefined index: long in /api/app_request/left_join.php on line 16
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in api/app_request/left_join.php on line 22**
There are a couple things to note:
First, your SQL query is incorrect:
SELECT carlist.id, carlist.vin, link_qr.qr, link_qr.vin
FROM carlist
LEFT JOIN linkqr ON linkqr.vin=carlist.vin
WHERE qr="$value";
Should be the correct format so long as those tables and columns exist. Secondly, however, you should not be querying a database with an unescaped value. This leads to SQL Injection. More appropriately you could write your query like:
$query = <<<SQL
SELECT carlist.id, carlist.vin, link_qr.qr, link_qr.vin
FROM carlist
LEFT JOIN linkqr ON linkqr.vin=carlist.vin
WHERE qr=?
SQL;
$stmt = mysqli_prepare($query);
mysqli_bind_param($stmt, "s", $value); // this sets the ? in the sql query to $value
mysqli_execute($stmt);
$result = mysqli_get_result($stmt);
while($row = mysqli_fetch_array($result)) {
echo $row['id'];
echo $row['vin'];
echo $row['qr'];
}
Why not do:
"SELECT C.PrimaryId, Field, AnotherField
FROM tablename AS C
LEFT JOIN tablename AS L ON C.matching_id = T.matching_id
WHERE tablename.fieldname = :fieldname
?
I use codeigniter calendar class and i am trying to display all reservations for the current month. In the field where the day is, I may have more than one result,and I would like to display them coma separated.
However, I get the following error message:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: stdClass::$reservationID
Filename: models/reservationcalendarmodel.php
Line Number: 81
A PHP Error was encountered
Severity: Notice
Message: Undefined property: stdClass::$reservationArrivalDate
Filename: models/reservationcalendarmodel.php
Line Number: 81
Error is displayed 10 times ( i expect 9 records from the database)
Here is the (relevant) code from the model:
function getReservations($year,$month) {
$currentHotelUser = $this->session->userdata('currentHotelUser');
$query = $this->db->query(
"SELECT r.*
FROM room r
LEFT JOIN (SELECT rr.roomID, re.reservationID,re.reservationArrivalDate
FROM reservationroom rr
LEFT JOIN reservation re ON re.reservationID = rr.reservationID
WHERE re.reservationArrivalDate LIKE('".$year."-".$month."-%'))
sq ON sq.roomID = r.roomID
WHERE r.hotelID = ".$currentHotelUser['hotelID']."
AND sq.reservationID IS NOT NULL
");
$cal_data = array();
foreach($query->result() as $row) {
// BELLOW IS THE LINE 81 WHERE THE ERROR IS
$cal_data[substr($row->reservationArrivalDate,8,2)]= $row->reservationID;
}
return $cal_data;
}
function generate($year,$month) {
$cal_data = $this->getReservations($year, $month);
$this->load->library('calendar', $this->conf);
return $this->calendar->generate($year,$month,$cal_data);
}
Here is the (relevant) code from the controller:
$this->load->model('ReservationCalendarModel');
$data['calendar'] = $this->ReservationCalendarModel->generate($year,$month);
Here is the (relevant) code from the view:
echo $calendar;
Did anyone know why do I get the above error?
The problem is you query results not having reservationID and reservationArrivalDate. That is the reason, while looping through you are getting this error. Add these two columns in your main select query
"SELECT r.*, sq.roomID, sq.reservationId
FROM room r
LEFT JOIN (SELECT rr.roomID, re.reservationID,re.reservationArrivalDate
FROM reservationroom rr
LEFT JOIN reservation re ON re.reservationID = rr.reservationID
WHERE re.reservationArrivalDate LIKE('".$year."-".$month."-%'))
sq ON sq.roomID = r.roomID
WHERE r.hotelID = ".$currentHotelUser['hotelID']."
AND sq.reservationID IS NOT NULL
"
I want to extract up to five values from an array and place them in a msql query as such:
$frontpage_hot_list_data = array();
while (#$row = mysql_fetch_array($sql_frontpage_hot_list)) {
$frontpage_hot_list_data[] = $row['id'];
}
$sql_frontpage_hot_down = mysql_query("SELECT * FROM submissions WHERE
id !='$frontpage_hot_list_data[0]' AND id !='$frontpage_hot_list_data[1]' AND
id !='$frontpage_hot_list_data[2]' AND id !='$frontpage_hot_list_data[3]' AND
id !='$frontpage_hot_list_data[4]' AND thumbnail_large=0 AND popular=1 AND
popular_datetime > '$frontpage_hot_hot_three_onlineNowTm' AND
views > '$frontpage_hot_hot_three_views' ORDER BY views DESC LIMIT 4");
The problem here appears to be when I have less than five values, I get the following error:
Notice: Undefined offset: 1 in
D:\Hosting\8847501\html\scripts\timeframes.php on line 298
Notice: Undefined offset: 2 in
D:\Hosting\8847501\html\scripts\timeframes.php on line 299
Notice: Undefined offset: 3 in
D:\Hosting\8847501\html\scripts\timeframes.php on line 300
Notice: Undefined offset: 4 in
D:\Hosting\8847501\html\scripts\timeframes.php on line 301
Any idea how to solve this problem? Maybe placing in the query only the exact number of variables? Im lost...
You can use implode to create a comma-separated list of your IDs, then put this newly created list into a MySQL IN expression (UPDATE: now I see that you're using != in your query, so let's make it NOT IN).
$list = implode(',', $frontpage_hot_list_data);
$sql_frontpage_hot_down = mysql_query("SELECT * FROM submissions WHERE
id NOT IN ($list) AND thumbnail_large=0 AND popular=1 AND
popular_datetime > '$frontpage_hot_hot_three_onlineNowTm' AND
views > '$frontpage_hot_hot_three_views' ORDER BY views DESC LIMIT 4");
Important note: I assumed here that your ids are numeric, so implode() would produce something like 1,5,133,31. If they are strings, then you have to wrap them in apostrophes first using array_map() for example.
Also, if you expect that the array can be empty, you can add another condition that will omit the whole id NOT IN () AND part when necessary.
It seems that there's no $sql_frontpage_hot_list result, because array $frontpage_hot_list_data[] does not contain any of results
I would do following:
$frontpage_hot_list_data = array();
while (#$row = mysql_fetch_array($sql_frontpage_hot_list)) {
$frontpage_hot_list_data[] = $row['id'];
}
if(count($frontpage_hot_list_data))
{
$frontpage_ids_str = implode($frontpage_hot_list_data, ',');
$sql_frontpage_hot_down = mysql_query("SELECT * FROM submissions WHERE
id not in (".$frontpage_ids_str.") AND thumbnail_large=0 AND popular=1 AND
popular_datetime > '$frontpage_hot_hot_three_onlineNowTm' AND
views > '$frontpage_hot_hot_three_views' ORDER BY views DESC LIMIT 4");
}
else
{
$sql_frontpage_hot_down = mysql_query("SELECT * FROM submissions WHERE
thumbnail_large=0 AND popular=1 AND
popular_datetime > '$frontpage_hot_hot_three_onlineNowTm' AND
views > '$frontpage_hot_hot_three_views' ORDER BY views DESC LIMIT 4");
}
So if we have $frontpage_hot_list_data we exclude them, if not then we ignore them.