Is there an easier way to do this instead of writing the same line of code 100+ times? I need the value of the field L_key each time as you will notice:
$query1 = "SELECT L_key FROM profiles WHERE v_key = '$L1_key'";
$result = mysqli_query($dbh, $query1);
if ($row = mysqli_fetch_array($result))
{
$L2_key = $row['L_Key'];
$query2 = "UPDATE profiles SET min = min + 1 WHERE v_key = '$L2_key'";
mysqli_query($dbh, $query2);
}
$query3 = "SELECT L_key FROM profiles WHERE v_key = '$L2_key'";
$result = mysqli_query($dbh, $query1);
if ($row = mysqli_fetch_array($result))
{
$L3_key = $row['L_Key'];
$query2 = "UPDATE profiles SET min = min + 1 WHERE v_key = '$L3_key'";
mysqli_query($dbh, $query2);
}
$query3 = "SELECT L_key FROM profiles WHERE v_key = '$L3_key'";
$result = mysqli_query($dbh, $query1);
if ($row = mysqli_fetch_array($result))
{
$L4_key = $row['L_Key'];
$query2 = "UPDATE profiles SET min = min + 1 WHERE v_key = '$L4_key'";
mysqli_query($dbh, $query2);
}
$query3 = "SELECT L_key FROM profiles WHERE v_key = '$L4_key'";
$result = mysqli_query($dbh, $query1);
if ($row = mysqli_fetch_array($result))
{
$L5_key = $row['L_Key'];
$query2 = "UPDATE profiles SET min = min + 1 WHERE v_key = '$L5_key'";
mysqli_query($dbh, $query2);
}
Do I use a loop? If so, can you please show me a code to execute this over and over as I am still learning and do not know what a loop is? Or, is there a different method?
You have a recursive structure in your profiles table (v_key => (l_key : v_key)=> ( l_key... )) and SQL does not really handle recusion terribly well with simple queries. Your options are to write a stored procedure or handle this with PHP. Since your a beginner, I'd imagine that PHP is far simpler for this task:
<?php
// define a variable on the outside -- we'll need it for each iteration
$lKey;
// If you know how many are going to be used, use a for loop because
// then you know you won't get some sort of nasty infinite recursion issue.
// $count is however many times this needs to operate.
for($i = 0; $i < $count; $i++ )
// while( true ) // <-- this will keep going until "break" is called.
// If you don't know how many will be used. Comment out the for loop
// and uncomment thie while loop.
{
// your initial query -- I added a limit because you only need one
// and there is no sense in doing anything more than what you need
$query1 = "SELECT L_key FROM profiles WHERE v_key = '$lKey' LIMIT 1";
$result = mysqli_query($dbh, $query1);
if ($row = mysqli_fetch_array($result)) // so far so good.
{
$lKey = $row['L_Key']; // assign that outside variable to the new key
// and run the necessary update.
$query2 = "UPDATE profiles SET min = min + 1 WHERE v_key = '$lKey'";
mysqli_query($dbh, $query2);
}
else
{
break;
}
// as of right now, $lKey is now the value from the select above.
// which means that you'll be able to start the next loop with it.
}
If I understand correctly, you can use the mysql_num_rows OR mysql_result to get the total numbers, so you can use a while:
<? $a = 0; while($total != $a) { //query $a++; } ?>
Use pdo and solve all your worries. This is cinche in pdo using prepared statements. Will even perform better.
Related
$frame_type = '';
$ret = mysqli_query($con, "select * from products where status='1' AND frame_type = '$frame_type' ");
while ($row = mysqli_fetch_array($ret)) {
$emparray[] = $row;
}
Get All Rows If The $frame_type Is Empty I am trying this way but i get zero rows , How to fix that Where $frame_type has value then send to query else not
There are a some of things that is wrong with your question (code). But if you only want answer. Just copy this
$frame_type = '';
$query = "select * from products where status='1' ";
// strlen has value
if(strlen($frame_type)) {
$query .= "AND frame_type = '$frame_type'";
}
$ret = mysqli_query($con, $query);
while ($row = mysqli_fetch_array($ret)) {
$emparray[] = $row;
}
PS: It's never a safe idea to pass everything to the query. Use prepared statement if you can.
In several PHP codes I have to just increment a field value from a MySQL DB.
Tipically, I use this snippet:
$sql = "SELECT IDpage, numPages FROM Pages WHERE IDpage=".$page;
$result = mysqli_query( $conn,$sql)
$row = mysqli_fetch_array($result);
$num = $row['numPages'] + 1;
$sql = "UPDATE Pages SET numPages=".$num." WHERE IDpage=".$page;;
$result = mysqli_query( $conn,$sql)
Is there any more elegant and concise method?
You don't need to fetch the data first, just do the update.
$sql = "UPDATE Pages SET numPages = numPages + 1 WHERE IDpage = ".$page;
$result = mysqli_query($conn, $sql);
Also, your snippet is missing a few semicolons.
I'm having problem on dividing two values and it keeps showing
Warning: Division by zero in ...
I've read and tried some post about dividing values in here but I can't still solve the problem.
This is the snippet :
$query2 = "SELECT SUM(jumlah_poin) AS jumlah_mk FROM tbl_nilai WHERE nama_mk = 'Pengantar Teknologi Informasi'
AND nip_dsn = '198'";
$result2 = mysql_query($query2);
$row2 = mysql_fetch_assoc($result2);
$query4 = "SELECT count(1) FROM tbl_nilai WHERE nama_mk = 'Pengantar Teknologi Informasi'
AND nip_dsn = '198'";
$result4 = mysql_query($query4);
$row4 = mysql_fetch_array($result4);
$sum ['jumlah2']= $row2 ['jumlah_mk'] / $row4 [0];
echo json_encode(array($resultArray,$sum));
and I need to make it in one echo. Any explanation you can provide to a newb will be appreciated. help and teach me please :D
To prevent the devide by zero error I might put it in an if statement
if( $row4[0] != 0 ) { // any falsy value would evaluate to zero
$sum ['jumlah2']= $row2 ['jumlah_mk'] / $row4 [0];
} else {
$sum = $sum_default_value;
}
There are two reasons, try one of this:
Change query to
$query4 = "SELECT count(1) as totalCount FROM tbl_nilai WHERE nama_mk ='Pengantar Teknologi Informasi' AND nip_dsn = '198'";
and
$sum ['jumlah2']= $row2 ['jumlah_mk'] / $row4 ['totalCount'];
Your 2nd query returns count as 0, if no record match.
I am trying to populate a table displaying statistics. I have a list of campaign id's have been placed into an array using the following query:
$query = "SELECT `id` FROM `c_templates` ORDER BY `id`";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$id[] = $row['id'];
}
Once I have all of my id's, I am using a foreach loop to make 5 queries per id gathering all the table data.
foreach($id as $i){
// sent
$query = "SELECT `template`, COUNT(*) as count FROM `s_log` WHERE `template` = '".$i."' AND `time_sent` BETWEEN '".$start."' AND '".$stop."'";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$template[] = $row['template'];
$count[] = $row['count'];
}
// opens
$query = "SELECT COUNT(*) as count FROM `t_opens` WHERE `campaign_id` = '".$i."' AND `timestamp` BETWEEN '".$start."' AND '".$stop."'";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$opens[] = $row['count'];
}
// clicks
$query = "SELECT `campaign_id`, COUNT(*) as count FROM `t_analytics` WHERE `campaign_id` = '".$i."' AND `timestamp` BETWEEN '".$start."' AND '".$stop."'";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$click_count[] = $row['count'];
}
// conversions
$query = "SELECT `conversion_value`, COUNT(*) as count FROM `t_analytics` WHERE `campaign_id` = '".$i."' AND `timestamp` BETWEEN '".$start."' AND '".$stop."' AND `conversion_value` > 0";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$conversion_value[] = $row['conversion_value'];
$conversion_count[] = $row['count'];
}
// bounce rate
$query = "SELECT COUNT(*) AS `ck` FROM `s_log` WHERE `time_sent` BETWEEN '".$start."' AND '".$stop."' AND `status` = 'hardbounce' OR `status` = 'softbounce' AND `template` = '".$i."'";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$bounce_count[] = $row['ck'];
}
}
The issue is that this process takes 4-5 seconds to perform over 40-50 id records. I placed some timers after each query to confirm the queries were performing correctly and they were. I ran OPTIMIZE and double checked to be sure everything was properly indexed. As far as I can tell, the issue is not in mysql.
The only thing I could find is that there is a .1-.5 second delay after each loop cycle. When trying to run 40-50 rows, this definitely begins to add up.
My question: Is there a better and faster way to fetch this data? Is there something else I should be checking to speed the process up?
The only solution I could think of was to run one query for each statistic (5 total queries) fetching the data for all id and placing them in a array for later display. I'm not sure how that could be done or if it's even possible. It seems to me I would have to run a separate query for each id, but I'm hoping I'm wrong.
Any help will be greatly appreciated!!
Quick Question;
$sql = mysql_query("SELECT * FROM cronjobs WHERE status = 0 ");
while($cronjob = mysql_fetch_array($sql)){
if($cronjob['suid'] != $cronjob['cuid']){
//echo 'not equal<br>';
$set = 0;
$sid = $cronjob['sid'];
$suid = $cronjob['suid'];
$cuid = $cronjob['cuid'];
$set = notify($sid, $suid, $cuid);
if($set==1){
//echo 'notified<br>';
$sql = "UPDATE cronjobs SET status = '1' WHERE id='".$cronjob['id']."'";
if(mysql_query($sql)){
echo '1<br>';
$set = 0;
}
}
}
}
}
notify() will return 1 (numeric)
The problem is only one iteration of the while loop is executed even though there are more records. I don't know what's wrong here. Help me out pls.
Please change inner $sql variable name to something else..outer $sql and inner one are making conflict
$sql = mysql_query("SELECT * FROM cronjobs WHERE status = 0 ");
while($cronjob = mysql_fetch_array($sql)){
if($cronjob['suid'] != $cronjob['cuid']){
//echo 'not equal<br>';
$set = 0;
$sid = $cronjob['sid'];
$suid = $cronjob['suid'];
$cuid = $cronjob['cuid'];
$set = notify($sid, $suid, $cuid);
if($set==1){
//echo 'notified<br>';
$sql_2 = "UPDATE cronjobs SET status = '1' WHERE id='".$cronjob['id']."'";
if(mysql_query($sql_2)){
echo '1<br>';
$set = 0;
}
}
}
}
}
Just an observation:
Because you have:
$sql = mysql_query("SELECT * FROM cronjobs WHERE status = 0 ");
while($cronjob = mysql_fetch_array($sql)){
Its going to execute the Query EVERY SINGLE time it goes through the loop. If you have a 100 rows, its going to execute 100 times. If you do this instead, then it executes only once.
$sql = mysql_query("SELECT * FROM cronjobs WHERE status = 0 ");
$res = mysql_fetch_array($sql);
while($cronjob = $res){
It wouldnt have conflicted either!
It is clearly an issue that occurs when you have the same variable for query ($query)
and Result Object ($result).try different name for mysql_query() inside the WHILE Loop.