Hi i'm working on some code with mysql and need to display the mysql results with php..
MySQL
select distinct(year(Cataloged_Date)) from records;
+------------------------+
| (year(Cataloged_Date)) |
+------------------------+
| 2009 |
+------------------------+
1 row in set (0.00 sec)
PHP
foreach($query->result() as $show){
$data[$i] = $show->Cataloged_Date;
$i++;
}
I'm using codeigniter for the php. Using $show->Cataloged_Date will not return anyting. I'm thinking its $show-> something to display the results...Just cant get it right now...
You need to provide a explicit name or alias for your field in the mysql query - when you apply functions to a column then it's non-obvious what the column name will be.
Try this:
//on MySQL
select distinct(year(Cataloged_Date)) as "Cat_Date" from records;
<?php
foreach($query->result() as $show){
$data[$i] = $show->Cat_Date;
$i++;
}
?>
You can apply an alias to any "value" on your select, be it a column name or the result of a function.
Just do
SELECT something AS YourAlias ...
To give you a clear example:
mysql> select 1;
+---+
| 1 |
+---+
| 1 |
+---+
1 row in set (0.00 sec)
#A simple value can be given an alias
mysql> select 1 as "Number";
+--------+
| Number |
+--------+
| 1 |
+--------+
1 row in set (0.00 sec)
mysql> select max(val) from my_values;
+----------+
| max(val) |
+----------+
| 4 |
+----------+
1 row in set (0.00 sec)
#A function
mysql> select max(val) as "max_val" from my_values;
+---------+
| max_val |
+---------+
| 4 |
+---------+
1 row in set (0.00 sec)
#or even a plain column
mysql> select val as "lav" from my_values;
+------+
| lav |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
+------+
4 rows in set (0.00 sec)
Yes this is normally true but i'm using Codeigniter php framework. I did get it working with
$query->first_row() as $show
Then just echo $show and the results will display without you needing to know the rows name..
Related
This question already has answers here:
MySQL query finding values in a comma separated string
(11 answers)
Closed 5 years ago.
I have field in MySQL table. The field is called 'vehicles' When I add vehicles I add them by ID not name, so the field is populated like '2:3:4:6:7:9' 2 will be a car, 7 will be a bike, etc.
What I want to do quickly and simply is when I query the table I want to see if the field vehicle contains '2' is in that field within the 2:3:4:7:9.
I have tried a lot but coming up blank?
Thanks
if you just want to locate any specific number then do like this
$data = "2:3:4:6:7:9";
echo "found on index : " . array_search("2", explode(":", $data));
You can use FIND_IN_SET in quer query like
SELECT *
FROM yourTable
WHERE FIND_IN_SET('2',REPLACE(vehicle ,':',',')) > 0;
samples
mysql> SELECT FIND_IN_SET('1',REPLACE('2:3:4:7:9',':',',')) as a;
+------+
| a |
+------+
| 0 |
+------+
1 row in set (0,00 sec)
mysql> SELECT FIND_IN_SET('2',REPLACE('2:3:4:7:9',':',',')) as a;
+------+
| a |
+------+
| 1 |
+------+
1 row in set (0,00 sec)
mysql> SELECT FIND_IN_SET('7',REPLACE('2:3:4:7:9',':',',')) as a;
+------+
| a |
+------+
| 4 |
+------+
1 row in set (0,00 sec)
mysql> SELECT FIND_IN_SET('8',REPLACE('2:3:4:7:9',':',',')) as a;
+------+
| a |
+------+
| 0 |
+------+
1 row in set (0,00 sec)
Is there a more efficient way to do this? I want the maximum value of a column (int) in a given MySQL table using PHP.
$results = $conn->query("SELECT statement_id FROM statement ORDER BY statement_id DESC LIMIT 1");
$next_statment_id = $results->fetch_assoc();
$next_statment_id = $next_statment_id['statement_id'];
echo $next_statment_id;
MAX is an OK method too
SELECT MAX(statement_id) AS statementIdentity
FROM statement;
output produce something like this:-
+-------------------+
| statementIdentity |
+-------------------+
| 4 |
+-------------------+
Consider the following:
SELECT COUNT(*) FROM test;
+----------+
| COUNT(*) |
+----------+
| 10000000 |
+----------+
SELECT i FROM test ORDER BY i DESC LIMIT 1;
+----------+
| i |
+----------+
| 18482903 |
+----------+
1 row in set (0.00 sec)
SELECT MAX(i) FROM test;
+----------+
| MAX(i) |
+----------+
| 18482903 |
+----------+
1 row in set (0.00 sec)
See. Not much in it.
Here's my query:
mysql> select * from jobs where datediff(now(),str_to_date(last_modified,'%M %d,%Y'))>=1095;
I get 0 results
mysql> select max(last_modified) from jobs;
+--------------------+
| max(last_modified) |
+--------------------+
| 9/9/2013 |
+--------------------+
1 row in set (0.06 sec)
mysql>
It doesn't seem to be working to well and Im not sure why. I think it has to do with the original formatting of the last_modified column
UPDATE
mysql> select distinct(last_modified) from jobs where datediff(now(),str_to_date(last_modified,'%m/%d/%Y'))>=1095 limit 10;
+---------------+
| last_modified |
+---------------+
| 12/4/2003 |
| 12/5/2003 |
| 12/6/2003 |
| 12/8/2003 |
| 12/9/2003 |
| 12/10/2003 |
| 12/11/2003 |
| 12/12/2003 |
| 12/13/2003 |
| 12/14/2003 |
+---------------+
10 rows in set (0.00 sec)
mysql>
Use str_to_date(last_modified,'%m/%d/%Y') instead of str_to_date(last_modified,'%M %d,%Y')
When i am running a query, for example:
"SELECT * FROM program_detaljer where program_ref_id='a'"
All I get from the result is program_ref_id = 0.
Why does this happen, when the program_ref_id column is supposed to be int(11)?
You should read about types conversion in mysql
Before comparison mysql tries to convert string ('a') to number ('0').
mysql> SELECT 0='a';
+-------+
| 0='a' |
+-------+
| 1 |
+-------+
1 row in set, 1 warning (0.00 sec)
You could check value before quering. Or replace = with LIKE : ). (But better to check value type before.)
mysql> SELECT 0 LIKE 'a';
+------------+
| 0 LIKE 'a' |
+------------+
| 0 |
+------------+
1 row in set (0.00 sec)
I'm doing a work for a client but since I haven't been using PHP/MySQL for a while I forgot some simple things, hope you can help me out.
I have the following SQL table:
ID (non-null, autoincrement) | credit (int)
My query should put the whole "credit" column to 0 except for the row that has the higher ID.
So I would do:
UPDATE $table SET credit = 0 WHERE... ?
Thanks in advance for any help :)
UPDATE $table SET credit = 0 WHERE ID > $ID
Will update any rows that have and ID greater than the variable $ID
If you only want to update the row with the maximum ID then use:
UPDATE $table SET credit = 0 WHERE ID = (select max(id) from $table)
Edit: As Eggyal correctly points out MySQL doesn't like a subquery on the same table as an update - but you can get around it nicely:
UPDATE $table
SET credit = 0
WHERE
credit='$credit'
AND statid='$statid'
AND userid='$userid'
AND ID = (select ID from (SELECT MAX(ID)as ID from $table) a)
And examples from my console:
mysql> select * from first;
+------+-------+
| id | title |
+------+-------+
| 1 | aaaa |
| 2 | bbbb |
| 3 | cccc |
| 4 | NULL |
| 6 | eeee |
+------+-------+
5 rows in set (0.00 sec)
mysql> update first set title='ffff' where id=(select max(id) from first);
ERROR 1093 (HY000): You can't specify target table 'first' for update in FROM clause
mysql> update first set title='ffff' where id=(select ID from (select max(id) as ID from first) a);
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from first;
+------+-------+
| id | title |
+------+-------+
| 1 | aaaa |
| 2 | bbbb |
| 3 | cccc |
| 4 | NULL |
| 6 | ffff |
+------+-------+
5 rows in set (0.00 sec)
Note: As the subquery within a subquery trick unlocks the original table, it is a good idea to run this within a transaction - if the table is unlocked from a query, it might have changed by the time it is updated - so it will be a good idea to use this type of query within a transaction.