I have problem with my script
the problem with loop if someone can help me please
Problems:
I am starting loop perfectly without any problem with first loop only.
When result = 0 next loop start it gives me -1 -2 -3 etc in first account only.
and the correct way :
when next loop start its add +2 in smtp $server_index++; and start from first without sticky in first account and get minues numbers in same account .
$server_index = 0;
while($customer = mysql_fetch_assoc($result)){
//// Start Server Switch
$available_server_limit = $servers[$server_index]["per_day_limit"] - getEmailUsage($servers[$server_index]["id"],date("d/m/Y",time()));
if($available_server_limit==0){
if($server_index==$servers_count-1){
//exit(showError("Sorry! We don't have limit to send more emails"));
mysql_query("UPDATE `smtp_servers` SET `per_day_limit`=`per_day_limit`+2");
$server_index=0;
}else{
$server_index++;
}
}
It's really difficult to see exactly what you want from only this much info. However, I should point out that you're only going to hit $server_index++; IF $available_server_limit==0
If $available_server_limit does not equal 0, you'll get stuck.
The loop -- by the way -- is in a while statement relying on variables you're not using. May I suggest a foreach, or for statement?
On another note, please do away with mysql_* while you still can. It's depreciated as of recent versions of PHP.
See this for more info
I also notice that you don't show the hole loop. So with that in mind, here's my GUESS at the best way around this for YOU.
$server_index = 0;
foreach( $servers as $key=>$val )
{
$available_server_limit = $servers[$server_index]["per_day_limit"] - getEmailUsage($servers[$server_index]["id"],date("d/m/Y",time()));
if($available_server_limit==0)
{
if($server_index==$servers_count-1)
{
mysql_query("UPDATE `smtp_servers` SET `per_day_limit`=`per_day_limit`+2");
$server_index=0;
}
}
$server_index++;
Note: I didn't close the foreach loop because your while loop isn't closed. I figure you've got code after this that you're using.
Related
I made a while loop that will tell if there are items in the transaction history and will put it back in the inventory as the transaction ends but the problem is. It fetches an error called
Uncaught Error: Call to a member function fetch_assoc() on bool in
So I tried the query's one by one and it works.
I tried to experiment and comment the other query and adding a counter++; to tell if the loop works. The problem I found is that after the first if else the counter only add's 1 Where as if I only try this it loops 4 which is right
Query I tried to check the number of loops
$counter=0;
$sql="SELECT * FROM brb_backtransaction WHERE trans_uk='$curr_trans' ";
if($rs=$con->query($sql)){
while ($row=$rs->fetch_assoc()){
$counter++
}
}
It echoes 1234 so it's correct but when the first if else happen it only echo'es 1
$counter =0;
$sql="SELECT * FROM brb_backtransaction WHERE trans_uk='$curr_trans' ";
if($rs=$con->query($sql)){
while ($row=$rs->fetch_assoc()){
$item = $row['trans_item'];
$quan = $row['trans_quantity'];
$sqlsitem="SELECT itmQuantity FROM brb_inventory WHERE itmName='$item'";
if($rs=$con->query($sqlsitem)){
$quanrow = $rs->fetch_assoc();
$currquan = $quanrow['itmQuantity'];
$counter++;
}
I remove other query's as I think this is the problem I ran a total of 3 queries in the while loop.
For your while loop to work it needs access to your original data stream as stored in the $rs variable.
However, further down in your code you are replacing the contents of the $rs variable with a totally new data stream. Therefore, the while loop no longer has access to the original data stream as it is basically throw away.
To solve this, change the second instance to another variable name such as $rs2. That way you have two completely different variables for two different data streams.
With that said, your code is also open to injection attacks. I would recommend looking into PDO and prepared statements.
Also, Tangentially Perpendicular is correct in using SQL JOINS
you see, I have these variables that are set within a While, I'm using MySQL also, What I want to do is to sort of "Group" those variables into one, and then the rest of the variables that are not repeated, i want to assign them "Display:none" So , look at this image:
Now, as you can see there is two duplicate dates, but the values are different, so in this case (Take in mind that I only want to group these "Recibos" but dont worry about it) you can see in the first row it has a different date, and has no repeated dates, while the last two of "Recibos" is sort of duplicated
Basically what I want to do is to group the two last "Recibos" (The duplicate dates), only one of the same date has to appear, and the rest of them, are gonna be shown when the TR is hover
Though , I only want a simple version of what I want, it can be some sort of testing, as you can see in this image, i've been trying for weeks and still didnt get anything, I hope you can help me! thanks in advance
I have made this code, which is the one in the image above:
Inside the while:
$acumulado-=$monto;
if(($fila>=$mostrar_de_aca_en_adelante && $detalles=='minimo') || ($detalles=='maximos')){
$conteo++;
$string_fecha[$conteo] = $fecha_operacion;
$string_fecha_tmp[$conteo] = $fecha_operacion;
$recibo_id_cliente[$conteo]=$id_cliente;
$recibo_monto[$conteo]=$monto;
$recibo_saldo_total[$conteo]=$saldo_total;
$recibo_detalle_movimiento[$conteo]=$datos_detalles['detalle_movimiento'];
$recibo_recibo[$conteo]=$recibo[0];
$recibo_verificado[$conteo]=$datos_detalles['verificado'];
$recibo_acumulado[$conteo]=$acumulado;
$label = $monto;
}
Outside the while:
echo "<br><br><br>";
for($i = 1; $i <= $conteo; $i++){
if($string_fecha[$i] == $string_fecha_tmp[($i + 1)]){
$label += $recibo_monto[$i];
$string_imprime_tmp .= "<br>[Repetido]".$string_fecha[$i]."(".$label.")";
}
}
echo $string_imprime_tmp;
var_dump($string_fecha);
By the way: I know this can be done in MySQL, but it can only be done in php i'm limited to php.
I'm really sorry if this has been asked before, I've looked as far as I could on here and none of the similar questions answer mine...
I'm trying to create a raffle of sorts... I have a database table of members and their entries and I'm trying to insert their names into another database table repeatedly for as many times as their individual entry amounts say.
Now I've looked up for() loops and as far as I can understand it, this attempt of my code should have worked:
$Amount = mysql_query("SELECT * FROM reapingbowl WHERE userid = $reapablef->id") or die(mysql_error());
$reapablef = mysql_query("SELECT * FROM members");
$reapablef = mysql_fetch_object($reapablef);
$amount = $reapablef->entries
for($e = 1; $e < $amount; $e++)
{
mysql_query("INSERT INTO reapingbowl (userid) VALUES ($reapablef->id)") or
die(mysql_error());
}
Wherein $amount is previously defined to pull the entry amounts from the database and $reapablef is previously defined to pull the specific user from the database table. The whole thing is wrapped in a while loop that works on one user at a time.
My problem is only that this for() loop never ends. It should, as far as I understand it, end after the amount of entries supersedes the $e value, but it just keeps going and I don't understand why... I have a similar problem with another for() loop and I don't understand why.
I tried a while loop as well with the coding being this:
$Amount = mysql_query("SELECT * FROM reapingbowl WHERE userid = $reapablef->id") or die(mysql_error());
while ($amount = mysql_num_rows($Amount), $amount < $ENTRIES)
{
mysql_query("INSERT INTO reapingbowl (userid) VALUES ('$reapablef->id')") or
die(mysql_error());
}
This does the same thing, just keeps going with entering the names neverendingly... I'm really sorry if I'm being stupid or something but please can someone help me figure out why?
Hope that's all the code necessary. All I really want to know is why the for() loop doesn't end ever... (I've tried it somewhere else with the limit of 10 and it still just went on forever)
My bet is, your $amount variable contains an object.
As PHP manual states, when using comparison operators, an object is always considered greater than a number.
In an alternate reality, PHP could throw a warning at you for doing silly comparisons, but for some reason in this universe of ours it doesn't.
It is well possible there is a simple explanation and/or a good reason for that, but right now I can't think of any.
I want to simply display something using a while loop with php, but it is not working.
Here is the code -
<?php
mysqli_select_db($connect,"users");
$select_title = "select title, message from messages where user = '$u' LIMIT 4 ";
$querying = mysqli_query($connect,$select_title) or die ('Whops! Something went wrong.');
$line = mysqli_fetch_assoc($querying);
//$title = mysqli_real_escape_string($connect,trim($line['title']));
while(($rows = mysqli_fetch_assoc($querying)))
{
echo $rows['title'];
}
?>
Now I have two titles, but only one is being displayed. Why so?
You have fetched one row here:
$line = mysqli_fetch_assoc($querying);
The cursor will be moved to the next row, hence
while(($rows = mysqli_fetch_assoc($querying)))
{
echo $rows['title'];
}
will display the second row only.
The best solution is simply comment that line:
//$line = mysqli_fetch_assoc($querying);
Delete this line:
$line = mysqli_fetch_assoc($querying);
Comment this line will solve your problem
//$line = mysqli_fetch_assoc($querying);
... because you fetch the first title with the line $line = mysqli_fetch_assoc($querying); but never echo it and then start a loop to go through the rest of the titles.
Delete/uncomment the line and you should be fine.
you already fetch one record with
$line = mysqli_fetch_assoc($querying);
so there is only one left in while
Debug
My MySQL is a bit rusty(Long time ago since I used SQL => Moved to NoSQL camp...), but this is what I would do:
First I would test if query returns correct result using phpmyadmin console or just mysql console from commandline.
But I guess the query is correct because a lot of people are saying that
$line = mysqli_fetch_assoc($querying);
is already fetching a row. But still you could use it for debugging purposes I guess.
PDO
Also I would like to point out you should PDO instead of mysqli(almost deprecated I guess). PDO prepared statements also protect you against SQL-injections and could be faster because it is precompiled.
Every time you call mysqli_fetch_assoc() you "take" one record from the result set. When there are no more records, mysqli_fetch_assoc() returns false. This is, by the way, the reason you can use while loop.
When you called $line = mysqli_fetch_assoc($querying);, you took one record from the result set, leaving only 1 in the result set. So your while loop iterates over this one record only. As everyone else pointed out - you just need to comment that line.
I can also recommend using mysqli_fetch_all() function, this will give you an ordinary array that you can use, well, like a normal array
$results = mysqli_fetch_all($querying, MYSQLI_ASSOC);
I have a php page query mysql database, it will return about 20000 rows. However the browser will take above 20 minutes to present. I have added index on my database and it do used it, the query time in command line is about 1 second for 20000 rows. but in web application, it takes long. is anyone know which causing this problem? and better way to improve it?Below is my php code to retrieve the data:
select * from table where Date between '2010-01-01' and '2010-12-31'
$result1 = mysql_query($query1) or die('Query failed: ' . mysql_error());
while ($line = mysql_fetch_assoc($result1)) {
echo "\t\t<tr>\n";
$Data['Date'] = $line['Date'];
$Data['Time'] = $line['Time'];
$Data['Serial_No'] = $line['Serial_No'];
$Data['Department'] = $line['Department'];
$Data['Team'] = $line['Team'];
foreach ($Data as $col_value) {
echo "\t\t\t<td>$col_value</td>\n";
};
echo "\t\t</tr>\n";
}
Try adding an index to your date column.
Also, it's a good idea to learn about the EXPLAIN command.
As mentioned in the comments above, 1 second is still pretty long for your results.
You might consider putting all your output into a single variable and then echoing the variable once the loop is complete.
Also, browsers wait for tables to be completely formed before showing them, so that will slow your results (at least slow the process of building the results in the browser). A list may work better - or better yet a paged view if possible (as recommended in other answers).
It's not PHP that's causing it to be slow, but the browser itself rendering a huge page. Why do you have to display all that data anyway? You should paginate the results instead.
Try constructing a static HTML page with 20,000 table elements. You'll see how slow it is.
You can also improve that code:
while ($line = mysql_fetch_assoc($result1)) {
echo "\t\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t\t<td>$col_value</td>\n";
flush(); // optional, but gives your program a sense of responsiveness
}
echo "\t\t</tr>\n";
}
In addition, you should increase your acceptance rate.
You could time any steps of the script, by echoing the time before and after connecting to the database, running the query and outputting the code.
This will tell you how long the different steps will take. You may find out that it is indeed the traffic causing the delay and not the query.
On the other hand, when you got a table with millions of records, retreiving 20000 of them can take a long time, even when it is indexed. 20 minutes is extreme, though...