Im pretty new to MySQl and PHP. I need some help with a small issue.
My statement is as follows:
while($row = $stmt->fetch()) {
$return_arr[] = $row['name'];
$return_arr[] = $row['value'];
}
It outputs as follows:
Mr James Jones
23
How can I bring it together into one line? Like this:
Mr James Jones 23
Thank you
Use implode to join the array elements:
echo implode(' ', $return_arr);
Do like this
while($row = $stmt->fetch()) {
$return_arr[] = $row['name']." ".$row['value'];
}
//print_r($return_arr); // The results gets printed as you expected or you could make use of a foreach construct as shown below.
//Printing using a foreach construct
foreach($return_arr as $k=>$v)
{
echo $v;echo "<br>";
}
Just use like this,
while($row = $stmt->fetch()) {
$return_arr[] = $row['name']. ' '.$row['value'];
}
it will work.
And if you want any other help let me know.
An alternate way is handling it inside your mysql query:
SELECT CONCAT(name, ' ', value) as name_and_value FROM ....
Than you can use
$row['name_and_value']
in your php code.
Related
This code shows the output as subject1 subject2 subject3 subject4 but I want to store that values in different variables (like $s1=[1],$s2[2],$s3[3],$s4[4]) so I and use them in different functions what should I do? can any one help please...
$query = mysql_query("select `subject` from `markssub1` where `sname`='$user' AND `sid`='$id'");
$subjects = [];
while($row = mysql_fetch_array($query))
{
$subjects[] = $row['subject'];
}
foreach($subjects as $subject){
echo $subject . '\n';
}
Right now I'm working on a new homepage for a museum, and I have to use the old database (because it has over 70k entries).
My job now is to change the values in the "dias" table (supporter row) from full names of the supporters to their IDs (supporter table).
Right know I'm using this code:
$result = mysql_query("SELECT pate FROM dias");
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$pate = explode(" ", print_r($row[0]));
echo "<br>";
}
If I now put in $pate[0], I only get "1" as output (along with the names from the print_r).
My question is:
How can I split the names and save them seperately in a variable, because I think I need this to be able to compare the names with the one in the supporter table and to write the ids in the supporter row in the "dias" table.
If you need more infomration, feel free to ask.
explode needs a string as the second argument, but you are taking the return value of print_r, which is true in this case. Just take $row[0] and you should have an array of your data in $pate.
Try printing $pate after that.
$result = mysql_query("SELECT pate FROM dias");
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$pate = explode(" ", $row[0]);
print_r($pate)
echo "<br>";
}
After that continue :).
It looks like you are talking about List() function. list() will enable you explode into variables like;
$address ="Benson Bush|4545547 | living in California" ;
list($name ,$code , $description) = explode("|", $address);
echo $name."<br>";
echo $code."<br>";
echo $description."<br>";
$result = mysql_query("SELECT pate FROM dias");
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$pate = explode(" ", $row[0]);
print_r($pate);
echo "<br>";
}
Don't do this after you've received the data - do it from your query by using SUBSTRING_INDEX:
SELECT SUBSTRING_INDEX(pate, ' ', 1) FROM dias
$result = mysql_query("SELECT pate FROM dias");
while ($row = mysql_fetch_assoc($result)) {
$data[] = $row;
print_r($data);
echo "</br>";
}
print_r($data);
If you would like to stored value in variable then you can use extract function also.
this is the values in the $result
id name
1 a
2 b
2 c
2 d
3 e
..
I'm creating a loop function using mysql_fetch_array()
while($row = mysql_fetch_array($result)) {
$temp_id = $row['id'];
while($temp_id == $row['id']) {
if($temp_id == $row['id']) {
mysql_fetch_array($result);
}
$temp_id = $row['id'];
echo $row['name'].",";
}
echo "<br/>";
}
this works but the problem is, the mysql_fetch_array jumps on one of the values during the transition of the id's..
I want the values of this to be like these
a
b,c,d
e
my question is, is there a simple rewind function that will step only once in the rows?
I have searched about the mysql_data_seek() but I think this would require additional control variables like $i to locate the address..
thanks.. any suggestions and function samples will be very great!
Use one loop with mysql_fetch_array first to create an array of the rows. Then, iterate over the rows themselves.
Based on the update to your question it seems like you would want to use GROUP_CONCAT in the query, but you can still do it in PHP:
$rows = array();
while($row = mysql_fetch_assoc($result)) {
if (!isset($rows[$row['id']]) {
$rows[$row['id'] = array();
}
$rows[$row['id']][] = $row['name'];
}
foreach ($rows as $row) {
echo implode(',', $row) . "<br>";
}
Try this code
while($row = mysql_fetch_array($result)) {
$temp[$row['id']][] = $row['name'];
}
foreach($temp as $row) {
echo implode(',', $row) . '<br/>';
}
Hey usually I use mysqli_fetch_array to display the content of my database in my CMS, but recently someone told me I should use mysqli_fetch_assoc and push the results into an array so that database only runs once instead of running the database for each record.
But I'm not really sure how to display my fields without showing them all, usually I would echo $data['field_name'], but what I've noticed with mysqli_fetch_assoc is I can't just echo $value['field_name'], all I can do is echo $value and it displays all the results.
This is what I've done, hope that makes sense. Thanks in advance for any help!
PHP
$sql = "SELECT * FROM app_categories";
if($result = query($sql)){
$list = array();
while($data = mysqli_fetch_assoc($result)){
array_push($list, $data);
}
foreach($list as $i=>$row){
foreach($row as $column=>$value){
echo $value;
}
}
}
why you don't just
echo $row['field_name'];
I have one query where the output processing looks like this:
while($row = mysql_fetch_array($result)){
echo $row['username'] . " " . $row['earning'];
}
And outputs result is like this:
JOHN 200
JOHN 350
NEO 100
NEO 220
What I want to achieve is that every name appears once, and "earning" is sum of all earnings of that name, like this:
JOHN 550
NEO 320
I have to mention that I CANNOT change the query; that is biggest problem.
Is there any hope? Some suggestions? :)
You can sum the values in the loop to another array and then output it.
Try:
$earnings = array();
while($row = mysql_fetch_array($result)) {
if (!isset($earnings[$row['username']])) $earnings[$row['username']] = 0;
$earnings[$row['username']] += $row['earning'];
}
foreach($earnings as $user => $earnings) {
echo "$user $earnings<br />\n";
}
try:
$user = array();
while($row = mysql_fetch_array($result)){
$user[$row['username']] += $row['earning'];
}
to do echo :
foreach($user as $key => $value) {
echo $key."=". $earnings."<br>";
}
To get a quick solution to this answer you may want to simply append these results to an associated array and then simply loop over it to get the final count.
So, something like this:
$names= array();
while($row = mysql_fetch_array($result)){
$names[$row["username"]] += $row["earning"];
}
foreach($names as $k => $v) {
echo $k." ".$v."\n";
}
Instead of just selecting the data, select the field and SUM(otherfield) with otherfield being the field with the number to be summed. Then add GROUP BY with the first field.
try this in mysql side you don't need to calculate in php side so don't change your PHP side code jst change query like this
SELECT
username,
SUM(earning)
FROM yourtable
GROUP BY (username)
hope its will work for you