Skip to main content
deleted 1 character in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

If I have to loop results of a query echoing first all fields of a column, then echoing something not to loop, then fields of another column. So I'm using

<?php
$con = mysqli_connect();
echo 'Latest users who tried our test<br>';
//select the fields of the first column
$query1 = mysqli_query($con,"SELECT user FROM ris_uinversita LIMIT 0,10");
//loop the results
while($assoc1=mysqli_fetch_assoc($query1)){echo $assoc1['user'].'<br>';}
//echo something not to loop
echo 'Users got the following scores<br>';
//select the fields of the second column
$query2 = mysqli_query($con,"SELECT score FROM ris_universita LIMIT 0,10");
//loop the results
while($assoc2=mysqli_fetch_assoc($query2)){echo $assoc2['score'].'<br>';}
?>

Is it possible to use just one query like this

mysqli_query($con,"SELECT * FROM ris_universita LIMIT 0,10");

Toto select both fields just once? If

If it could be helpful, table "ris_universita"ris_universita has just the two columns: "score" (tinyinttinyint) and "user" (varchar 30varchar 30), for now there. There aren't so many records for now, but I hope in future there will be in the future.

If I have to loop results of a query echoing first all fields of a column, then echoing something not to loop, then fields of another column. So I'm using

<?php
$con = mysqli_connect();
echo 'Latest users who tried our test<br>';
//select the fields of the first column
$query1 = mysqli_query($con,"SELECT user FROM ris_uinversita LIMIT 0,10");
//loop the results
while($assoc1=mysqli_fetch_assoc($query1)){echo $assoc1['user'].'<br>';}
//echo something not to loop
echo 'Users got the following scores<br>';
//select the fields of the second column
$query2 = mysqli_query($con,"SELECT score FROM ris_universita LIMIT 0,10");
//loop the results
while($assoc2=mysqli_fetch_assoc($query2)){echo $assoc2['score'].'<br>';}
?>

Is it possible to use just one query like this

mysqli_query($con,"SELECT * FROM ris_universita LIMIT 0,10");

To select both fields just once? If it could be helpful, table "ris_universita" has just the two columns "score" (tinyint) and "user" (varchar 30), for now there aren't so many records, but I hope in future there will.

If I have to loop results of a query echoing first all fields of a column, then echoing something not to loop, then fields of another column.

<?php
$con = mysqli_connect();
echo 'Latest users who tried our test<br>';
//select the fields of the first column
$query1 = mysqli_query($con,"SELECT user FROM ris_uinversita LIMIT 0,10");
//loop the results
while($assoc1=mysqli_fetch_assoc($query1)){echo $assoc1['user'].'<br>';}
//echo something not to loop
echo 'Users got the following scores<br>';
//select the fields of the second column
$query2 = mysqli_query($con,"SELECT score FROM ris_universita LIMIT 0,10");
//loop the results
while($assoc2=mysqli_fetch_assoc($query2)){echo $assoc2['score'].'<br>';}
?>

Is it possible to use just one query like this

mysqli_query($con,"SELECT * FROM ris_universita LIMIT 0,10");

to select both fields just once?

If it could be helpful, table ris_universita has just the two columns: "score" (tinyint) and "user" (varchar 30). There aren't so many records for now, but I hope in there will be in the future.

Tweeted twitter.com/#!/StackCodeReview/status/484656581158907905
Post Reopened by 200_success
Described the situation more specifically
Source Link
user46302
user46302

If I have to loop results of a query, but in the middle echoing first all fields of them I want to inserta column, then echoing something I don't wantnot to be repeatedloop, I usually repeat the query two times like thisthen fields of another column. So I'm using

<?php
$con = mysqli_connect();
echo 'Latest users who tried our test<br>';
//select the fields of the first column
$query1 = mysqli_query($con,"SELECT *user FROM table"ris_uinversita LIMIT 0,10");
//loop the results
while($assoc1=mysqli_fetch_assoc($query1)){echo $assoc1['col'];$assoc1['user'].'<br>';}
//echo 'Somethingsomething not to loop';loop
echo 'Users got the following scores<br>';
//select the fields of the second column
$query2 = mysqli_query($con,"SELECT *score FROM table"ris_universita LIMIT 0,10");
//loop the results
while($assoc2=mysqli_fetch_assoc($query2)){echo $assoc2['anothercol'];$assoc2['score'].'<br>';}
?>

Is there a better wayit possible to douse just one query like this

mysqli_query($con,"SELECT * FROM ris_universita LIMIT 0,10");

To select both fields just once? If it could be helpful, table "ris_universita" has just the two columns "score" (tinyint) and "user" (varchar 30), for now there aren't so many records, but I hope in future there will.

If I have to loop results of a query, but in the middle of them I want to insert something I don't want to be repeated, I usually repeat the query two times like this

<?php
mysqli_connect();
$query1 = mysqli_query($con,"SELECT * FROM table");
while($assoc1=mysqli_fetch_assoc($query1)){echo $assoc1['col'];}
echo 'Something not to loop';
$query2 = mysqli_query($con,"SELECT * FROM table");
while($assoc2=mysqli_fetch_assoc($query2)){echo $assoc2['anothercol'];}
?>

Is there a better way to do this?

If I have to loop results of a query echoing first all fields of a column, then echoing something not to loop, then fields of another column. So I'm using

<?php
$con = mysqli_connect();
echo 'Latest users who tried our test<br>';
//select the fields of the first column
$query1 = mysqli_query($con,"SELECT user FROM ris_uinversita LIMIT 0,10");
//loop the results
while($assoc1=mysqli_fetch_assoc($query1)){echo $assoc1['user'].'<br>';}
//echo something not to loop
echo 'Users got the following scores<br>';
//select the fields of the second column
$query2 = mysqli_query($con,"SELECT score FROM ris_universita LIMIT 0,10");
//loop the results
while($assoc2=mysqli_fetch_assoc($query2)){echo $assoc2['score'].'<br>';}
?>

Is it possible to use just one query like this

mysqli_query($con,"SELECT * FROM ris_universita LIMIT 0,10");

To select both fields just once? If it could be helpful, table "ris_universita" has just the two columns "score" (tinyint) and "user" (varchar 30), for now there aren't so many records, but I hope in future there will.

Post Closed as "Not suitable for this site" by 200_success
Source Link
user46302
user46302

Two while for the same query

If I have to loop results of a query, but in the middle of them I want to insert something I don't want to be repeated, I usually repeat the query two times like this

<?php
mysqli_connect();
$query1 = mysqli_query($con,"SELECT * FROM table");
while($assoc1=mysqli_fetch_assoc($query1)){echo $assoc1['col'];}
echo 'Something not to loop';
$query2 = mysqli_query($con,"SELECT * FROM table");
while($assoc2=mysqli_fetch_assoc($query2)){echo $assoc2['anothercol'];}
?>

Is there a better way to do this?