I have a table, with columns _photo(string) and f1(int)(this is users sex).
If _photo is empty I want to populate it with a string based on what column f1 contains.
I have this working fine for when a new user joins up, by doing this -
$sex = mysql_result(mysql_query("SELECT `f1` FROM `{$dbtable_prefix}user_profiles` WHERE `fk_user_id`='".$_SESSION[_LICENSE_KEY_]['user']['reg_id']."' "),0);
And then
if ($sex =='1') {
$no_photo = "no_photo_male.png";
}
if ($sex =='2') {
$no_photo = "no_photo_female.png";
}
if ($sex =='3') {
$no_photo = "no_photo_couple_ff.png";
}
if ($sex =='4') {
$no_photo = "no_photo_couple_mm.png";
}
if ($sex =='5') {
$no_photo = "no_photo_couple_mf.png";
}
$insertphoto= "UPDATE dsb_user_profiles SET _photo = '$no_photo' Where `fk_user_id` = '".$_SESSION[_LICENSE_KEY_]['user']['reg_id']. "' "; // note the use of reg_id and not user_id
if (!($res=#mysql_query($insertphoto))) {trigger_error(mysql_error(),E_USER_ERROR);}
I am trying to make a script that I can run on the database to update all the records to assign the correct string to all the records where _photo is empty.
I am new to mysql and php and can't work out how to cycle through each record and check f1, run the code to set the no_photo variable than insert it into the _photo column.
Any help is greatly appreciated!
Thanks.
You could loop trough records like this:
$userResult = mysql_query("SELECT `f1` FROM `{$dbtable_prefix}user_profiles` WHERE");
while($user = mysql_fetch_array($userResult)) {
if($user['f1'] == '1'){
$no_photo = "no_photo_male.png";
}
else if($user['f1'] == '2'){
//more else ifs
}
//your update query
}
Please try below update query:-
UPDATE Table_Name
SET _photo =
CASE
WHEN f1 = '1'
THEN "no_photo_male.png"
WHEN f1 = '2'
THEN "no_photo_female.png"
WHEN f1 = '3'
THEN "no_photo_couple_ff.png"
WHEN f1 = '4'
THEN "no_photo_couple_mm.png"
WHEN f1 = '5'
THEN "no_photo_couple_mf.png"
ELSE NULL
END
WHERE _photo = ''
You can update the database directly w/o PHP using nested MySQL IF() statements:
UPDATE user_profiles
SET _photo = IF(sex = 1, 'photo_1', IF(sex = 2, 'photo_2', IF(sex = 3, 'photo_3', IF(sex = 4, 'photo_4', NULL ) ) ) )
WHERE photo IS NULL;
Related
This is my table with name and unique numberenter image description here
This is my PHP code
$id = explode(",", $params["txtID"]);
for ($i = 0; $i <count($id); $i++) {
$sql = "SELECT 'auto_increment' as LastID FROM
INFORMATION_SCHEMA.TABLES WHERE table_name = 'esi_master' ";
$result = $this->con->query($sql);
if (intval($result->rowCount($result)) > 0) {
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
$lclId = intval($row["LastID"]) - 1 + intval('1');
$lclDcno = '00'.$lclId ;
echo $row["LastID"];
}
} else {
$lclId = intval($row["LastID"]) + intval('1');
$lclDcno = '00'.$lclId ;
echo $row["LastID"];
}
$sql = $this->con->prepare("UPDATE esi_master SET esi_status = 1,
esi_dcno = '$lclDcno[$i]' Where esi_id = '$id[$i]'");
echo $result = $sql->execute();
}
Here First I insert Names to the table using Insert Statements and next for some operation I update the Unique number unique number should start updating from the empty row, update done with unique id, The txtID contains ID`s like 1, 2, 3, 4 so on as I select table row in front end, in that basis that updates.Thanks in Advance.
You need another field to save, with type varchar(10).
data will not be able to save into integer(auto_increment)
How to get count of the sql search query
$u = new user();
$sql = "SELECT a.id FROM `accounts` AS a LEFT JOIN `users` AS u ON a.id = u.id WHERE a.id IS NOT NULL ";
$gender = $this->input->post('gender');
if($gender != NULL)
{
$sql .= "AND u.gender = '".$gender."' ";
}
$u->query($sql);
How to get count of the query results in $u->query($sql); .I need to set a validation on it. If the query results count is 0 , i need to set a message. Im using PHP Codeigniter ,datamapper library.
Thank you!!!
Just using count() function like this
...
$result = $u->query($sql);
$total_data = count($result);
if($u->exists())
{
echo "Found" // Do something
}
else
{
echo "Nothing found" //Do something
}
$result = $this->db->get();
For Codeigniter use $count = $result->num_rows(); // HERE IS YOUR COUNT
For OOP PHP use $count = $result->num_rows;
I am trying to find the most efficient (or proper if you prefer) way to filter a MySQL query.
What I am trying to achieve is from a list of around 20+ checkboxes (which I am using to filter the results) to see which one of them are "checked" and if they are "checked" to add them after the WHERE clause.
When they are "checked" the $var = '1' else it is NULL.
What I did until now was mix them together but with 20+ filters that's like o_O
if ($var1 == '1') {
$query = "SELECT * FROM table WHERE var1 = '1';";
..
} elseif ($var2 == '1') {
$query = "SELECT * FROM table WHERE var2 = '1';";
..
} elseif ($var1 == '1' AND $var2 == '1') {
$query = "SELECT * FROM table WHERE var1 = '1' AND var2 = '1';";
..
}
Combination of 20+ filters seems a bit stupid this way.. Is there a simpler way like put an IF $var1 != NULL THEN WHERE var1 = '1'?
Thank you!
This should do what you need:
$var1 = '1';
$var2 = '0';
$var3 = '1';
$where = array('1 = 1');
foreach (array('var1', 'var2', 'var3') as $var)
{
if ('1' == $$var)
{
$where[] = "$var = '1'";
}
}
// SELECT * FROM table WHERE 1 = 1 AND var1 = '1' AND var3 = '1'
echo $query = 'SELECT * FROM table WHERE ' . implode(' AND ', $where);
Suppose you have named your checkboxes as array of cb, so you can use their value as,
$cb=$_POST['cb'];
$q='';
for($i=0;$i<count($cb);$i++)
{
if(isset($cb[$i])&&$cb[$i]!='')
{
$q.=' AND var'.($i+1).'=1';
}
}
$q=trim(' AND');//removes AND from start of string
$query="SELECT * FROM table where $q";
There are several intersting solutions here; this one will work based on a numbered var scheme and add any variable (if it has length) to the query, not just 1.
You can see it working here: https://eval.in/94701
Here are some setup variables:
$numberOfVars = 3;
$var1=4;
$var2=5;
$var3='';
Note that $thevar actually holds the value of var1, var2, etc for each iteration:
$query = "SELECT * FROM table WHERE 1=1 ";
for($i=1;$i<=$numberOfVars;$i++) {
$thevar = ${'var'.$i};
if ( strLen($thevar) ) {
$query .= " AND var$i = '$thevar' ";
}
}
for($i=1;$i<=20;$i++)
{
if (${'var'.$i} == '1' && ${'var'.$i+1} == '1')
{
$query = "SELECT * FROM table WHERE var$i = '1' AND var.$i+1 = '1';";
}
else if (${'var'.$i} == '1')
{
$query = "SELECT * FROM table WHERE var$i = '1';";
}
}
demo
I need to access data from 2 different rows returned in the same array
Here is a picture
I need to access first_name and last_name
Here is my script
<?php
ini_set('display_errors', 1);
include_once('db.php'); // database class
$dbase['host'] = 'asdf';
$dbase['user'] = 'asdf';
$dbase['pass'] = 'asdf';
$dbase['name'] = 'asdf';
$db = new DB($dbase);
// SELECT `meta_value` FROM `wp_usermeta` WHERE `meta_key`='password'
$result = $db->query("SELECT * FROM `wp_usermeta` WHERE `meta_key`='user_email'");
while ($row = $result->fetch_array()) {
echo $row['meta_value'];
}
?>
Any help on this issue would be appreciated greatly!
Try this query..
SELECT
wp1.meta_value AS first_name,
wp2.meta_value AS last_name
FROM
wp_usermeta wp1
INNER JOIN
wp_usermeta wp2
ON ( wp1.user_id = wp2.user_id )
WHERE 1
AND wp1.meta_key = "first_name"
AND wp2.meta_key = "last_name";
GROUP BY wp1.user_id;
You're already looping over these rows, just inspect the meta_key.
$fname="";
$lname="";
while ($row = $result->fetch_array())
{
if ($row['meta_key'] == "first_name")
{
$fname = $row['meta_value'];
}
else if ($row['meta_key'] == "last_name")
{
$lname = $row['meta_value'];
}
}
echo $fname . " " . $lname;
lastly, your sql is incorrect:
SELECT * FROM `wp_usermeta` WHERE `user_id` IN (SELECT `user_id` FROM `wp_usermeta` WHERE `meta_key` = 'user_email' AND `meta_value` = "$user_email_passed_in")
In this case the MySQL query that you're doing is wrong.
It might be
$result = $db->query("SELECT * FROM `wp_usermeta` WHERE (`meta_key`='first_name' OR `meta_key`='last_name')");
In this case all rows matching 'first_name' OR 'last_name' in 'meta_key' field will be returned.
I think you'll have to distinguish these fields using also user_id field as discriminant.
Best regards
I am currently struggling with the following :
$res = $db->uniquequery("SELECT distinct won_current,ctt,darkmatter FROM ".USERS." WHERE `id` = '".$USER['id']."'");
$checkwon = $res['won_current'];
$ct=$res['ctt'];
$dm=$res['darkmatter'];
These appear to be working. Now...
$dmgift=0;
while($checkwon>=1000)
{
$ct=$ct+1;
//incrementing $ct doesnt seem to work in the loop but it works fine outside it
$checkwon=$checkwon-1000;
//threshold ranges and setting of dmgift.
switch($ct){
case $ct=1 : $dmgift=25000;break;
case $ct>=5 && $ct<10: $dmgift=50000;break;
case $ct>=10 && $ct<15: $dmgift=75000;break;
case $ct>=15 && $ct<20: $dmgift=100000;break;
case $ct>=20 : $dmgift=150000;break;
}
$dm=$dm+$dmgift;
//$db->query("UPDATE ".USERS." SET won_current=$checkwon,ctt=$checkthreshold,darkmatter=$dm WHERE `id` = ".$USER['id'].";");
$db->query("UPDATE ".USERS." SET won_current=$checkwon WHERE `id` = ".$USER['id'].";");
$db->query("UPDATE ".USERS." SET ctt='$ct' WHERE `id` = ".$USER['id'].";"); // this update query is not passing.db field remains the same
$db->query("UPDATE ".USERS." SET darkmatter=$dm WHERE `id` = ".$USER['id'].";");
}
Kindly advise...the other 2 update queries are passing well...if you notice above the 3 updates I had a full query commented in...split it to see what is not working in update...
I did echo and vardump...outside the loop they gave me the correct values...BUT inside the loop they have me 11111 instead of 5...for example...not sure what I'm doing wrong....I'm very new to php ( 2 days ? ) and would appreciate a solution...
The problem is with the switch.
switch($ct){
case $ct=1 : $dmgift=25000;break;
...
}
The first case changes $ct to 1, so its value is always 1 in the SQL query so it looks like the query doesn't work.
In any case switch doesn't work like that, you need separate if phrases:
if( $ct == 1 ) {
$dmgift=25000
}
else if( $ct>=5 && $ct<10 ) {
$dmgift=50000;
}
else if( $ct>=10 && $ct<15 ) {
$dmgift=75000;
}
else if( $ct>=15 && $ct<20 ) {
$dmgift=100000;
}
else if( $ct>=20 ) {
$dmgift=150000;
}
Also note that you don't have cases for $ct between 2 and 4.
Check for $ct value.
If $checkwon is not greater than or equal to 1000, $ct value will remain the same db value
$db->query("UPDATE ".USERS." SET ctt='" . $ct . "' WHERE `id` = ".$USER['id'].";");
Change your update query
$db->query("UPDATE ".USERS." SET won_current = '".$checkwon."' WHERE `id` = '".$USER['id']."'");
$db->query("UPDATE ".USERS." SET ctt = '".$ct."' WHERE `id` = '".$USER['id']."'");
$db->query("UPDATE ".USERS." SET darkmatter = '".$dm."' WHERE `id` = '".$USER['id']."'");
Remove ";" semicolon from here
use single quotes for values of MySQL
$db->query("UPDATE ".USERS." SET won_current='$checkwon' WHERE id = '".$USER['id']."'");