I have this code:
$sql='SELECT id FROM table1 WHERE "'.$date.'"=Date';
$result = mysqli_query($connect, $sql) or die(mysqli_error($connect));;
while($row = mysqli_fetch_array($result)){
$c=$row['id'];
}
This returns more than 1 value. How can i use the different values for another query?
Ex:
$sql2='SELECT name FROM TABLE2 WHERE name="'.$c.'" ';
PS: i know the code is not good
Is this what you mean? Not 100% sure but hope it helps ...
$sql='SELECT id FROM table1 WHERE Date = '.$date;
$result = mysqli_query($connect, $sql) or die(mysqli_error($connect));;
while($row = mysqli_fetch_array($result)){
$sql2='SELECT name FROM TABLE2 WHERE name="'.$row['id'].'" ';
$sub_result = mysqli_query($connect, $sql2) or die(mysqli_error($connect));
}
I m not sure if this s what you need but here we go:
$sql='SELECT id FROM table1 WHERE Date="'.$date.'"';
$result = mysqli_query($connect, $sql) or die(mysqli_error($connect));;
while($row = mysqli_fetch_array($result)){
$c=$row['id'];
$sql2='SELECT name FROM TABLE2 WHERE name="'.$c.'" ';
//DO SOMETHING HERE
}
Or with LEFT JOIN
$sql='SELECT table1.something, TABLE2.something2
FROM table1
LEFT JOIN TABLE2
ON table1.id=TABLE2.name
WHERE table1.Date="'.$date.'"';
$result = mysqli_query($connect, $sql) or die(mysqli_error($connect));;
while($row = mysqli_fetch_array($result)){
//DO SOMETHING HERE
}
Related
I have case manager table where i have inserted court table id as foreign key. i want to fetch record from both tables. when using nested while loop it shows only one row data.
$id = $_SESSION['id'];
$query1 = "SELECT * from `case_manager` where user_id = '$id' ";
$result1 = mysqli_query($conn, "$query1");
while($row = mysqli_fetch_array($result1, MYSQLI_ASSOC)) {
$Status = $row['status'];
$id = $row['id'];
$case_type = $row['case_type'];
$court_id = $row['court_id'];
$query2 = "SELECT * from `case_type` where case_id = '$case_type'";
if($result1 = mysqli_query($conn, "$query2")) {
while($row2 = mysqli_fetch_array($result1, MYSQLI_ASSOC)) {
echo $row2['case_name'];
}
}
}
Because you are overwritting you $result1 change inner query result to $result2 then try
$id = $_SESSION['id'];
$query1 ="SELECT * from `case_manager` where user_id = '$id' ";
$result1 = mysqli_query($conn , "$query1");
while ($row = mysqli_fetch_array($result1 ,MYSQLI_ASSOC)) {
$Status=$row['status'];
$id = $row['id'];
$case_type = $row['case_type'];
$court_id = $row['court_id'];
$query2 ="SELECT * from `case_type` where case_id = '$case_type'";
if($result2 = mysqli_query($conn , "$query2")){;
while ($row2 = mysqli_fetch_array($result2 ,MYSQLI_ASSOC)) {
echo $row2['case_name'];
}
}
}
1st : Because you are overwriting the variable $result1 In second query execution.
if($result1 = mysqli_query($conn , "$query2")){;
^^^^^^^^ ^^
Note : And remove that unnecessary semicolon .
2nd : No need multiple query simple use join
SELECT cm.*,c.* from `case_manager` cm
join `case_type` c
on cm.cas_type=c.case_id
where cm.user_id=$id;
You can use below query to fetch your record:
$query = SELECT case_manager.* ,case_type.case_name FROM case_manager Left JOIN case_type ON case_manager.case_type=case_type.case_id where case_manger.user_id = $id;
While($row = mysql_fetch_array()){
echo $row['case_name'];
}
This is the code I have at the moment which works fine:
<?php
$sql = "SELECT * FROM te_events order by eventTitle ASC ";
$result = $conn->query($sql);
while($row = $result->fetch_assoc())
{
$venueID = $row['venueID'];
$catID = $row['catID'];
$sql2 = "SELECT * FROM te_venue where venueID='$venueID'";
$result2 = $conn->query($sql2);
while($row2 = $result2->fetch_assoc())
{
$venueName = $row2['venueName'];
}
$sql3 = "SELECT * FROM te_category where catID='$catID'";
$result3 = $conn->query($sql3);
while($row3 = $result3->fetch_assoc())
{
$catName = $row3['catDesc'];
}
?>
But I want to Change it into this format. I could do only till this bit couldn't go further than this I get errors.
<?php
$sql ="SELECT eventTitle, eventID, venueID, catID, eventStartDate, eventEndDate, eventPrice FROM te_events ORDER BY eventTitle ASC";
$queryresult = mysqli_query($conn, $sql) or die(mysqli_error($conn));
while ($row = mysqli_fetch_array($queryresult)) {
$venueID = $row['venueID'];
$catID = $row['catID'];
$venueName = $row['venueName'];
$catName = $row['catDesc'];
?>
How can I do that then?
how can I join two tables?
You should be able to join the additional 2 tables to get the columns you need.
SELECT e.eventTitle, e.eventID, e.venueID, e.catID, e.eventStartDate, e.eventEndDate, e.eventPrice, v.venueName, c.catDesc
FROM te_events as e
join te_venue as v
on e.venueID = v.venueID
join te_category as c
on c.catID = e.catID
ORDER BY eventTitle ASC
You also should avoid putting data directly into a query. If you need to do that use parameterized queries. This is how SQL injections (or second level) occur.
Following is my code,
$result1 = "SELECT emp_id FROM employee where manager_id=".$userID;
$array = mysql_query($result1);
$cnt = 0;
while ($row = mysql_fetch_array($array)) {
"emp_id: " . $row[0];
$myArrayOfemp_id[$cnt] = $row[0];
$cnt++;
}
var_dump($myArrayOfemp_id);
$sql = "SELECT emp_id FROM emp_leaves WHERE emp_id='$myArrayOfemp_id' ORDER BY apply_date DESC";
$result = mysql_query($sql);
$total_results = mysql_num_rows($result);
When I'am trying to use $myArrayOfemp_id variable in $sql query, It shows that error:
Array to string conversion in..
How can I fix it?
You are trying to convert an array into a string in the following line:
$sql = "SELECT emp_id FROM emp_leaves
WHERE emp_id='$myArrayOfemp_id' ORDER BY apply_date DESC";
$myArrayOfemp_id is an array. That previous line of code should be changed to:
$sql = "SELECT emp_id FROM emp_leaves
WHERE emp_id={$myArrayOfemp_id[0]} ORDER BY apply_date DESC";
I placed 0 inside {$myArrayOfemp_id[0]} because I'm not sure what value want to use that is inside the array.
Edited:
After discussing what the user wanted in the question, it seems the user wanted to use all the values inside the array in the sql statement, so here is a solution for that specific case:
$sql = "SELECT emp_id FROM emp_leaves
WHERE ";
foreach ($myArrayOfemp_id as $value)
{
$sql .= " emp_id={$value) || ";
}
$sql .= "1=2";
$result = mysql_query($sql);
$total_results = mysql_num_rows($result);
$sql = "SELECT emp_id FROM emp_leaves WHERE emp_id in
(SELECT GROUP_CONCAT(emp_id) FROM employee where manager_id=".$userID.")
ORDER BY apply_date DESC";
$result = mysql_query($sql);
$total_results = mysql_num_rows($result);
just change your query like above might solve your problem.
you can remove following code now. :)
$result1 = "SELECT emp_id FROM employee where manager_id=".$userID;
$array = mysql_query($result1);
$cnt = 0;
while ($row = mysql_fetch_array($array)) {
"emp_id: " . $row[0];
$myArrayOfemp_id[$cnt] = $row[0];
$cnt++;
}
var_dump($myArrayOfemp_id);
I have two tables "BANDS" & "adres". In table "BANDS" there is a BOEKERID which is the same ID as the corresponding ID in "adres".
The bands is always different ofcourse and the BOEKER can be the same. Also because of adress change i have the 2 dfferent tables.
Now, i tried this to get the info:
$sql1 = "SELECT BANDID, NAAMBAND, CONTACTBAND, BOEKERID FROM `BANDS` ORDER BY $field $sort";
$sql2 = "SELECT ID, NAAM FROM `adres` WHERE ID = $BOEKERID";
$result1 = mysql_query($sql1) or die(mysql_error());
$result2 = mysql_query($sql2) or die(mysql_error());
while($row1 = mysql_fetch_array($result1))
while($row2 = mysql_fetch_array($result2))
echo'<tr>
<td>'.$row1['BANDID'].'</td>
<td>'.$row1['NAAMBAND'].'</td>
<td>'.$row1['CONTACTBAND'].'</td>
<td>'.$row1['BOEKERID'].'</td>
<td>'.$row2['NAAM'].'</td>';
Somebody can help me ?
If i use your sql :
$sql1 = "SELECT BANDID, NAAMBAND, CONTACTBAND, BOEKERID FROM `BANDS` ORDER BY $field $sort";
$result1 = mysql_query($sql1) or die(mysql_error());
while($row1 = mysql_fetch_array($result1)){
echo'<tr>
<td>'.$row1['BANDID'].'</td>
<td>'.$row1['NAAMBAND'].'</td>
<td>'.$row1['CONTACTBAND'].'</td>
<td>'.$row1['BOEKERID'].'</td>';
$sql2 = "SELECT ID, NAAM FROM `adres` WHERE ID = $row1['BOEKERID']";
$result2 = mysql_query($sql2) or die(mysql_error());
while($row2 = mysql_fetch_array($result2)){//change this while if $sql2 all time return only 1 result
echo '<td>'.$row2['NAAM'].'</td>';
}
echo'</tr>';
}
With join (http://sql.sh/cours/jointures/inner-join):
$sql1 = "SELECT NAAM,BANDID, NAAMBAND, CONTACTBAND, BOEKERID FROM `BANDS` INNER JOIN `adres` WHERE `BANDS`.BOEKERID = `adres`.id ORDER BY $field $sort";
$result1 = mysql_query($sql1) or die(mysql_error());
while($row1 = mysql_fetch_array($result1)){
echo'<tr>
<td>'.$row1['BANDID'].'</td>
<td>'.$row1['NAAMBAND'].'</td>
<td>'.$row1['CONTACTBAND'].'</td>
<td>'.$row1['BOEKERID'].'</td>
<td>'.$row1['NAAM'].'</td>
</tr>';
}
Try Sql join,
SELECT
b.BANDID, b.NAAMBAND, b.CONTACTBAND, b.BOEKERID, a.id, a.naam
FROM BANDS b
LEFT JOIN adres a ON b.boekerid=a.id
ORDER BY $field $sort
Also don't try to use mysql since it's deprecated, instead try switching to MySQLi or PDO
i guess you need to do something like this:
$sql1 = "SELECT a.BANDID, a.NAAMBAND, a.CONTACTBAND, a.BOEKERID, b.NAAM FROM BANDS a, adres b WHERE a.BANDID = b.ID AND ID = $BOEKERID ORDER BY $field $sort";
$result1 = mysql_query($sql1) or die(mysql_error());
while($row1 = mysql_fetch_array($result1))
echo'<tr>
<td>'.$row1['BANDID'].'</td>
<td>'.$row1['NAAMBAND'].'</td>
<td>'.$row1['CONTACTBAND'].'</td>
<td>'.$row1['BOEKERID'].'</td>
<td>'.$row1['NAAM'].'</td>';
Please correct me if im wrong
I have a query that gets 5 lines of data like this example below
$query = "SELECT ref,user,id FROM table LIMIT 0, 5";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$ref = $row['ref'];
}
I want to run a query inside each results like this below
$query = "SELECT ref,user,id FROM table LIMIT 0, 5";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$ref = $row['ref'];
$query = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) )
{
$title = $row['title'];
} else {
$title = "No Title";
}
echo "$ref - $tile";
}
but for some reason it's only display the first line when I add the query inside it. I can seem to make it run all 5 queries.
SELECT
t1.ref,
t1.user,
t1.id,
t2.domain,
t2.title
FROM
table AS t1
LEFT JOIN anothertable AS t2 ON
t2.domain = t1.ref
LIMIT
0, 5
The problem is that inside the while-cycle you use the same variable $result, which then gets overridden. Use another variable name for the $result in the while cycle.
You change the value of your $query in your while loop.
Change the variable name to something different.
Ex:
$query = "SELECT ref,user,id FROM table LIMIT 0, 5";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$ref = $row['ref'];
$qry = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";
$rslt = mysql_query($qry) or die(mysql_error());
if (mysql_num_rows($rslt) )
{
$title = $row['title'];
} else {
$title = "No Title";
}
echo "$ref - $tile";
}
Use the following :
$query = "SELECT ref,user,id FROM table LIMIT 0, 5";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$ref = $row['ref'];
$query_domain = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";
$result_domain = mysql_query($query_domain) or die(mysql_error());
if (mysql_num_rows($result_domain) )
{
$row_domain = mysql_fetch_row($result_domain);
$title = $row_domain['title'];
} else {
$title = "No Title";
}
echo "$ref - $title";
}
This is a logical problem. It happens that way, because you are same variable names outside and inside the loop.
Explanation:
$query = "SELECT ref,user,id FROM table LIMIT 0, 5";
$result = mysql_query($query) or die(mysql_error());
// Now $results hold the result of the first query
while($row = mysql_fetch_array($result))
{
$ref = $row['ref'];
//Using same $query does not affect that much
$query = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";
//But here you are overriding the previous result set of first query with a new result set
$result = mysql_query($query) or die(mysql_error());
//^ Due to this, next time the loop continues, the $result on whose basis it would loop will already be modified
//..............
Solution 1:
Avoid using same variable names for inner result set
$query = "SELECT ref,user,id FROM table LIMIT 0, 5";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$ref = $row['ref'];
$query = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";
$sub_result = mysql_query($query) or die(mysql_error());
// ^ Change this variable so that it does not overrides previous result set
Solution 2:
Avoid the double query situation. Use joins to get the data in one query call. (Note: You should always try to optimize your query so that you will minimize the number of your queries on the server.)
SELECT
ref,user,id
FROM
table t
INNER JOIN
anothertable t2 on t.ref t2.domain
LIMIT 0, 5
Learn about SQL joins:
SELECT table.ref, table.user, table.id, anothertable.title
FROM table LEFT JOIN anothertable ON anothertable.domain = table.ref
LIMIT 5
You're changing the value of $result in your loop. Change your second query to use a different variable.
it is not give proper result because you have used same name twice, use different name like this edit.
$query = "SELECT ref,user,id FROM table LIMIT 0, 5";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$ref = $row['ref'];
$query1 = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";
$result1 = mysql_query($query1) or die(mysql_error());
if (mysql_num_rows($result1) )
{
$title = $row['title'];
} else {
$title = "No Title";
}
echo "$ref - $tile";
}