$n=mysql_num_rows($rs);
$i=0;
while($n>0)
{
while(($row=mysql_fetch_array($rs))&&$i<=5)
{
echo $row['room_name'];
$i=$i+1;
//echo $n."<br>";
}
echo "<br>";
//echo "n1=".$n;
$n=$n-5;
//
$i=0;
}
Output:101102103104105106
108109110
The row for roomname 107 is missing....
anybody please tell me what is the problem while reentering the loop again...
When $i becomes 6 you fetch a row but do nothing. Because fetching happens before the $i<=5 check, the fetched row gets skipped.
Change the order of conditions in the while loop.
while(($row=mysql_fetch_array($rs))&&$i<=5)
To
while($i<=5 && ($row=mysql_fetch_array($rs)))
Just to follow up on my comment, this whole chunk of code could have been written much more clearly as follows. (assuming you meant to put in a <br> after every 5 records, right now you're doing it after 6 but I think that's probably a mistake)
$rownum = 1;
while ($row = mysql_fetch_array($rs))
{
echo $row['room_name'];
if ($rownum % 5 == 0)
echo '<br>';
$rownum++;
}
here if are checking $i<=5 condition so array stats from 0 , so your database values stats from 101,102,..106, so it will 6 elements .
$i<=5 condition remove this condition in while keep the condition if($i%5==0) echo ""; it will works
Related
this is my code:
$q=mysqli_query($idcon,"SELECT * FROM `subscribed-team`");
while($row=mysqli_fetch_array($q)){
if($row['id-team']==$_POST["teamm"]){
if($row['year-st']==$_SESSION["year"]){
$p=mysqli_query($idcon,"SELECT * FROM `subscription`");
while($row1=mysqli_fetch_array($p)){
if($row1['id-subs']==$row['id-subs'])
echo"<img src='".$row1[8]."'/>";
}
}
}
}
now this code works fine, but it echo my results on the same line since its a loop.
i want my output to be 3 photos per line for example:
image1 image2 image3 (jumps to line)
image4 image5 image6
and not:
image1 image2 image3 image4 image5 image6.
To be more precise i want to add a condition that when the loop reaches 3 values, it returns to a new line and continue printing the results.
Try this. not tested but should work as your need.
$q=mysqli_query($idcon,"SELECT * FROM `subscribed-team`");
while($row=mysqli_fetch_array($q)){
if($row['id-team']==$_POST["teamm"]){
if($row['year-st']==$_SESSION["year"]){
$p=mysqli_query($idcon,"SELECT * FROM `subscription`");
$counter = 0;
while($row1=mysqli_fetch_array($p)){
if($row1['id-subs']==$row['id-subs'])
echo"<img src='".$row1[8]."'/>";
$counter++;
if($counter == 2){
echo "<br>";
$counter = 0;
}
}
}
}
}
I think you are trying to actually put in line breaks. If that is the case use this:
$q=mysqli_query($idcon,"SELECT * FROM `subscribed-team`");
$counter = 0;
while($row=mysqli_fetch_array($q)){
if($row['id-team']==$_POST["teamm"]){
if($row['year-st']==$_SESSION["year"]){
$p=mysqli_query($idcon,"SELECT * FROM `subscription`");
while($row1=mysqli_fetch_array($p)){
if($row1['id-subs']==$row['id-subs'])
echo"<img src='".$row1[8]."'/>";
$counter++;
if($counter % 3 == 0){
echo "\n";
}
}
}
}
}
If you are trying to put in HTML breaks, try this
$q=mysqli_query($idcon,"SELECT * FROM `subscribed-team`");
$counter = 0;
while($row=mysqli_fetch_array($q)){
if($row['id-team']==$_POST["teamm"]){
if($row['year-st']==$_SESSION["year"]){
$p=mysqli_query($idcon,"SELECT * FROM `subscription`");
while($row1=mysqli_fetch_array($p)){
if($row1['id-subs']==$row['id-subs'])
echo"<img src='".$row1[8]."'/>";
$counter++;
if($counter % 3 == 0){
echo "<br/>\n";
}
}
}
}
}
You should avoid making iterated queries.
You should avoid pulling more rows than you intend to use.
You should avoid SELECTing more columns than you intend to use.
You should apply your filtering criteria to the earliest point of data retrieval; in this case, you should use WHERE in your sql instead of an if in your php loop.
You should always use a prepared statement instead of directly inserting user-supplied data into your sql.
I don't know what column number eight is actually called, so I am going to call it src.
I will urge you to make the switch to object-oriented mysqli syntax because it is simpler and more concise than procedural syntax.
The result set object is iterable, so it can be fed directly into the foreach().
To create an html line break between every three rows, echo <br> if $i is not zero and is evenly divisible by 3.
Recommended Code: (untested)
$sql = "SELECT `src`
FROM `subscription` AS s
JOIN `subscribed-team` AS st ON s.`id-subs` = st.`id-subs`
WHERE st.`id-team` = ?
AND st.`year-st` = ?";
$stmt = $idcon->prepare($sql);
$stmt->bind_param("ss", $_POST["teamm"], $_SESSION["year"]);
$stmt->execute();
foreach ($stmt->get_result() as $i => $row) {
if ($i && $i % 3 === 0) {
echo "<br>";
}
echo '<img src="' . $row['src'] . '"/>';
}
I am running a query from a table whose data gets refreshed (deleted and re-inserted) every 30 seconds.
I want my query (which outputs row count) to detect that the row count is null (or zero) and re-run the query. Maximum retries: 5.
And after 5th retries, if it's still zero, I want the row count to print "0".
I know it's a loop but I don't know how to loop it from within the if for row.
<?php
$con=mysqli_connect("HOST","USERNAME","PASS","TABLENAME");
$sql="SELECT id FROM candyshop WHERE candy <= 5 AND sugartype ='hard'";
$number = 0; //init count for loop
if ($result=mysqli_query($con,$sql))
{
$rowcount=mysqli_num_rows($result);
printf($rowcount); //print number of rows
mysqli_free_result($result);
$number = $number+1; //increment number for loop
}
mysqli_close($con);
?>
Use a do while loop and only leave the loop if the rowcount is higher than 0 or 5 retries have been reached.
<?php
$sql = "SELECT id FROM candyshop WHERE candy <= 5 AND sugartype ='hard'";
$count = 0;
$retry = 5;
do {
// Count the rows
if ($result = mysqli_query($con, $sql)) {
$count = mysqli_num_rows($result);
}
// Wait for 10ms
if ($count === 0) {
usleep(10000)
}
} while ($count === 0 && --$retry > 0);
mysqli_close($con);
This is however very bad practice, I would recommend looking for another approach. Why would you clear and then refill the table?
Still new to StackOverflow but can you try this?
<?php
$con=mysqli_connect("HOST","USERNAME","PASS","TABLENAME");
$sql="SELECT id FROM candyshop WHERE candy <= 5 AND sugartype ='hard'";
$number = 0; //init count for loop
if ($result=mysqli_query($con,$sql)) {
$rowcount=mysqli_num_rows($result);
printf($rowcount); //print number of rows
mysqli_free_result($result);
$number = $number+1; //increment number for loop
if ($number = 5) {
printf(0);
break 2;
}
}
mysqli_close($con);
?>
$i = 0;
do {
$result = mysqli_query($con, $sql);
$count = mysqli_num_rows($result);
} while ($count == 0 && $i++ < 5)
As i understood you are trying to write a Web Based Game. Actually your method is probably going to make database a little busy. You don't have to loop queries. If you are going to do it, we are talking about milliseconds here. Let's say your queries are generally giving results in 0.05 seconds that means for 5 query you are just going to have the data of 0.25 seconds.
You can write your game or whatever easily with COMET Programming. But if you decided to go with loop queries , there is only one thing you will do and it's for loop.
$rowcount=0;
do {
if ($result=mysqli_query($con,$sql)) {
$rowcount=mysqli_num_rows($result);
printf($rowcount); //print number of rows
mysqli_free_result($result);
}
} while(++$number<5 && $rowcount!=0);
that is basicaly the answer to your request, though it needs some more explanation to be useful.
staff table
code
<?php
//db configuration
$q = "select * from staff";
$r = mysqli_query($dbc, $q);
$num_rows = mysqli_num_rows($r);
$staffID = array();
while($row = mysqli_fetch_array($r, MYSQLI_ASSOC))
{
$staffID[] = $row['staffID'];
}
for($i = 0; $i < $num_rows; $i++)
{
if($staffID[$i] == $staffID[$i+1])
{
$remark = "Not OK. Multiple staff ID selected.";
$i++;
}
else
{
$remark = "OK.";
}
$data[$i] = $staffID[$i].','.$remark.'<br />';
}
$list = array($data);
print_r($list);
?>
From above code, I want to check whether the staffID got duplicate.
Expected output
0001, OK.
0002, Not OK. Multiple staffID.
0003, OK.
0004, OK.
However, I get another error as below:
Undefined offset: 5 in play.php on line X.
How do I solve it?
By performing the correct query in the first place.
SELECT staffID
FROM staff
GROUP BY staffID
HAVING COUNT(staffID) > 1
Or if you need to know about all staff IDs:
SELECT staffID, COUNT(staffID) as qty
FROM staff
GROUP BY staffID
Just updating your code and i think you have to check the existence first :
if (isset($staffID[$i]) && isset($staffID[$i+!])) {
if($staffID[$i] == $staffID[$i+1])
{
$remark = "Not OK. Multiple staff ID selected.";
$i++;
}
else
{
$remark = "OK.";
}
$data[$i] = $staffID[$i].','.$remark.'<br />';
}
You get the error at this line:
if($staffID[$i] == $staffID[$i+1])
Say for example your $num_rows variable is 3. Then the $staffID array would have indexes 0, 1 and 2. But what happens at this line I noted is that once $i reaches 2 (which is a valid since it's less than 3) you go and do $i+1 which equals 3. And, as you can see, the index 3 is out of the bounds of the array.
If you wish to do this like you wrote your for loop should be like this:
for($i = 0; $i < $num_rows-1; $i++)
But, there is a better way without even doing the for loop. You can do it all in the while loop by making use of the in_array function. Or as Ignacio pointed out, you could do it all on the SQL level.
Not a good way to solve this problem. You should use GROUP BY query for this.
SELECT staffID, COUNT(staffID) as count
FROM staff
GROUP BY staffID
HAVING count > 1
And also about your error. It's because of
if($staffID[$i] == $staffID[$i+1])
on last iteration $i is equal 4 and $i+1 is 5. But $staffID array has no element with such key
I have this. I'm trying to capture the SQL table header name from PHP. This works fine for me.
However I'm struggling to tune this to echo the whole list of table header names except one or two which I dont need to print.
Suppose the names of column number 10 and 15 do not need to be printed how do I tweak my attempt?
Here goes the the code thus far.
// DB1 Connection
$sql = "SELECT * FROM sal_vol;";
$result = mysqli_query($db1,$sql);
$i = 0;
while($i<mysqli_num_fields($result))
{
$meta=mysqli_fetch_field($result);
echo $i.".".$meta->name."<br />";
$i++;
}
while($i<mysqli_num_fields($result))
{
if ($i == 10 || $i == 15) continue;
$meta=mysqli_fetch_field($result);
echo $i.".".$meta->name."<br />";
$i++;
}
I echo results in DESC order from MySQL table with while loop. I've pagination system implemented already and its size is 9 records per page. The problem is, that if I do:
// ECHO CSS BREAK
if($row['id'] % 3 == 0){
echo '<li class="clear"></li>';
}
// SHOW VIDEOS
while($row = mysql_fetch_array($result)){
echo '<li>...echo code...</li>';
// problem = implement that echo css break in ASC order
}
Use a loop variable, e.g.
$i = 0;
Then instead of
if ($row['id'] % 3 == 0) {
do
if (++$i % 3 === 0) {
This makes sure it always happens are the third [sixth, ninth, ...] time.
You may want to get arbitrary rows from the database at another point in time, or shuffle the results -- relying on row IDs is not a good idea.
Is this what you are looking to implement?
// SHOW VIDEOS
while($row = mysql_fetch_array($result)){
if($row['id'] % 3 == 0){
echo '<li>...echo code...</li>';
echo '<li class="clear"></li>';
} else {
echo '<li>...echo code...</li>';
}
}