I am android developer and have not much knowledge of php. currently I am working for make backend of my android application. I have quote table with about 1000 row in it. I am trying to update some row with below query.
$randomnumbershare = (rand(10,50));
$randomnumberlike = (rand(10,50));
$results = mysqli_query($mysqli,"UPDATE tbl_quotes SET qu_status=0, qu_like=$randomnumberlike,qu_share=$randomnumbershare,qu_favorite=0, qu_time=Now() ORDER BY _quid ASC ");
I want set qu_like and qu_share with random number from 10 to 50. its working fine but have only one issue that it's setting same number in all row, instead I want different number in all row. Anyone can please suggest me how can I do it ?
Thanks
The problem is you don't have a where clause, that means that the update is going to update all the rows in the table, if you want to have different values you need to do the update inside a for or while statement to repeat the update and add a where clause that matches every id of the table on every cicle.
Now you have this:
$randomnumbershare = (rand(10,50));
$randomnumberlike = (rand(10,50));
$results = mysqli_query($mysqli,"UPDATE tbl_quotes SET qu_status=0, qu_like=$randomnumberlike,qu_share=$randomnumbershare,qu_favorite=0, qu_time=Now() ORDER BY _quid ASC ");
But you need something like:
If you have a numeric id that goes from 1 to 1000 you can do something like
for ($x = 1; $x <= 1000; $x++)
{
$randomnumbershare = (rand(10,50));
$randomnumberlike = (rand(10,50));
$results = mysqli_query($mysqli,"UPDATE tbl_quotes SET qu_status=0, qu_like=$randomnumberlike,qu_share=$randomnumbershare,qu_favorite=0, qu_time=Now() where id=$x ORDER BY _quid ASC ");
}
That code do your update 1000 times, one update for each row you have in you table, now if you have a varchar id that is somethin more complex than a numeric sequence you need to add a query before that stores that ids into an array, something like:
$sql = mysql_query("select id from table_name");
$tableinfo = array();
foreach ($tableinfo as $tableid)
{
$randomnumbershare = (rand(10,50));
$randomnumberlike = (rand(10,50));
$results = mysqli_query($mysqli,"UPDATE tbl_quotes SET qu_status=0, qu_like=$randomnumberlike,qu_share=$randomnumbershare,qu_favorite=0, qu_time=Now() where id=$tableid[id] ORDER BY _quid ASC ");
}
For n number of row thinking you have a sequence from 1 to x number you can do:
$sql = mysql_query("select id from table_name");
$num_rows = mysql_num_rows($sql);
for ($x = 1; $x <= $num_rows; $x++)
{
$randomnumbershare = (rand(10,50));
$randomnumberlike = (rand(10,50));
$results = mysqli_query($mysqli,"UPDATE tbl_quotes SET qu_status=0, qu_like=$randomnumberlike,qu_share=$randomnumbershare,qu_favorite=0, qu_time=Now() where id=$x ORDER BY _quid ASC ");
}
Related
Please how can I generate a unique id that increments when a new invoice is created.
For example, I have an invoice with the id NR20200001 so the Next invoice created the Id should be NR20200002.
This is my code below, I can't seem to increment the unique id I generated, so I just generated it randomly, but I need to generate it incrementally
<?php
$qry = "SELECT * from requisitions order by req_id desc";
$result = mysqli_query($connection, $qry);
$row = mysqli_fetch_array($result);
$lastid = $row['req_id'];
if($lastid == ""){
$number = "NR".date("Y").date("s");
}
else{
$length=2;
$number = substr($lastid,4);
$number = intval($number);
$number = "NR".date("Y").date("s").substr(str_shuffle(str_repeat($x='0123456789',ceil($length/strlen($x)))),1,$length);
}
?>
Firstly, you should set req_id to primary key and auto increment on database
Secondly, add LIMIT 1 for get only the latest req_id:
$qry = "SELECT * from requisitions order by req_id desc LIMIT 1";
Next, change condition follow as below:
<?php
$qry = "SELECT * from requisitions order by req_id desc LIMIT 1";
$result = mysqli_query($connection, $qry);
$row = mysqli_fetch_array($result);
if($row['req_id'] == "" || $row['req_id'] == null)
{
$lastid = 0;
}
else
{
$lastid = $row['req_id'];
}
$nextid = sprintf("%04d", $lastid+1); //set to 4 digit
$number = "NR".date("Y").$nextid;
?>
Note: Do not delete record from table because it will make duplicate invoice number. You should add field flag for active/inactive.
Please check code for increment number
6) ? $lastid : substr('00000000' . $lastid, -6);
$getValue = "NR".date("Y").date("s").$getValue;
}
?>
I need help in selecting a random data from the table
Let's suppose There are 10 columns in my table but i want that if I hit the query sometime it will bring all data and sometime it will bring data only from 1 column and some time from the no 1 and no 2 column
How can I achieve this?
Thanks in advance
function selectAllInventoriesINGroup($conn,$groupname,$import_id)
{
$cgstSgst = explode('-', $groupname);
$sql = "SELECT * FROM stockitems WHERE generalid = '$import_id' AND cgst = '$cgstSgst[0]' AND sgst = '$cgstSgst[1]' ORDER BY RAND()";
$result = $conn->query($sql);
$jsonxx = mysqli_fetch_all ($result, MYSQLI_ASSOC);
return $jsonxx ;
}
I have tested this but it fetch the all the data from the database
<?php
$sql = "SELECT * FROM stockitems WHERE generalid = '$import_id' AND cgst = '$cgstSgst[0]' AND sgst = '$cgstSgst[1]' ORDER BY RAND()";
?>
I want to get the sum total of the table columns in my database.
I've tried using the following code but have not been successful.
$link=mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_NAME);
$result = mysqli_query($link,'SELECT SUM(value) AS value_sum FROM User_Table');
$row = mysqli_fetch_assoc($result);
$sum = $row['value_sum'];
echo $sum;
Thank you very much!!
I hope you try to find total number of record of a table of User_Table
$link=mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_NAME);
$result = mysqli_query($link,'SELECT SUM(your_column_name) AS value_sum FROM User_Table');
//or like the query for return last row that indicate total number of record
// id auto increment
$result = mysqli_query($link,'SELECT * FROM User_Table ORDER BY id DESC LIMIT 1;');
$row = mysqli_fetch_assoc($result);
$sum = $row['id'];
echo $sum;
// or using count
$result = mysqli_query($link,'SELECT COUNT(*) total_row FROM User_Table;');
$row = mysqli_fetch_assoc($result);
$sum = $row['total_row '];
echo $sum;
As of my understanding you need count the number of columns your database have. If I am not wrong, you may please use the query below
select * from information_schema.columns
where table_schema = '<YOUR DATABASE NAME>'
order by table_name,ordinal_position
Hope this helps. Thanks
I want to select the maximum value of a column of my table. I'm using PHP and MySQL. This is what I have so far:
$max = "SELECT MAX(Id) FROM trialtabel2";
$max1 = mysqli_query($dblink, $max);
echo= $max1;
My debugger is just saying that it is a query returning a 0 boolean value (false). I cannot find a specific answer anywhere on the internet.
You need to fetch the data from the mysqli_result object that was returned to you when you executed your query using mysqli_query.
$max = "SELECT MAX(Id) as id FROM trialtabel2";
$max1 = mysqli_query($dblink, $max);
$row = mysqli_fetch_assoc($max1); // this was missing
$id=$row['id'];
echo $id;
Note: I removed the loop because with MAX query without any grouping you will get only 1 row returned. If you had multiple rows in result set you would need to loop through all of them
two ways
first is as people described
$max = "SELECT MAX(Id) as max_id FROM trialtabel2";
$max1 = mysqli_query($dblink, $max);
$row = mysqli_fetch_assoc($max1);
$max_id=$row['max_id'];
echo $max_id;
second is ordering and limiting
$max_id = 0;
$max = "SELECT id FROM trialtabel2 order by id desc limit 0,1";
$max1 = mysqli_query($dblink, $max);
while($row = mysqli_fetch_assoc($max1)){
$max_id=$row['id'];
}
echo $max_id;
In Your code you missing the fetch statement. you need to fetch from the resultset. see above what you are missing.
Try using this..,
$max = "SELECT MAX(Id) as maxId FROM trialtabel2";
$max1 = mysqli_query($dblink, $max);
$row = mysqli_fetch_assoc($max1);
echo $row['maxId'];
Hope this helps..
$max = "SELECT Id FROM trialtabel2 order by id desc limit 1";
$max1 = mysqli_query($dblink, $max);
$result = mysqli_fetch_assoc($max1);
pre($result);
I have the following code which selects information from one random row.
$query = mysql_query("SELECT * FROM lines_angles_shapes ORDER BY RAND() LIMIT 1 ");
while($rows = mysql_fetch_array($query))
{
$question = $rows['question'];
$hint = $rows['hint'];
$level = $rows['level'];
$keyword = $rows['keyword'];
$showme = $rows['showme'];
$picture_path = $rows['picture_path'];
}
This works well for me but I now I need to be able to select two more DIFFERENT pictures from the picture_path column and assign them to variables. Again, all three rows need to be different.
Any tips for a newbie on how to do this?
Just change your query as follows:
$query = mysql_query("SELECT * FROM lines_angles_shapes ORDER BY RAND() LIMIT 3");
You are ordering by ORD() so it will give you different records.
No, new modification, just change the limit to 3 (whatever your need).
$query = mysql_query("SELECT * FROM lines_angles_shapes ORDER BY RAND() LIMIT 3");
As your are already getting random values with order by clause it will always return different values so you just need to edit your limit value and you are done!
$query = mysql_query("SELECT * FROM lines_angles_shapes ORDER BY RAND() LIMIT 3");