Update rows of a SQL table - php

I have a table with a column called 'status'. The defaut value of 'status' is 0. I want to update the value to '1' after using it.
I basically want to check if the status is 0, if it is, do an operation and then change the value to 1.
Here is the code. All works perfectly except that the value of 0 is not changed to 1.
I am a novice so maybe is a very basic mistake :(
<?php
$sql = "SELECT number, status FROM summonerid";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$SummonerID = $row["number"];
$status = $row["status"];
if($status=='0'){
$recentgames=$lol->getRecentGames($SummonerID);
$MatchID1=$recentgames->games[0]->gameId;
$sql = "INSERT INTO matchid (number) SELECT * FROM (SELECT '$MatchID1') AS tmp WHERE NOT EXISTS (SELECT number FROM matchid WHERE number = '$MatchID1') LIMIT 1;";
$sql = "UPDATE summonerid SET status='1' WHERE status='0';"; // THIS IS THE PART THAT DOES NOT WORK WELL
}
}
}
?>
Any help would be highly appreciated

Try this.. you are not executing the sql statements
$sql = "SELECT number, status FROM summonerid";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$SummonerID = $row["number"];
$status = $row["status"];
if($status=='0'){
$recentgames=$lol->getRecentGames($SummonerID);
$MatchID1=$recentgames->games[0]->gameId;
$sql = "INSERT INTO matchid (number) SELECT * FROM (SELECT '$MatchID1') AS tmp WHERE NOT EXISTS (SELECT number FROM matchid WHERE number = '$MatchID1') LIMIT 1;";
$result1 = $conn->query($sql);
$sql = "UPDATE summonerid SET status='1' WHERE status='0';";
$result1 = $conn->query($sql);
}
}
}
?>

Related

Expiry date IF santance is not working with MYSQL UPDATE

I have table with many rows. I have exp date and I want to change status of those rows where date is already exp.
It work well if I do not use UPDATE. If I just echo them. But when I want to UPDATE status of this row to 0, problems start. My problem is that it change only 1 row and not all of them that needs to be whit status 0.
$sql = "SELECT * FROM table";
$result = mysqli_query($conn, $sql);
$resultcheck = mysqli_num_rows($result);
$id = $row['id'];
if($resultcheck > 0) {
while($row = mysqli_fetch_assoc($result)) {
$expdate = $row['date'];
$exp = strtotime($date);
$today = date('m/d/Y');
$td = strtotime($today);
if($td>$exp) {
$status=0;
$sql = " UPDATE table SET status = '$status' WHERE ID = '$id' ";
$result = mysqli_query($conn, $sql);
}
}
}
Any advice how to fix that, I tried several option but nothing worked.
Use row['id'] instead of $id.
$sql = " UPDATE table SET status = '$status' WHERE ID = 'row['id']' ";
Rename $result for update. You are reusing the same variable name, hence, after a success update, it will set $result to true and the while loop will stop.

PHP not displaying result from MYSQL query

I have the following code
$sql = "SET #uid := (SELECT ID FROM channels WHERE Used = 0 ORDER BY RAND() LIMIT 1);";
$sql = "UPDATE channels SET Used = 1 WHERE ID = #uid;";
$sql = "SELECT * FROM channels WHERE ID IN = #uid;";
$result = mysqli_multi_query($conn, $sql)
or die( mysqli_error($sql) );
if (mysqli_num_rows($result) > 0) {
$text = '';
while($row = mysqli_fetch_assoc($result)) {
$Channel_Location = $row['Channel_Location'];
$text = $text . $Channel_Location;
}
}
Now the issue i'm having is the php isnt displaying the result returned by the MYSQL query which is stored in a session later on in the code to be displayed on a dummy page it comes up with the following error
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result
The my SQL query does exactly what I need it to I just need to, so I don't really want to change it. I just need some advice on how i'd get the PHP to echo the #uid
is there anyone willing to help me solve the issue? if so thankyou.
You have 3 queries in your $sql so you should use multi_query function
http://php.net/manual/en/mysqli.multi-query.php
And you can change your first query to:
SET #uid = 0;
SELECT #uid := ID FROM channels WHERE Used = 0 ORDER BY RAND() LIMIT 1);
Update You can try this fragment of your code modified with all commented improvements.
$sql = 'SET #uid = 0;';
$sql .= 'SELECT #uid:= ID FROM channels WHERE Used = 0 ORDER BY RAND() LIMIT 1);';
$sql .= 'UPDATE channels SET Used = 1 WHERE ID = #uid;';
$sql .= 'SELECT * FROM channels WHERE ID IN = #uid;';
if (mysqli_multi_query($conn, $sql)) {
do {
$result = mysqli_store_result($conn);
} while(mysqli_next_result($conn));
if (mysqli_num_rows($result) > 0) {
$text = '';
while($row = mysqli_fetch_assoc($result)) {
$Channel_Location = $row['Channel_Location'];
$text = $text . $Channel_Location;
}
}
} else {
die( mysqli_error($conn) );
}

why does my while loop stop after executed another query inside?closed

my while loop stops after executed another query inside... can you correct my codes? I want to update the column status in table ordered_items_supplier to "Pending" when the pi_number is found in the table purchased_items_supplier and if not found the column status is "Active".
$sql2 = "select * from ordered_items_supplier";
$result = $connect->query($sql2);
if($result->num_rows > 0){
while ($row = $result->fetch_assoc()) {
$pi_number = $row['pi_number'];
$sql = "select * from purchased_items_supplier where pi_number = '$pi_number'";
$result = $connect->query($sql);
if($result->num_rows > 0){
while ($row2 = $result->fetch_assoc()) {
$pi_number = $row2['pi_number'];
$sql = "update ordered_items_supplier set status = 'Pending' where pi_number = '$pi_number'";
$query = $connect->query($sql);
}
}else{
$sql = "update ordered_items_supplier set status = 'Delivered' where pi_number = '$pi_number'";
$query = $connect->query($sql);
}
}
}
here's my mysql.. it should update the status "Delivered" in ID 11
The problem is overwriting the same variable each time.
Check that you use $result for the outer and inner query both.That's why the problem occur. So don't overwriting the $result variable.

Random row selection using MySQL returns NULL

I am trying to get a random row from MySQL table but all three attemps:
$query = "SELECT cid FROM table LIMIT 1 OFFSET ".rand(1,$num_rows);
$query = "SELECT cid FROM table OFFSET RANDOM() * (SELECT COUNT(*) FROM table) LIMIT 1";
$query = "SELECT * FROM table ORDER BY RAND() LIMIT 1";
give a NULL result in mysql_query($query).
Higher up my PHP code I obtain a row from the same table OK by specifying WHERE, so I don't understand why I can't retrieve a random one.
Here is the code snippet:
$query = "SELECT uid,clu FROM uable WHERE un = '$un'";
$result = mysql_query($query) or die(sqlerror(__LINE__,mysql_errno(),mysql_error()));
$resultid = mysql_fetch_assoc($result);
$uid = $resultid['uid'];
file_put_contents('debugging.txt',__LINE__.' - $uid = '.var_export($uid,true).PHP_EOL,FILE_APPEND);
$query = "SELECT * FROM table WHERE uid = $uid AND cn = '$cn'";
$result = mysql_query($query) or die(sqlerror(__LINE__,mysql_errno(),mysql_error()));
$cr = mysql_fetch_assoc($result);
$cid= $cr['cid'];
file_put_contents('debugging.txt',__LINE__.' - $cid= '.var_export($cid,true).PHP_EOL,FILE_APPEND);
$query = "SELECT * FROM fable WHERE cid= '$cid'";
$result = mysql_query($query) or die(sqlerror(__LINE__,mysql_errno(),mysql_error()));
file_put_contents('debugging.txt',__LINE__.' - $result = '.var_export($result,true).PHP_EOL,FILE_APPEND);
$fr = mysql_fetch_assoc($result);
file_put_contents('debugging.txt',__LINE__.' - $fr = '.var_export($fr,true).PHP_EOL,FILE_APPEND);
echo '<form action="'.$_SERVER['PHP_SELF'].’" method="post">';
if (!$fr) {
$o= $cn;
while ($o= $cn) {
// $ac = mysql_query("SELECT * FROM table") or die(sqlerror(__LINE__,mysql_errno(),mysql_error()));
// $num_rows = mysql_num_rows($ac);
//file_put_contents('debugging.txt',__LINE__.' - $num_rows = '.$num_rows.PHP_EOL,FILE_APPEND);
// --$num_rows;
// $query = "SELECT cid FROM table LIMIT 1 OFFSET ".rand(1,$num_rows);
$query = "SELECT cid FROM table OFFSET RANDOM() * (SELECT COUNT(*) FROM table) LIMIT 1";
// $query = "SELECT * FROM table ORDER BY RAND() LIMIT 1";
$resultid = mysql_query($query) or die(sqlerror(__LINE__,mysql_errno(),mysql_error()));
$opr = mysql_fetch_assoc($resultid);
$o= $opr['cn'];
}
file_put_contents('debugging.txt',__LINE__.' - $query = '.$query.PHP_EOL,FILE_APPEND);
file_put_contents('debugging.txt',__LINE__.' - $resultid = '.var_export($resultid,true).PHP_EOL,FILE_APPEND);
file_put_contents('debugging.txt',__LINE__.' - $op[\'cid\'] = '.$op['cid'].PHP_EOL,FILE_APPEND);
$query = "SELECT * FROM table WHERE cid= ".$op;
$result = mysql_query($query) or die(sqlerror(__LINE__,mysql_errno(),mysql_error()));
$opr = mysql_fetch_assoc($opr);
$o= $opr['cn'];
$od= $opr['description'];
echo '<p>'.$op;
if ($od<> '') {
echo ','.$odesc;
}
echo '</p>';
echo '<input type="submit" name="continue" id="continue" value="Continue">';
} else {
echo '<p>'.$fr['p'].'</p>';
echo '<input type="submit" name="continue" id="continue" value="Continue">';
}
echo '</form>';
The resulting debugging.txt:
24 - $uid = '4'
29 - $cid = '21'
32 - $result = NULL
34 - $fr = false
These queries look OK, but I think you're starting at the wrong place. When you're uncertain how to frame something in SQL, open up a SQL client like SequelPro or Navicat and try writing a few queries by hand until you get the result you want. (Also this gives you a chance to double-check the contents of relevant tables and ensure the expected data are there.) Then you can go back into the PHP with full confidence that the SQL code is correct, so if there's a problem it must be with the PHP (either the variables you inject into a Mysql statement, or the way you call that statement).

Show all data in column

I am trying to show all of the data in the 'status' column of my table but am having troubles. What am I doing wrong:
<?php
$query1 = "SELECT id, status FROM alerts WHERE customerid='".$_SESSION['customerid']."' ORDER BY id LIMIT $start, $limit ";
$result = mysql_query($query1);
while ($row = mysql_fetch_array($result))
{
echo $row['status'] ;
}
?>
Try this:
$query1 = "SELECT id, `status` FROM alerts WHERE customerid='".$_SESSION['customerid']."' ORDER BY id LIMIT $start, $limit ";
$result = mysql_query($query1) or die(mysql_error());
while ($row = mysql_fetch_array($result))
{
echo $row['status'];
}
Also, make sure that:
$_SESSION['customerid'], $start and $limit are not empty. You can test the constructed query with echo $query1;
Note: Addition of mysql_error() in in the mysql_query will allow you to see if there is an error in the query.
I am trying to show all of the data in
the 'status' column of my table
If you want to show all the rows, your query should be:
$query1 = "SELECT id, `status` FROM alerts ORDER BY id";
But if you want to show for a specific customer, your query should be:
$query1 = "SELECT id, `status` FROM alerts WHERE customerid='".$_SESSION['customerid']."' ORDER BY id";

Categories