Imprimir varias filas de mysql

Its a very simple question ! I execute a select query in mysql and I have result as an array. I only need to print some of values and not the whole array. What is the best way to do that ?

echo $row['id']['range']['name]';

only print id.

Also I need to have them in a good format.

Like : Id range name

And please help me on my second question How can I separate the result of this code in a good format (in this case it print 3 string) :

while($row= mysql_fetch_assoc($result)) {
 echo $row['name'];
}

preguntado el 12 de junio de 12 a las 22:06

in your select for the first part you need a WHERE clause to limit which data is returned from your database. -

What is your output environment? A web page? Command line/terminal? -

do you simply want to print some of the columns but not all the columns or some of the rows but not all the rows? -

all of the rows but some of the columns. the result of my second code is something like johnmattolive and I need to see it like john matt olive -

can you update the select statement and simply select the columns you want to print either-wise just echo $row['id'].' '.['range'].' '.['name]. '<br/>' the format is really up to you but theres simply so many ways... -

3 Respuestas

Formatting will be done by outputting the html you want to use. The example below uses the paragraph tag. You cannot simply stick array keys together like that to reference multiple elements. That syntax is for delving down into multidimensional arrays.

while($row= mysql_fetch_assoc($result)) {
  echo '<p>';
  echo $row['id'];
  echo ' ';
  echo $row['range'];
  echo ' ';
  echo $row['name'];
  echo '</p>';
}

Respondido el 12 de junio de 12 a las 22:06

Depends what kinda format you're after, a table format would be something like this:

//start table, and column headings
echo '<table><tr><td>I.D</td><td>Name</td><td>Range</td></tr>';

//add new row for each returned SQL result row
while($row = mysql_fetch_assoc($result)) {

echo '<tr>';
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['name'] . '</td>';
echo '<td>' . $row['range'] . '</td>';
echo '</tr>';

}

echo '</table>';

Respondido el 12 de junio de 12 a las 22:06

I suggest you to separate a little bit the logic from the format, something like this:

<ul>
<?php while($row= mysql_fetch_assoc($result)): ?>  
    <li><?php $row['id'] ?></li>
    <li><?php $row['range'] ?></li>
    <li><?php $row['name'] ?></li>
<?php endwhile; ?>
</ul>

Respondido el 12 de junio de 12 a las 22:06

What about when its more than 3 result ? it get the info from a html form as post data and then execute a query and print some data based on the submitted data and the MYSQL data. - user1419018

Those aren't three results.... (id, range and name) are the columns of each row. When you use the while loop this while, it traverse all the results. - PachinSV

As you can see the way I tell you is the cleanest, because you don't mix HTML with PHP code. - PachinSV

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.