The scipt should subtract 1 from the primary key (auftrag_id) from a table. But somehow it's not working. The variable $preauftrag_id is not getting decreased. The type of the variable is integer. I tried several subtraction-methods, but none is working. Someone have an idea?
$auftrag_id = $db->insert_id;
$preauftrag_id = $auftrag_id;
$preauftrag_id--;
echo "$preauftrag_id"; // Turns out the same value as $preauftrag_id = $auftrag_id;
Just another thought, if $db->insert_id returns an object, PHP variables assigned by reference.
Therefore, you've essentially assigned another way to access $auftrag_id, but you haven't taken its value:
$auftrag_id $preauftrag_id
\ /
------memory------
(sorry for the terrible graphics)
Therefore, decrementing $preauftrag_id would actually be decrementing the same reference as $auftrag_id, which may not be allowed because its a STATIC variable of the DB class?
Assuming you want the id of the second to last row (the one before the one that was just inserted), the only reliable way is to run a query. Try this:
$query = 'SELECT id
FROM table
WHERE id < '.(int) $db->insert_id.'
ORDER BY id DESC
LIMIT 1';
That should fetch the id of the row prior to the one in question.
In case of, try with integer casting :
$auftrag_id = (int) $db->insert_id;
$preauftrag_id = $auftrag_id;
$preauftrag_id--;
echo "$preauftrag_id"; // Turns out the same value as $preauftrag_id = $auftrag_id;
Related
I'm new to PHP and i want to know how i can subtract a specific amount from the results from counting the total amount of rows in a table. In this case i'd like to minus the value 3 from whatever the value of the total rows is. But i keep getting an error. Below is my code.
$cartwork = $con->query("SELECT count(*) FROM table");
$vs = '3';
$camount = $cartwork - $vs;
echo "$camount";
When the code runs i get the error "Object of class mysqli_result could not be converted to int" what can i do to fix this and get it to work properly.
The query returns a result set. You need to parse through the result set(s) in order to access the values returned. That's basically what the error states.
Please see here for documentation on the PHP function for fetching rows:
http://php.net/manual/en/function.mysql-fetch-row.php
So basically you would need
$row=$cartwork->mysql_fetch_row();
$cartWork_value = $row[0];
$vs = '3';
$camount = $cartwork_Value - $vs;
echo "$camount";
Note - this assumes that you get back exactly one result row (which should be the case with your query).
You can simply change your query to:
$cartwork = $con->query("SELECT count(*)-3 FROM table");
It doesn't smell particularly good though.
When I'm trying to get the number of rows on a SQL request and if not, the connection that follow fails.
But in any case (also if the request should return 0), it returns 1.
Here's my code :
$str = 'SELECT count(*) FROM admins WHERE mail = ? AND mdp = ?';
$arr = array($mail, $pass);
$rqt = sendRqt($str, $arr);
$tab = $rqt->fetchColumn();
$cnt = count($tab);
echo $cnt;
I don't understand why there's no time it returns 0
The problem is the use of the php function count().
You already have the correct number in your $tab variable as a string (probably, depends on php configuration / version) so you can echo it or cast it to an integer to make sure it is a number.
However, in php:
count(0) === 1
count('0') === 1
See here for example.
You should remove count($tab).
the SQL "COUNT(*)" allways returns a row with the count (quantity) of values, so if you apply the php count() function, always will return 1 because there is one row that contains the value of the COUNT sql function
I believe COUNT() needs a column name.WRONG!! count(*) should count all rows, but I still reccomend a column name like id or something. You could also use an AS to make life a little easier
$str = 'SELECT count(`COLUMNNAME`) AS cnt FROM table WHERE ....
i have an issue gettin' the value from a query:
public static function sa() {
$resul = Yii::app()->db->createCommand()->select('MAX(id)')->from('yii_availability')->execute();
$got = mysql_query($result);
$res = $got['MAX(id)'] + 1;
$rs='SA'.$res;
return "{$rs}";
}
it always return SA1, but i want get the last id and after plus 1 so in this case i have the next autoincremental id from my id column.
For example: i am creating a new registry with the field SA0000005. This number is calculated getting the last autoincremental value plus 1.
thanks for your valuable help
$resul = Yii::app()->db->createCommand()->select('MAX(id)')->from('yii_availability')->execute();
$got = mysql_query($result); // what are you even doing here
Apart from typos, that's not how you are supposed to use the query builder. Have you read the documentation on the query builder?
The probable reason why you always get SA1, is because the $got['MAX(id)'] expression is NULL. You add 1 to that. You want something like this.
// returns false on no-result, MAX('id') otherwise
Yii::app()->db->createCommand()->select('MAX(id)')->from('yii_availability')->queryScalar();
I know there are a lot of topics with the same title. But mostly it's the query that's been inserted in the wrong place. But I think I placed it right.
So the problem is, that I still get 0 even when the data is inserted in the db.
Does someone knows an answer where I could be wrong?
here's my code:
mysql_query('SET NAMES utf8');
$this->arr_kolommen = $arr_kolommen;
$this->arr_waardes = $arr_waardes;
$this->tabel = $tabel;
$aantal = count($this->arr_kolommen);
//$sql="INSERT INTO `tbl_photo_lijst_zoekcriteria` ( `PLZ_FOTO` , `PLZ_ZOEKCRITERIA`,`PLZ_CATEGORIE`)VALUES ('$foto', '$zoekje','$afdeling');";
$insert = "INSERT INTO ".$this->tabel." ";
$kolommen = "(";
$waardes = " VALUES(";
for($i=0;$i<$aantal;$i++)
{
$kolommen .=$this->arr_kolommen[$i].",";
$waardes .="'".$this->arr_waardes[$i]."',";
}
$kolommen = substr($kolommen,0,-1).")";
$waardes = substr($waardes,0,-1).")";
$insert .=$kolommen.$waardes;
$result = mysql_query($insert,$this->db) or die ($this->sendErrorToMail(str_replace(" ","",str_replace("\r\n","\n",$insert))."\n\n".str_replace(" ","",str_replace("\r\n","\n",mysql_error()))));
$waarde = mysql_insert_id();
Thanks a lot in advance, because I have been breaking my head for this one for almost already a whole day. (and probably it's something small and stupid)
According to the manual mysql_insert_id returns:
The ID generated for an AUTO_INCREMENT column by the previous query on
success, 0 if the previous query does not generate an AUTO_INCREMENT
value, or FALSE if no MySQL connection was established.
Since it does not give you false and not the correct number it indicates that the queried table didn't generate an auto-increment value.
There are two possibilities I can think of:
Your table doesn't have an auto_increment field
Since you doesn't provide the link to the mysql_insert_id() but using a link with mysql_query() it might not be the correct table that's queried when retrieving the last inserted id.
Solution:
Make sure it has an auto_increment field
Provide the link aswell: $waarde = mysql_insert_id($this->db);
It is possible that your INSERT query was not successful - e.g., maybe you were trying to insert duplicate data on a column whose data must be unique?
If the id is indeed set to auto increment and still get '0' as your response do a column and value count i experienced this only later on I noticed a number of my column count did not match values count.
Codeigniter has an odd behaviourd when calling mysql_insert_id(). The function returns 0 after the first call. So calling it twice will return 0.
Use a variable instead of calling the function more times:
$id = mysql_insert_id();
im using hook form alter in drupal.
If the fields left empty I want it to get the last value submitted for the field and increase it by .01
Ive tried
function uc_pa_form_submit($form, &$form_state) {
global $user;
$maxbid = db_result(db_query('SELECT MAX(amount) FROM {uc_auction_bids} WHERE nid = %d', $node->nid));
$input01 = (($maxbid) ? $maxbid : 0) + .01;
drupal_write_record('table', $input01);
but it isnt updating with anything, I know $input01 works as I tried it in a different function.
if i change drupal_write_record('table', $input01); to the value submitted it works.
drupal_write_record() expects the second argument to be an object, at the moment you're passing a number. Also, if you're trying to update a record you'll need to provide the table's primary keys as the third argument. Something like this:
$sql = 'SELECT * FROM FROM {uc_auction_bids} WHERE nid = %d ORDER BY amount DESC LIMIT 1';
$obj = db_fetch_object(db_query($sql));
$obj->amount = (($obj->amount) ? $obj->amount: 0) + .01;
drupal_write_record('uc_auction_bids', $obj, array('bid'));
I'm new here and wish I could just comment rather than say I'm answering anything...
Anyway, what is $maxbid? It doesn't exist in the function before the comparison. Is it a global? Or should $input be $maxbid (or vice-versa) in your code sample?