select based on random value if not exist change value php - php

I'm trying to select values from database where date is equal to random date and sometimes there is no records how can I run the select again with another random value without refreshing website?
$start = "2017-08-12";
$now = date("Y-m-d");
$now = date('Y-m-d', strtotime($now. ' - 7 days'));
$daty = array();
while($start<$now){
$start = date('Y-m-d', strtotime($start. ' + 7 days'));
$daty[] = $start;
}
$rand_keys = array_rand($daty, 2);
$random_data = $daty[$rand_keys[0]] ;
$sql2= 'SELECT count(*) as total from tab where
Date="'.$random_data.'"';
$result12 = mysqli_query( $conn, $sql2 );
$row12 = $result12->fetch_assoc();
if($row12>0){
$sql1 = 'select * from premier1718 where Date="'.$random_data.'"';
$result1 = mysqli_query( $conn, $sql1 );
while($row1 = $result1->fetch_assoc()){
echo $row1["HomeTeam"]." ".$row1["AwayTeam"]."</br>";
}

I think you can possibly do this in one query like this
$sql = 'select * from `premier1718` where `date`=(
select `date` from `tab` where `date` in ( "'.implode( '","', $daty ).'" ) order by rand() limit 1
)';
$result = mysqli_query( $conn, $sql );
if( $result ){
while( $rs=$result->fetch_object() ) printf('%s %s<br />',$rs->hometeam,$rs->awayteam);
}

$rand_keys = array_rand($daty, 2);
$random_data = $daty[$rand_keys[0]] ;
$sql2= 'SELECT count(*) as total from tab where
Date="'.$random_data.'"';
$result12 = mysqli_query( $conn, $sql2 );
$row12 = $result12->fetch_assoc();
if($row12>0){
$success = false;
while ($success == false) {
$sql1 = 'select * from premier1718 where Date="'.$random_data.'"';
$result1 = mysqli_query( $conn, $sql1 );
while($row1 = $result1->fetch_assoc()){
$success = true;
echo $row1["HomeTeam"]." ".$row1["AwayTeam"]."</br>";
}
}

Related

SQLSRV bind param thats in mysql

I'm trying to convert this mysql code to work using sqlsrv
$planeId = $_GET["pid"];
$conn = OpenCon();
$sql = "SELECT id, pid, fullname, tat, date, engine1, engine2, engine3, engine4 FROM oil WHERE pid = ? order by date desc";
$stmnt = $conn->prepare($sql);
$stmnt->bind_param("s", $planeId);
$stmnt->bind_result($id, $pid, $fullname, $tat, $date, $engine1, $engine2, $engine3, $engine4);
$stmnt->execute();
$theRows = Array();
while ( $stmnt->fetch() )
{
$aRow['id'] = "$id";
$aRow['pid'] = "$pid";
$aRow['fullname'] = $fullname;
$aRow['tat'] = $tat;
$aRow['date'] = $date;
$aRow['engine1'] = $engine1;
$aRow['engine2'] = $engine2;
$aRow['engine3'] = $engine3;
$aRow['engine4'] = $engine4;
$theRows[] = $aRow;
}
$stmnt->close();
echo json_encode($theRows);
CloseCon($conn);
This I what I've done so far but I'm missing the bind-param function not sure how to implement that. Because the output keeps coming out like this
[{"id":"","pid":"","fullname":null,"tat":null,"date":null,"engine1":null,"engine2":null,"engine3":null,"engine4":null}]
Even though I know there's an entry in Microsoft DB
$planeId = $_GET["pid"];
$theRows = Array();
$conn = OpenCon();
$query = "SELECT id, pid, fullname, tat, date, engine1, engine2, engine3, engine4 FROM oil WHERE pid = ? order by date desc";
//$stmnt = $conn->prepare($query);
$stmnt = sqlsrv_prepare($conn, $query, array(&$planeId));
if (sqlsrv_execute($stmnt) === false){
die( print_r( sqlsrv_errors(), true));
}
else{
while ( sqlsrv_fetch($stmnt) )
{
$aRow['id'] = "$id";
$aRow['pid'] = "$pid";
$aRow['fullname'] = $fullname;
$aRow['tat'] = $tat;
$aRow['date'] = $date;
$aRow['engine1'] = $engine1;
$aRow['engine2'] = $engine2;
$aRow['engine3'] = $engine3;
$aRow['engine4'] = $engine4;
$theRows[] = $aRow;
}
echo json_encode($theRows);
}
CloseCon($conn);

How to use three conditions with AND operator in while loop in PHP

How to use three conditions with AND operator in while loop in PHP
Here is my code:
$query = "select * from agent_profile where status = 1";
$run = mysqli_query($con, $query);
$pricequery = "select * from airport_parking where airport_name = '$airport' AND '$parking_start_time' >= start_time ";
$resultprice = mysqli_query($con, $pricequery);
$squery = "select * from agent_services where agent_id = '$crnt_id' AND service_type = '$service_type' ";
$srun = mysqli_query($con, $squery);
if ($run && $resultprice && $squery) {
while (($datarow = mysqli_fetch_array($run)) AND ($getdata = mysqli_fetch_array($resultprice)) AND ($sdata = mysqli_fetch_array($srun))) {
}
Try This
$query = "select * from agent_profile where status=1
UNION
select * from airport_parking where airport_name = '$airport' AND '$parking_start_time' >= start_time
UNION
select * from agent_services where agent_id = '$crnt_id' AND service_type='$service_type';";
$run = mysqli_query($con,$query);
if(strlen($run) <> ''){
while (($datarow = mysqli_fetch_array($run)))
{
$column_name = $datarow['column_name'];
}
}

PHP SQL Result stored as var in new sql query

So its a php game etc...
This is the main part of my script;
$sql = "SELECT * from search1 WHERE username='Hitachi'";
$result = mysqli_query($connection, $sql);
while ($rows = mysqli_fetch_array($result)) {
$timeleft = $rows['time'];
$target55 = $rows['target'];
$id = $rows['id'];
$status = $rows['status'];
$hours = $rows['hours'];
$started = $rows['started'];
$deletetime = $started - 86400;
mysqli_query($connection, "DELETE FROM search WHERE started<'$deletetime'");
$starttime = $started;
$time1 = $starttime + 10800;
$a_query = mysqli_query($connection, "SELECT * FROM BG WHERE username='$target55' AND status='Ready'");
$check_found = mysqli_num_rows($a_query);
if ($check_found >= 1) {
$bg = mysqli_query($connection, "SELECT * FROM BG WHERE status='Ready' AND username='$target55' ORDER by slot DESC LIMIT 1");
while ($i = mysqli_fetch_object($bg)) {
$bg1 = $i->bodyguard;
}
echo "This user has a bodyguard called <b>$bg1</b>!";
if ($time1 < time() && $hours >= 3 || $userlevel >= 14) {
$sql2 = "SELECT * from users WHERE username='$target55'";
$result2 = mysqli_query($connection, $sql2);
while ($rows2 = mysqli_fetch_array($result2)) {
$target_country = $rows2['country'];
}
mysqli_query($connection, "UPDATE search1 SET status='2', location='$target_country', found='$target_country' WHERE id='$id'");
}
mysqli_query($connection, "DELETE FROM BG WHERE username='$target55'");
This does nothing at all regardless if there is a bodyguard or not.
If i remove the check for the bodyguard etc then it works however i need it to check?

PHP, using Joomla, algorithm, networking if else,

Hi, I have a question, when i ever insert this code
if ($pointsreach>=$tempunilevel ){
mysql_query("UPDATE `incentives_table` SET pointsreach = pointsreach +500 where userid = '$newsponsorid' ");
the if statement does not work anymore
if (count($items) >0 && $lev <= 10 ){ // found get sponsor id for the next computation
$sponsorid = $items->upline; //sponsor id
but when i remove the
if ($pointsreach>=$tempunilevel ){
mysql_query("UPDATE `incentives_table` SET pointsreach = pointsreach +500 where userid = '$newsponsorid' ");
the statement works perfectly ...
$query = " UPDATE `#__eds_incentives_table` SET `temp` = `temp` + 25 where `userid` = '$sponsorid' ";
$db->setQuery($query);
$db->query();
$mote = $this->entryuni($sponsorid, $lev + 1, +25);
}
else {
return 'ok';
}
This is the code
public function entryuni($newsponsorid = null, $lev = 2, $fpv = 0)
{
$db = & JFactory::getDBO();
$query = "SELECT upline,fslot FROM `table` where userid = '$newsponsorid' ";
$db->setQuery($query);
$items = $db->loadObject();
$items = (!empty($items)) ? $items : array();
$queryreach = mysql_query("SELECT * FROM incentives_table WHERE userid = '$newsponsorid' ");
$fetchreach = mysql_fetch_array($queryreach);
$pointsreach=$fetchreach['pointsreach'];
$tempunilevel=$fetchreach['temp_unilevel'];
if ($pointsreach>=$tempunilevel ){
mysql_query("UPDATE `incentives_table` SET pointsreach = pointsreach +500 where userid = '$newsponsorid' ");
if (count($items) >0 && $lev <= 10 ){ // found get sponsor id for the next computation
$sponsorid = $items->upline; //sponsor id
$query = " UPDATE `#__eds_incentives_table` SET `temp` = `temp` + 25 where `userid` = '$sponsorid' ";
$db->setQuery($query);
$db->query();
$mote = $this->entryuni($sponsorid, $lev + 1, +25);
}
else {
return 'ok';
}

Can i create array by use php variable in a loop?

Can I create an array by use php variable in a loop ?
<?php
for($i=5;$i<=100;$i++)
{
$sql = "SELECT * FROM users WHERE id = '$i' order by id asc";
$result = mysql_query($sql);
$datas=mysql_fetch_array($result);{
$username = stripslashes(str_replace('\r\n', '<br>',($datas['username'])));
}
${'name' . $i} = $username;
}
?>
Okay , Now I have php variable $name1-$name100
And then I want to create array by use php variable $name1-$name100
$my_array = array(php variable $name1-$name100);
How can I do ?
Just modify how you store the username:
$my_array = array();
for ( $i = 5; $i <= 100; $i++ ) {
$sql = "SELECT * FROM users WHERE id = '$i' order by id asc";
$result = mysql_query($sql);
$datas = mysql_fetch_array($result);
$username = stripslashes(str_replace('\r\n', '<br>',($datas['username'])));
$my_array[] = $username; // push usernames into array
}
Also you shouldn't run so many queries, so I suggest something like this:
$usernames = array();
$query = "SELECT * FROM users WHERE id >= 5 AND id <= 100 ORDER BY id ASC";
$result = mysql_query( $query );
if ( mysql_num_rows( $result ) > 0 ) {
while ( $data = mysql_fetch_assoc( $result ) )
$usernames[] = stripslashes( str_replace( '\r\n', '<br>', $data['username'] ) );
}

Categories