echo "<pre>"; print_r($jobbrnchesids); exit;
<pre>Array
(
[0] => Array
(
[id_branch] => 6
)
[1] => Array
(
[id_branch] => 1
)
)
From the above array, i am getting job id branches.. Now i am trying to Get the students who are with these branches.
i have tried like this way but it something is going wrong unable to debug can anyone help me out.
$studentBranch = '';
foreach ($jobbrnchesids as $k => $v){
$stuBranch = $conn->query("SELECT student_pid FROM tbl_students
WHERE graduation_branch = ".$v." ");
$studentsWithBranches[] = $stuBranch->fetch_assoc();
}
echo "<pre>"; print_r($studentsWithBranches); exit;
instead of $v it need to be $v['id_branch']
missing array index 'id_branch'
Try:
$stuBranch = $conn->query("SELECT student_pid FROM tbl_students
WHERE graduation_branch = ".$v['id_branch']." ");
Reduce your overhead by doing the following:
$branchIds = array_column($jobbrnchesids,"id_branch");
$result = $conn->query("SELECT student_pid FROM tbl_students
WHERE graduation_branch IN (".implode(",",$branchIds.")");
$studentsWithBranches = $result?$result->fetch_all(MYSQLI_ASSOC):[];
array_column Returns the values from a single column in the input array
$studentBranch = '';
foreach ($jobbrnchesids as $k => $v) {
$stuBranch = $conn->query("SELECT student_pid FROM tbl_students WHERE graduation_branch = '" . ['id_branch'] . "'");
$studentsWithBranches[] = $stuBranch->fetch_assoc();
}
echo "<pre>";
print_r($studentsWithBranches);
exit;
Related
I am having some trouble trying to get this done. The issue is:
I have an array that looks like
wants_arr_flat ( [0] => booze [1] => nudes [2]
=> rooms
I want my foreach loop to go through these values making a different SQL statement and then saving the result of that SQL statement on to another array. The code I am trying to work with now is this.
foreach ( $wants_arr_flat as $value ) {
$sql = "SELECT * FROM offers WHERE user_id != $_SESSION[user_id] AND offer='$value'";
$result=$conn->query($sql);
$want_match_arr = mysqli_fetch_row($result);
echo '<pre>'; print_r($want_match_arr); echo '</pre>'; //testing echo
Obviously this is just overwriting the last array each iteration so I only end up with one result in the array.
instead of
$want_match_arr = mysqli_fetch_row($result);
use
$want_match_arr[] = mysqli_fetch_row($result);
If you get multiple rows from SQL then this is better.
$want_match_arr = [];
foreach ( $wants_arr_flat as $value ) {
$sql = "SELECT * FROM offers WHERE user_id != $_SESSION[user_id] AND offer='$value'";
$result=$conn->query($sql);
while ($row = mysql_fetch_assoc($result)) {
$want_match_arr[] = $row;
}
}
echo '<pre>'; print_r($want_match_arr); echo '</pre>'; //testing echo
How do I use REGEXP to fetch multiple data from the array $vall.
I referred a sample code from:
PHP sql statement where clause to multiple array values
Some of the sample data: CGCG-0025-0,CGCR-0003-0,CGRQ-0163-0
foreach ($list as $value) // this $list is from another array
{
$part = $value[3];
$vall[] = $value[3]; // $list1 is an empty array
}
$partnumber =[];
foreach($vall as $v)
{
$partnumber[] = "*.".$v.".*";
print_r(array_values($partnumber)); // these are some of the values Array ( [0] => *.CGCG-0025-0.* ) Array ( [0] => *.CGCG-0025-0.* [1] => *.CGCG-0025-0.* ) Array ( [0] => *.CGCG-0025-0.* [1] => *.CGCG-0025-0.* [2] => *.CGCR-0003-0.* )
}
foreach($partnumber as $x)
{
echo '<br>'.$partnumber; // shows 'Array' on each lines
}
$fsql="select * from Error where RptDatime = 201706091000 and partnumber REGEXP '".implode("|",$partnumber)."'";
//example 1 $fsql="select * from MPdError where RptDatime = 201706091000 and partnumber = ('CGRQ-0057-0') ";
//example 1 shows the correct data but i need multiple partnumber
$getResults = $conn->prepare($fsql);
$getResults->execute();
$results = $getResults->fetchAll(PDO::FETCH_BOTH);
foreach($results as $row)
{
$mac = $row['Machine'];
$id = $row['Id'];
echo '<br><br><br>ID:'.$id.'<br>Machine Number :'.$mac;
}
Though this may not be a good solution as it contains a huge security risk, I will still post it based on what is needed above.
REGEXP is not needed for data that are not complex in design.
$fsql = "select * from Error where RptDatime = 201706091000 and partnumber in ('" . implode("','", $vall) . "');"
I am querying the DB successfully using an array and I am getting the correct result. But I am having difficulty using the results because of the way the array is created.
Here is the OUTPUT:
Array (
[0] => Array (
[0] => Array (
[0] => 1#1.1
)
[1] => 2#2.2
)
[2] => 3#3.3
)
Here is how the code is generated:
$owners_string = $row['profile_id'];
$owners_stringd = unserialize($owners_string);
foreach ($owners_stringd as $profileid => $valuee) {
if ($valuee) {
$sql = "select email from {$mysql_tbl_pre}profile where profile_id = '$valuee' ";
$err = mysqli_query($estate_db, $sql);
if (!$err)
error_dlg(mysqli_error($estate_db));
elseif (mysqli_num_rows($err) <= 0)
info_dlg("Error");
while ($row = mysqli_fetch_array($err))
$new_array[$profileid] = $row['email'];
$new_array = array($new_array);
foreach ($new_array as $emailvalue)
$emailsd = print_r($emailvalue, true);
} else {
$emailsd = "";
}
}
I know how to implode the results, but when i do implode this result, is comes out as "Array, 1#1.1" (which ever email was last).
I need it to be one entire result such as 1#1.1,2#2.2,3#3.3
These are two separate Tables. The first Tables stores a string of Ids that, the second Tables has each Id in its own row with an email in a separate column.
Solution from the while loop:
while ($thisemail = mysqli_fetch_array($rsz)) {
$emailsd .= $thisemail['email'] . ',';
}
foreach ($emailsd as $emailvalue)
$email = print_r($emailvalue, true);
I have the following function:
public function albums($artist) {
$artist = $this->db->sanitize($artist);
$query = "
SELECT `album`, `cover`
FROM `albums`
join `artists`
on `albums`.`artistsID` = `artists`.`id`
WHERE `artists`.`artist` = ?
";
$stmt = $this->db->mysqli->prepare($query);
$stmt->bind_param('s', $artist);
$stmt->execute();
$result = $stmt->get_result();
while($row = $result->fetch_assoc()) {
$data[] = $row;
}
return $data;
}
As you can see this will return an array with the album name and the cove file path.
The array output looks like this:
Array
(
[0] => Array
(
[album] => Album name
[cover] => file path
)
[1] => Array
(
[album] => Album name
[cover] => file path
)
)
As you can see it returns an array with 0 and 1 with an array of album and cover.
What I would like it to be is an array which is easier to use with a while loop.
So I can do something like:
while($row = $data) {
echo $row['album'].' <img src="'.$row['cover'].'">';
}
instead of 2 foreach loops like I have to do now. So my question is how can I get an array which is easier to use like you would normaly get when you do something like:
<?php
$query = mysqli_query($connect, "SELECT `username` FROM `users` WHERE `id` = 1");
while($row = mysqli_fetch_assoc($query)) {
echo $row['username'];
} //Just as an example to make the question a bit more understandable
?>
I hope someone could help me out on this one. If you need more information please let me know.
What I would like it to be is an array which is easier to use with a while loop
You should rather use a foreach loop for this … then the whole “problem” solves itself without any further meddling with the array structure.
You're using fetch_assoc() so...
while($row = $result->fetch_assoc()) {
echo $row['album'] . ' <img src="' . $row['cover'] . '">';
}
I have the following code:
<?php
include('settings.php'); //Here I have my connection string
$result = mysql_query("SELECT myrow FROM mytable");
$storeArray = Array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$storeArray[] = $row['myrow'];
}
print_r($storeArray);
?>
And the results look like this:
Array ( [0] => emailaddress1#email.com [1] => [2] => emailaddress2#email.com [3] => emailaddress3#email.com )
I was wondering what I need to do in order for it to print like this:
emailaddress1#email.com, emailaddress2#email.com, emailaddress3#email.com ..and so forth.
The reason is I've been tasked with creating mailing list, so I need to print out all of the email addresses in the database in an easy-to-insert into Outlook format
Is there a better way?
implode( ', ', $storeArray)
Or try this method :
foreach($storeArray as $value)
$stringval.= $value.', ';
$finalval = substr($stringval,0,-2);
echo $finalval;