Php code results not matching mySQL query

mySQL Table

ID | Name

1 | A

2 | B

3 | C

4 | D

5 | E

mySQL Query

$query = "SELECT * FROM ego_work WHERE 1; $result = mysql_query($query); $rows = array(); while ($row = mysql_fetch_array($result)) { $rows[] = $row; }

php Code

<?php foreach ($rows as $work): ?> <span> <?php echo $work['id']; ?>, </span> <?php endforeach; ?> <br \> <?php foreach ($rows as $work): ?> <span> <?php echo $work['name']; ?>, </span> <?php endforeach; ?>

RESULT

1,2,3,4,5

E,A,B,C,D

What did I do wrong? I'm trying to get the 2nd result to be A,B,C,D,E

--------------Solutions-------------

can you try below code

<?php foreach ($rows as $work): ?>
<span> <?php echo $work['id']; ?>, </span>
<?php endforeach; ?>

<?php reset($rows);?>
<br \>
<?php foreach ($rows as $work): ?>
<span> <?php echo $work['name']; ?>, </span>
<?php endforeach; ?>

Category:php Time:2011-12-17 Views:1
Tags: php mysql

Related post

  • Sorting the results of a mysql query 2012-04-09

    Im having difficulties trying to figure out an elegant solution to sorting the results of a mysql query based on a delimited string. Ill explain in more detail below I am creating a database of contacts where individual users can add/remove people fr

  • Cache results of a mysql query manually to a txt file 2010-09-11

    Is there a way to cache results of a mysql query manually to a txt file? Ex: $a=1; $b=9; $c=0; $cache_filename = 'cached_results/'.md5("$a,$b,$c").'.txt'; if(!file_exists($cache_filename)){ $result = mysql_query("SELECT * FROM abc,def WHERE a=$a AND

  • How do you convert the result of a MySQL query into an integer in PHP? 2011-02-26

    I tried changing the result of a MySQL query to an integer with intval(), but I got an error saying it could not be changed to an int. Why I want to do this is to obtain the value of one of the fields in the database, minus one from it, and update th

  • Quick way to email results of a mysql query 2011-09-01

    Is there a simple way to email the results of a mysql query from either the mysql console directly or from the linux console? Nice formatting is a bonus. --------------Solutions------------- mysql -u .. -p.. -H <<<"your query" | mutt -s 'sub

  • I want query for show Table Result and Use MySQL query only 2011-10-07

    TABLE A ID | NAME -----+------- n1 | BLACK n2 | WHITE n3 | RED n4 | GREEN TABLE B ID | A_ID | BLABLA | TIME ---+---------+---------------+-------- 1 | n1 | X | sometime 2 | n1 | X | sometime 3 | n1 | X | sometime 4 | n1 | Y | sometime 5 | n1 | Z | so

  • Using results from one MySQL query to insert into another query 2011-11-28

    I am trying to use PHP and MySQL to automatically generate a list like this. Subject #1 Resource #3 Resource #5 Resource #12 Subject #2 Resource #7 Resource #4 Resource #1 etc. Here are the tables: eresources erid etitle subjects sid stitle subjectma

  • PHP code to convert a MySQL query to CSV 2008-09-24

    What is the most efficient way to convert a MySQL query to CSV in PHP please? It would be best to avoid temp files as this reduces portability (dir paths and setting file-system permissions required). The CSV should also include one top line of field

  • using while statement for each individual result of a mysql query 2011-06-10

    I'm unsure whether to use 'while' or 'for each' for this code. I have a mysql query that will bring back more then one result. I then, for each result, want to do a certain thing if $row[number] is empty or not I have this: $sql = "SELECT * FROM $tbl

  • How can I edit the results of a MySQL query inline using AJAX or something similar? 2011-07-19

    I'm using PHP to inject a table, generated from a MySQL query, to a mixed HTML/PHP file, using a <div>. The <div> is declared in index.php like so: <div id="searchdiv"></div>. And the content for the <div> is generated l

  • Place All Resulting Rows from MySQL Query in Arrays 2011-09-19

    I have the following MySQL query: $imgDimensions_query = "SELECT imgWidth, imgHeight FROM ( SELECT imgHeight, imgWidth FROM primary_images WHERE imgId < 10 UNION SELECT imgHeight, imgWidth FROM secondary_images WHERE primaryId < 10) as imgDimen

  • How do I write results of a MySQL query to a text file inside a loop? 2011-12-14

    I have a large database of individuals (say 1000 individuals/ 5,000 records per individual). I'd like to write a couple of fields for each individual (in this example let's say lat and long) to a text file (preferably comma separated). The algorithm

  • The least amount of code possible for this MySQL query? 2012-03-24

    I have a MySQL query that: gets data from three tables linked by unique id's. counts the number of games played in each category, from each user and counts the number of games each user has played that fall under the "fps" category It seems to me tha

  • How to code a "multi-command" mysql query in php 2010-12-25

    Please note that this question is not about the mysql query per se, but HOW TO RUN IT from php. For lack of a better term, I called it a multi-command query. I have a query which runs perfectly in mysql commandline client, and in phpmyadmin, as follo

  • How to index the result of a mySql query as an array of array? 2011-10-06

    If I need to select and use information of every element of a table in a database the procedure would be this: $query = "...mySql query..."; $query_result = mysql_query($query) or die (mysql_error()); Then if I wished to access the fields of the resu

  • Retrieving field metadata from the result of a MySQL Query 2012-01-08

    I want to retrieve the result of a complex query from a MySQL DB implementing a serverside method (API programmed with php+CodeIgniter) that will allow configuration options (sent by the clientside) such as tables, related fields, join type etc. I al

  • Saving results of complex MySQL query/report 2011-07-13

    I'm looking for the best way to implement the following. I have a MySQL query that analyses performance of a company, and outputs sales, revenue, costs and so on, and basically outputs a final Gross Profit and Net Profit figure. It's working well, an

  • Is it possible to execute PHP code returned from a MySQL query result? 2011-02-21

    The issue I am having is as follows: I have a MySQL table that contains details for page content I wish to display on my site. The content for one of my pages however I wanted to contain some actual PHP code to be executed, not just printed as a stri

  • Add column from another table matching results from first MySQL query 2010-05-05

    This is my query for available rooms in choosen period: SELECT rooms.room_id FROM rooms WHERE rooms.room_id NOT IN ( SELECT reservations.room_id FROM reservations WHERE ( reservations.arrivaldate >= $arrival_datetime AND reservations.departuredate

  • How to limit duplicate column results in a MySQL query? 2011-10-14

    I'm having difficulty removing the duplicate data when exporting an order where more than 1 product is ordered. Example: orders_id, product_id, customer_first_name, customer_last_name 001 , ProductA , FirstName , LastName 001 , ProductB , FirstName ,

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 0.564 (s). 13 q(s)