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;
Related
I want three element in associative array, so far am successful in getting two in the array.
$sql = "SELECT * FROM `notification_table` ";
$resultsd1 = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($resultsd1);
$associativeArray = array();
while ($row = mysqli_fetch_assoc($resultsd1))
{
$associativeArray[$row['name']] = $row['price'] ;
}
foreach($associativeArray as $k => $id){
echo $k."=>".$id .' ';
}
And am getting the response like this
name1=>24.725 name2=>24.265
Now i want to add another column in array as well and the name is column is notification_check .
Am not able to get how to add three columns in a single array. Any help will be appreciated.
I want the output like name1=>24.725=>yes_notification name2=>25.43=>no_notification
And when i print_r($row) is show this output Array ( [sno] => 1 [name] => name1 [price] => 23 [notification_check] => yes_notification)
You could shorten this and use mysqli_fetch_all to create an array of all of the data and then manipulate the array using array_column to create the index...
$result = mysqli_fetch_all($resultsd1, MYSQLI_ASSOC);
$associativeArray = array_column($result, null, 'name');
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 am trying to fetch a column from a database and display the entire contents as an array. I have so far been able to fetch only one of the entries from the table. I understand that I am fetching only $row[0] which is why i am getting only 1 element. I have tried to fetch all elements but I have not succeeded yet. can someone please let me know how to do it correctly? I have attached my code below.
<?php
$con=mysqli_connect("localhost","root","raspberry","users");
if (mysqli_connect_errno())
{
echo "failled to connect:".mysqli_connect_error();
}
$sql = "SELECT `current` FROM `monitor` ORDER BY `Sno.`";
$result = mysqli_query($con,$sql);
$row = mysqli_fetch_array($result,MYSQLI_NUM);
printf("%s\n",$row[0]);
mysqli_close($con)
?>
PDO has a dedicated feature for your problem PDOStatement::fetchColumn.
So, from your question what I understand is- you want to fetch a database column and store this in a array which will make you to display the results.
<?php
$con=mysqli_connect("localhost","root","raspberry","users");
if (mysqli_connect_errno())
{
echo "failled to connect:".mysqli_connect_error();
}
$sql = "SELECT current FROM monitor ORDER BY Sno";
$result = mysqli_query($con,$sql);
$arr = array();
while ($row = mysqli_fetch_array($result)) {
$arr2 = array();
foreach ($row as $val) $arr2[] = $val;
$arr[] = $arr2;
}
$first_col = array_column($arr, 0);
print_r($first_col);
mysqli_close($con)
?>
i.e. if you have five entry to the database whose first column contains
the values like 'current1', 'current2', 'current3', 'current3' and 'current4' the result of this code will be
Array
(
[0] => current1
[1] => current2
[2] => current3
[3] => current4
[4] => current5
)
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'] . '">';
}