JSON_ENCODE for two columns with same name in php - php

my problem is i want to JSON_ENCODE the result of inner join query and the two columns i want to select have the same name so, the JSON object override one of them and carry only data for one column cause they have the same name,,this is my code till now.
$query = "select faculty.NAME,sector.NAME from faculty inner join sector
on faculty.SECTOR_ID=sector.ID";
$result = mysql_query($query);
while($r = mysql_fetch_assoc($result)) {
$rows[] = $r;
}
echo json_encode($rows);
how to do this without change the column name in the DataBase...

Try changing the output of your query:
select faculty.NAME AS facultyName,sector.NAME AS sectorName from faculty inner join sector
on faculty.SECTOR_ID=sector.ID

Using as:
select faculty.NAME as faculty_name, sector.NAME as sector_name from faculty inner join sector
on faculty.SECTOR_ID=sector.ID
This will change your json values to something like:
{"faculty_name": "first", "sector_name": "second"}
so you will need to update your javascript.

You can use ALIAS In your query so you will have different names for your columns
select faculty.NAME as faculty_name ,sector.NAME as sector_name

Related

PHP PDO - Get same field name values of three tables in join

query = SELECT * FROM design_trip,meal_pref,types where design_trip.meal_id = meal_pref.meal_id and design_trip.triptype_id = types.type_id;
There is two columns called "name_en", when i write in foreach
<?php echo $value->budget; ?>
its getting just first table value.
I'm using this in PDO query to get values from 3 table join, how can i print this values separately?
You could give separate names when selecting for the fields. Like
SELECT design_trip.meal_id as designtripmealId
FROM design_trip,meal_pref,types
where design_trip.meal_id = meal_pref.meal_id
and design_trip.triptype_id = types.type_id;
To get all the fields incase you don't want to specify the rest of the fields, you can use table_name.*.
try this
SELECT * FROM table1
JOIN table2 ON table1.userid = table2.userid
WHERE table1.userid = 1

Why do i double my display in json

So i fetch my data from two tables in my php and encode it in one json object. I got everything i needed except that it doubles the display. And my teamone is located in the matches tables. instead of starting from array 0, it starts after the schedules tables. Which is array 7. I dont know why this happen.
Here is my php.
$sql = "SELECT * from schedule, matches;";
$con = mysqli_connect($server_name,$mysql_user,$mysql_pass,$db_name);
$result = mysqli_query($con,$sql);
$response = array();
while($row=mysqli_fetch_array($result))
{
array_push($response, array("start"=>$row[4],"end"=>$row[5],"venue"=>$row[6], "teamone"=>$row[8], "teamtwo"=>$row[9],
"s_name"=>$row[17]));
}
echo json_encode (array("schedule_response"=>$response));
mysqli_close($con);
?>
Here is my display. As you can see there are four displays but in my database it only has 2. It doubles the display. How do i fix this? Thanks
{ "schedule_response":[
{"start":"2016-11-10 00:00:00","end":"2016-11-04 00:00:00","venue":"bbbb","teamone":"aaa","teamtwo":"bbb",
"s_name":"hehehe"},
{"start":"2016-11-23 00:00:00","end":"2016-11-24 00:00:00","venue":"bbbbbbbb","teamone":"aaa","teamtwo":"bbb",
"s_name":"hehehe"},
{"start":"2016-11-10 00:00:00","end":"2016-11-04 00:00:00","venue":"bbbb","teamone":"ehe","teamtwo":"aha",
"s_name":"aaa"},
{"start":"2016-11-23 00:00:00","end":"2016-11-24 00:00:00","venue":"bbbbbbbb","teamone":"ehe","teamtwo":"aha",
"s_name":"aaa"}]}
I need to get the teamone, teamtwo and s_name values from the matches while i need the start, end and the venue from the schedule table in one query.
Schedule table
Matches Table
Because of your SQL query, you should not forget to perform some form of a grouping (the way you select results from both table defines that):
$sql = "SELECT * from schedule s, matches m GROUP BY s.id"; //I assume that your table schedule has an `id`
Or, you can rework the query to be more readable:
$sql = "SELECT s.*, m.* FROM schedule s
INNER JOIN matches m ON m.schedule_id = s.id
GROUP BY s.id"; //I assume that you have such database design that you have defined foreign key on table `matches`.
Of course, the INNER JOIN above could be LEFT OUTER JOIN - it all depends on your database design.
I think it is the problem with mysqli_fetch_array().
Can you please change mysqli_fetch_array() to mysqli_fetch_assoc()

echoing data From mySQL when table column joined twice

I am trying to echo both the employee name and manager name
SQL QUERY:
SELECT *
FROM `form`
INNER JOIN `emp` AS employee
ON `form`.emp_ID = employee.emp_ID
INNER JOIN `emp` AS manager
ON `form`.manager_ID = manager.emp_ID
ECHO:
while($row = $result->fetch_assoc()){
echo $row['emp_name'];
}
Always outputs the managers name.
have tried the following:
$row['employee.emp_name']
$row['employee']['emp_name']
which all don't work.
any help is appreciated
When the columns have the same name, their values will overlap when retrieving by fetch_assoc(). You can either use fetch_array() and reference the columns by numeric index (not recommended with SELECT *, since you can't easily guarantee the order of columns), or you will have to list the column names explicitly and alias them. For example:
SELECT emp.emp_name emp_emp_name, manager.emp_name manager_emp_name, ...
This would give you separate distinctly named fields in the result, that you could then access from what fetch_assoc() returns.

How can I display columns with the same name from two different tables?

I have made an SQL query from two tables. Everything works good but problem is that this two tables have the same field names and after I do not know how to display them correct, how to tell that $data['aaa'] is from table 1 and the same $data['aaa'] from table 2
here is my SQL query :
$query_str = "SELECT
cm.id,
cm.global_category_id,
cm.num,
cm.menu_lv,
cm.menu_ru,
cm.menu_en,
u.id,
u.menu_lv,
u.menu_ru,
u.menu_en
FROM products_category cm, products_global_category u
WHERE cm.global_category_id = u.id
";
and display data
<? foreach ($sub_category_list as $line) : ?>
<tr>
<td><?=$line['menu_lv']?></td> <---- here I want to display data from products_global_category u
<td><?=$line['sub_menu_lv']?></td> <---- products_category cm
<td><?=$line['sub_menu_ru']?></td> <---- products_category cm
<td><?=$line['sub_menu_en']?></td> <---- products_category cm
</tr>
<? endforeach; ?>
As a solution you can change your SQL query to give an alias to the fields that have the same name.
For example:
SELECT somefield AS othername FROM table.
In this case, the somefield field will be available through the alias othername.
In your case:
$query_str = "SELECT
cm.id AS cm_id,
cm.global_category_id,
cm.num,
cm.menu_lv AS cm_menulv,
cm.menu_ru AS cm_menuru,
cm.menu_en AS cm.menuen,
u.id as u_id,
u.menu_lv AS u_menulv,
u.menu_ru AS u_menuru,
u.menu_en AS u_menuen
FROM products_category cm, products_global_category u
WHERE cm.global_category_id = u.id
";
And then in your PHP:
$line['u_menulv'] //Access field menu_lv from products_global_category table
$line['cm_menulv'] //Access field menu_lv from products_category table
EDIT: In mysql_fetch_array documentation page:
If two or more columns of the result have the same field names, the
last column will take precedence. To access the other column(s) of the
same name, you must use the numeric index of the column or make an
alias for the column. For aliased columns, you cannot access the
contents with the original column name.
In other words, either create an alias like shown above or access the fields by the numeric index of the array.
Simply alias the records you wish to print out which conflict with one another, for example:
SELECT
table1.pid AS page_id,
table2.pid AS product_id
FROM
table1
LEFT JOIN
table2
ON
table1.id = table2.id
And then within your PHP you can echo them out as follows:
while ($row = mysql_fetch_assoc($res)) {
echo $row['page_id'];
echo $row['product_id'];
}
Hope this helps!

How to select value from a drop-down list, get the corresponding values from another table, then put those into another drop-down list?

Ok, here's a database.
http://i.stack.imgur.com/j05AB.png
Say I've inserted values into the database for each of these tables, although the IDs would be auto incrementing. There are many BVALUES from each AVALUE, thus the AB table. I have all the AVALUEs from TABLE A in a drop-down list. A user selects an AVALUE which I put into a variable using
$AVALUE = $_POST['AVALUE']
Then I do an sql statement to get the AVALUEs from TABLE A that equal $AVALUE.
$sql = "SELECT AVALUE FROM TABLEA WHERE" . $AVALUE . " = AVALUE";
How do I then get the NAMEID from TABLEA for that AVALUE, then reference to AB where TABLEANAMEID = NAMEID from TABLEA? Then I want to get all the BVALUES by getting all the TABLEBNAMEIDs that correspond to the TABLEANAMEIDs.
I then want those BVALUES in a drop-down list on a seperate HTML page. After a bit of Googling the solution I think would be to do some sort of a loop putting the BVALUES into a variable as all the NAMEIDs from TABLE B increment where the variable would be in an $BVALUE loop and the list values would show with all the BVALUES.
I hope I explained that right. I think I know what I'm trying to do but I have no idea how to actually implement it. Please help guys.
You need to join those table together. What you are describing is an m:n relation. In this case you have do use 2 joins like this:
SELECT * FROM TableA AS a WHERE a.avalue = $AVALUE
JOIN TableAB AS a2b ON a.namevalue = a2b.id_a
JOIN TableB AS b ON a2b.id_b = b.id
Maybe u means, u want to get all BVALUE which has relation with selected AVALUE in table AB
$sql = "SELECT B.NAMEID as id, BVALUE as value FROM TABLEA A
LEFT JOIN AB ON TABLEANAMEID=A.NAMEID
LEFT JOIN TABLEB B ON TABLEBNAMEID=B.NAMEID
WHERE AVALUE = $AVALUE";`
get mysql result
$result = mysql_query($sql);
iterate
echo "<select>";
foreach ($r = mysql_fetch_object($result)) {
echo '<option value="'.$r->id.'">'.$r->value.'</option>';
}
echo "</select>";

Categories