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();
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 am trying to check if there were any records (more than 0) in my database table:
$ifExists = DB::select('SELECT COUNT(*) FROM `characters` WHERE userid = ?', array(Auth::id()));
Now I am checking if there are more than 0:
if($ifExists > 0){
return Redirect('/board');
}
else{
return view('intro');
}
It always takes me to the /board. But there are many rows in this database table.
The echo $ifExists; shows me [{"COUNT(*)":0}]
How do I check this number?
Try it this way:
$ifExists = DB::table('characters')->where('userid', Auth::id())->count();
If you do it like in your question, you will receive an array of objects.
If you only want to check for existens of entries and do not need the count, you can just use the exists() function whitch will return you a boolean:
$ifExists = DB::table('characters')->where('userid', Auth::id())->exists();
if($ifExists){
return Redirect('/board');
}else{
return view('intro');
}
Been a while since I've done PHP, but from the looks of it you get a result object from the database. That object is 1 object.
I think that's why it's always redirecting to /board.
Not sure how to do this in PHP, but you want to get the value of "COUNT(*)" from the object for your if statement. Hope that helps :)
I am using the following code to return the last insert id from the table employee kindly let me know what is wrong with the following code:
<?php
$temp_array = mysql_query("select last_insert_id() from employee");
//now you can display it, to test it
echo $temp_array;
?>
it return 0 value. Pls tell me the easy way to get this without using max(),
mysql_insert_id can be used to return the last ID generated for an auto increment column by the previous query (if the column is auto increment)
$temp_array is of type Resource, rather than integer. You need to do
$rslt = mysql_fetch_array($temp_array);
$id = $rslt[0];
See http://php.net/manual/en/function.mysql-fetch-array.php for details
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;