I have the following query in my php script:
SELECT last_value FROM sys.identity_columns WHERE OBJECT_NAME(OBJECT_ID) = 'MYTABLE' AND last_value IS NOT NULL
This works great, it returns the last id from MYTABLE which is what I need.
But, as I'm going to have more than one database I should modify the query to select the database where MYTABLE is.
I'm doing:
SELECT last_value FROM sys.identity_columns WHERE OBJECT_NAME(OBJECT_ID) = 'mydatabase.dbo.MYTABLE' AND last_value IS NOT NULL
But this does not work, I get nothing in return.
Any ideas?
Thanks a lot!
When you execute an SQL statement, it runs in the context of a database you specified on connection. In order to query a different database you should change the database context.
Try this:
USE MyDatabase;
SELECT last_value FROM sys.identity_columns WHERE OBJECT_NAME(OBJECT_ID) = 'MYTABLE' AND last_value IS NOT NULL
If you need to pass the database name as a variable IDENT_CURRENT accepts a database qualified object name. e.g.:
SELECT IDENT_CURRENT('mydatabase.dbo.MYTABLE');
As an unrelated aside it would be better to use:
WHERE OBJECT_ID = OBJECT_ID(N'MYTABLE');
Instead of
WHERE OBJECT_NAME(OBJECT_ID) = 'MYTABLE'
Since if MYTABLE was not in the default schema, it would fail, consider a table MYSCHEMA.MYTABLE. This returns no rows
WHERE OBJECT_NAME(OBJECT_ID) = 'MYSCHEMA.MYTABLE'
Whereas this would:
WHERE OBJECT_ID = OBJECT_ID(N'MYSCHEMA.MYTABLE');
Related
I have this mysql need to run in php:
$sql_subject_summary = "SELECT
c.subject_code_id, c.subject_name, #total_target:= SUM(s.total_target_question) AS total_target_question,
#total_correct := ROUND((RAND() * (#total_target-10))+10) AS total_correct,
(#total_correct / #total_target)*100 AS percent, c.icon_filename
FROM
edu_subject_code c LEFT JOIN wkp_wg_student_subject s ON c.subject_code_id=s.subject_code_id
WHERE s.student_id=$student_id AND s.week_id = $current_week_id
$sql_inject
GROUP BY s.subject_code_id ";
However, the values of #total_correct, #total_target return null on php mysql execution .
When I run in mysql IDE, then the result is ok.
How to solve this problem?
That's right cause for that to work you need to use SELECT INTO... construct like select col1 into #arg1 from tbl1. Moreover since you are running the query from PHP why you need that at all? if you really need that then consider wrapping the query in a stored procedure and have those parameter as OUT parameter.
Well it's return null cause you are not selecting those parameter. After your query executes, you need to select those parameter saying select #total_correct, #total_target;.
Why don't you just run the query as is (like below) and fetch the specific columns value
SELECT
c.subject_code_id, c.subject_name, SUM(s.total_target_question) AS total_target_question,
ROUND((RAND() * (#total_target-10))+10) AS total_correct,
(#total_correct / #total_target)*100 AS percent, c.icon_filename
FROM
edu_subject_code c LEFT JOIN wkp_wg_student_subject s ON c.subject_code_id=s.subject_code_id
WHERE s.student_id=$student_id AND s.week_id = $current_week_id
$sql_inject
GROUP BY s.subject_code_id
I am using a Postgres 9.4 database and have PHP as my front end.
A general query I may run would look like this:
PHP :
$query = "select * from some_table";
pg_prepare($connection,"some_query",$query);
$result = pg_execute($connection,"some_query",array());
while ($row = pg_fetch_array($result,null,PGSQL_ASSOC)) {
echo $row['some_field'];
echo $row['some_field_1'];
echo $row['some_field_2'];
}
I am running into a front-end that requires to know the datatype of the column that spits out - specifically I need to know when the echo'd database field is a timestamp column.
Obviously I can tell integers and string, however timestamp is a bit of a different thing.
I suppose I could see if strtotime() returns false, however that seems a little dirty to me.
So my question is:
Is there a PHP built-in function that can return a multi-dimensional array of the database row with not only $key=>$value pair but also the datatype?
Any help on this would be appreciated - thank you!
You can query from information_schema.columns and fetch just like any other query:
SELECT column_name, data_type
FROM information_schema.columns
WHERE table_name='some_table'
Or after your query use pg_field_type():
$type = pg_field_type($result, 0);
But you need to know the position of the column in the result so you should (best practice anyway) list the columns. For the above case using 0 would give the type of col1 in the query below::
SELECT col1, col2, col3 FROM some_table
I have a table image_tb, which has 3 fields, id,images,link (id is auto auto_increment), this is my insert code:
mysql_query("
insert into image_tb
(images,link)
select
'".(max(id)+1).".jpeg','".$link."'
from image_tb
");
it return:
Warning: max(): When only one parameter is given, it must be an array
how to modify? thanks.
its better you define a function , get the last id by query like this:
"select id from image_tb order by id desc limit 0,1"
then increase it , its realibe .
I think you don't mean to close the string:
select '" ...
should be
select MAX(id) + 1
Otherwise you are using the php function max, and I don't think you intend to do that at all.
By the way, you shouldn't be using mysql_*. Use PDO or mysqli.
do it inside the query,
mysql_query("
insert into image_tb (images,link)
select CONCAT(COALESCE((SELECT MAX(ID) + 1 FROM image_tb),1), '.jpeg'),'".$link."'
from image_tb
");
your query is vulnerable with SQL Injection, please read the article below to protect from it
How can I prevent SQL injection in PHP?
Have you considered something like this?
Insert data with empty filename
Update table where filename is empty, set filename to CONCAT(id, ".jpeg") or something (CONCAT is function used to "add" strings).
My database contains empty table columns.
I would like to add a character like § to these empty rows so that I can search for them easier. How would I go about?
I already have a script that lets me replace or remove characters but I dont know a way to specify that rows that are empty should be updated with a character.
First, you probably don't have empty rows but empty column values in the rows. Wouldn't it be better if you just do it like if (!empty($row['column'])) instead of trying to put some bogus character?
Or if you want to do a SELECT just do something like this:
SELECT * FROM table_name WHERE column_name > ''; // seems to work for both NULL and empty string
Or:
DELETE FROM table_name WHERE column_name IS NULL or column_name = '';
UPDATE `table` SET column = "§" WHERE column = "";
It's bad to add character to an empty column because you are only adding extra size to the database. It's easy to search empty string on the database. Possible solutions of searching will be using of IS NULL to search for null columns.
SELECT * FROM tableName WHERE collName IS NULL
Another is by using CHAR_LENGTH (which gets the length of the data in the column)
SELECT * FROM tableName WHERE CHAR_LENGTH(collName) = 0
or by simply comparing it to ''
SELECT * FROM tableName WHERE colName = ''
I have a tables like this
Results
-------
id - autoincrement value
TestCase - varchar
Verdict - varchar
AppID - varchar
TestCases
---------
id - autoincrementr value
TestCase - varchar
TestCase_container - varchar
Basically I am displaying the results in php code. while displaying the testcase, I am storing the testcase in a variable. in the while loop of mysql_query, I am creating another connection to DB and passing this variable to TestCases table to get the TestCase_Container assiciated with it.
This is a long way of doing this but I am unable to figure out proper direct SQL query using join or any other thing. Can someone point me in right direction please?
Thanks,
LIke this?
select r.id,r.TestCase,r.Verdict,r.AppId,tc.TestCase_container
from Results r,TestCases tc
where Results.TestCase=TestCases.TestCase
For DB normalization, results table must have testcase_id field instead of TestCase
Kind of hard to say, but I think what you're trying to do is a query like this maybe?
SELECT b.id AS result_id, a.TestCase, b.Verdict, b.AppID, a.id AS testcase_id, a.TestCase_container
FROM TestCases a
LEFT JOIN Results b
ON b.TestCase = a.TestCase;
Would probably be better to be joining on an indexed integer/id field, but that would work fine.
You could easily select all data from your tables by this SQL query:
SELECT Results.TestCase AS TestCase, Results.Verdict AS Verdict, Results.AppID AS AppID, TestCases.TestCase_container AS Container FROM Results JOIN TestCases ON Results.TestCase = TestCases.TestCase
After you should iterate getted array of values in any loop (for example while) like that:
$query = "SELECT Results.TestCase, Results.Verdict, Results.AppID, TestCases.TestCase_container FROM Results JOIN TestCases ON Results.TestCase = TestCases.TestCase";
$res = mysql_query($query) or die(mysql_error());
while ($row=mysql_fetch_array($res)) {
echo $row['TestCase'], ":", $row['Verdict'], ":", $row['AppID'], ":", $row['Container'], "\n";
}