multiple sql statements - php

$sql = "UPDATE user SET `niveua`='beginner' WHERE `km`< 50";
$result = $con->query($sql);
$sql = "UPDATE user SET `niveua`='trained' WHERE `km`> 50 or `km` < 99";
$result = $con->query($sql);
$sql = "UPDATE user SET `niveua`='expert' WHERE `km`> 100";
$result = $con->query($sql);
I can't get those 3 sql's to work. It's only the third one that is updating.
So how do i get all 3 to work?

Missing AND, also you can simplify your query like below, so that things can be faster
UPDATE user
SET niveua =
CASE
WHEN (`km` < 50 )
THEN 'beginner'
WHEN (`km`> 50 AND `km` < 99 )
THEN 'trained'
WHEN ( `km` > 100 )
THEN 'expert'
ELSE niveua
END;

Related

How to increase count by 100 to every data

I am trying to increase the views of every user by 100 but it updates all users view by adding the view in the last data of the table.
$views = '100';
$res_query = "SELECT * FROM inventory_test WHERE status='0' AND role='0'";
$result = mysqli_query($conn, $res_query);
foreach($result as $res){
$visiter = $res['visiter'];
$update_visiter = "UPDATE inventory_test SET visiter = '$visiter'+'$views'";
$run_update=mysqli_query($conn, $update_visiter);
}
$msg="Views has been successfully updated.";
Try this code:
$sql = "UPDATE inventory_test SET visiter = visiter + 100 WHERE status='0' AND role='0'";
mysqli_query($conn, $sql);
No need to perform a select before the update in that case.

MySQL alternative to Read/Modify/Write a field

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.

getting all mysql results that is less than given id

i am working on a timeline for my website but i am having some problem when i ran the query to select all id that is less than given identifier its still return the identifier result upon every query
example if identifier is id=4 i want to select everything less than 4 and not from 4 > 3 > 2 > 1 i want it to be 3 > 2 > 1
here is my php. i know its not secure or what not but i have written it in prepared statement and get the same thign so i need some here.
if(isSet($_POST['lastmsg']))
{
$feed_id = mysqli_real_escape_string($con, $_POST['lastmsg']);
$get1 = mysqli_query($con, 'SELECT receiver FROM connection where sender="'.$_SESSION['userid'].'"');
$id_feed = array();
while($id_result1 = mysqli_fetch_array($get1)){
$id_feed[] = $id_result1['receiver'];
$ids1 = join(',', $id_feed);
$get_feed1 = mysqli_query ($con, "select * from feed where users in '".$ids1."' or users='".$_SESSION['userid']."' and 'feed_id' < '".$feed_id."' ORDER BY feed_id DESC LIMIT 2");
}
while($res1 = mysqli_fetch_array($get_feed1)){
echo $load = $res1['feed_id'];
}
}

clear votes greater than 50 php script

I only want to reset votes greater than 50, currently is resets every one
<?
include('mysql_connect.php');
$query = "SELECT id, votes, callback FROM websites";
$result = mysql_query($query) OR die(mysql_error());
$query = "UPDATE websites SET votes = 0";
$result = mysql_query($query) OR die(mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC);
echo ('Database has been cleaned with a tissue.');
?>
Im used to coding Java so im not sure if i can use this
$query = "UPDATE websites WHERE votes >= 50 SET votes = 0";
Thanks for your help
you're close. Do this instead.
$query = "UPDATE websites SET votes = 0 WHERE votes >= 50 ";
Also mysql_* functions are deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used.
You need the WHERE statement after the SET:
$query = "UPDATE websites SET votes = 0 WHERE votes >= 50";

Repeating PHP Queries with New Select Values?

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.

Categories