The following select returns an empty result set, although it shoudn't:
$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'tx_xmluploader_xml_import_tree', 'xml_import_id='.$xml_import_id);
$xml_import_id is set. And it works if I remove the where clause..
Thanks
I still don't understand why it doesn't work.. A simple workaround suggested by a coleague:
// select all from the db
$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'tx_xmluploader_xml_import_tree');
while( $entry = $GLOBALS['TYPO3_DB']->sql_fetch_assoc() )
{
if( $entry['xml_import_id'] == $xml_import_id ) {
....
}
}
First, make sure the following is set in localconf.php:
$TYPO3_CONF_VARS['SYS']['sqlDebug'] = '1';
$TYPO3_CONF_VARS['FE']['debug'] = '1';
Then try
$res = $GLOBALS['TYPO3_DB']->SELECTquery('*', 'tx_xmluploader_xml_import_tree', 'xml_import_id='.$xml_import_id);
t3lib_div::debug($res);
Result is the output of the query in the frontend. You can then execute it in MySQL for debugging.
a) make sure $xml_import_id actually has a value (one which is in the database as well)
b) Try this:
$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
'*',
'tx_xmluploader_xml_import_tree',
"xml_import_id='".$xml_import_id."'"
);
How do you process the result?
How does your expected $xml_import_id value look like?
cu
Roman
Related
I have this code on my component.
$results = \Db::select('select * from engegraph_forms_membros where id = ?', [1]);
echo $results;
but i have this error
"Array to string conversion"
Someone can help a newbie ??
The problem is you are printing array as as string.
returning result of \Db::select is an array so to print array you can use print_r()
$results = \Db::select('select * from engegraph_forms_membros where id = ?', [1]);
echo '<pre/>'; print_r($result); exit();
you can print result like this or you can use build in debugger function.
$results = \Db::select('select * from engegraph_forms_membros where id = ?', [1]);
dd($result);
// or dump($result);
if you use dump/dd function you dont need to worry it can print anything. dd [die and dump] stops php flow to next statements. dump will continues flow so you can print another values/or/execute next statements if you need.
if any doubts please add comments.
Does anyone know if it's possible to do the below in one query instead of doing a numrows?
$select1 = "SELECT service FROM UPSServices WHERE code = '$serviceCode' AND ship_from_code = '$shipFrom'";
$result = mssql_query($select1);
//print_r($result);
if(mssql_num_rows($result) == 0) {
$select2 = "SELECT service FROM UPSServices WHERE code = '$serviceCode' AND ship_from_code IS NULL";
$result = mssql_query($select2);
}
while ($service = mssql_fetch_array($result)) {
return $service['0'];
}
You are using SQL Server, so you can do this. It is slightly more complicated than you might expect. The following version counts the number of valid values. If this is greater than 0, then the NULLs are filtered out.
select service
from (SELECT service,
count(ship_from_code) over () as NumValues
FROM UPSServices
WHERE code = '$serviceCode' AND (ship_from_code = '$shipFrom' OR ship_from_code IS NULL)
) t
where (NumValues > 0 and service is not NULL) or (NumValues = 0 and service is NULL)
limit 1
I think you need value with NULL shipCode if specified shipCode does not exists
SELECT service FROM UPSServices WHERE
code = '$serviceCode' AND (ship_from_code = '$shipFrom' OR ship_from_code IS NULL)
ORDER BY ship_from_code"
Order by will make sure that if your ship code exists then its at top
I am attempting a quick prototype-port of some old database code over to use the FreeTDS library. Currently, I am looking at a query similar to
SELECT x,y,z from MyTable WHERE id = #arg1
When I execute the query, I naturally get an error like Must declare the scalar variable "#arg1".
But one thing eludes me. How do I declare this variable? I have looked through the API docs and code examples over and over again and I can't seem to find how to solve this should-be-trivial task.
The code I currently use is:
if(dbcmd(proc, "SELECT x,y,z from MyTable WHERE id = #arg1") != SUCCEED) {
return fail("Failed to dbcmd()");
}
if(dbsqlexec(proc) != SUCCEED) {
return fail("Failed to dbsqlexec()");
}
while((retcode=dbresults(proc)) == SUCCEED) {
while(dbnextrow(proc) != NO_MORE_ROWS) {
int len = dbdatlen(proc, 1);
char* data = (char*)dbdata(proc, 1);
cout << string(data, len) << endl;
}
}
You can try either "DECLARE #MyVariable int" or "#arg1", OleDbType.Integer" to add as dbcmd(dbproc, "HERE" ); after your dbcmd(proc,"SELECT .....");
dbcmd(dbproc,"SELECT x,y,z from MyTable WHERE id = #arg1");
dbcmd(dbproc, "DECLARE #arg1 int" );
I do'nt think if there is any API however I am sure this is the way you could use it.
In a data importing script:
client = TinyTds.Client.new(...)
insert_str = "INSERT INTO [...] (...) VALUE (...)"
client.execute(insert_str).do
So far so good.
However, how can I attach a .pdf file into the varbinary field (SQL Server 2000)?
I've recently had the same issue and using activerecord was not really adapted for what I wanted to do...
So, without using activerecord:
client = TinyTds.Client.new(...)
data = "0x" + File.open(file, 'rb').read.unpack('H*').first
insert_str = "INSERT INTO [...] (...) VALUE (... #{data})"
client.execute(insert_str).do
To send proper varbinary data, you need to read the file, convert it to hexadecimal string with unpack('H*').first and prepend '0x' to the result.
Here is PHP-MSSQL code to save binary data:
mssql_query("SET TEXTSIZE 2147483647",$link);
$sql = "UPDATE UploadTable SET UploadTable_Data = ".varbinary_encode($data)." WHERE Person_ID = '".intval($p_id)."'";
mssql_query($sql,$link) or
die('cannot upload_resume() in '.__FILE__.' on line '.__LINE__.'.<br/>'.mssql_get_last_message());
function varbinary_encode($data=null) {
$encoded = null;
if (!is_null($data)) {
$a = unpack("H*hex", $data);
$encoded = "0x";
$encoded .= $a['hex'];
}
return $encoded;
}
Here is PHP-MSSQL code to get binary data:
mssql_query("SET TEXTSIZE 2147483647",$link);
$sql = "SELECT * FROM UploadTable WHERE ID = 123";
$db_result = mssql_query($sql,$link);
// work with result like normal
I ended up using activerecord:
require 'rubygems'
require 'tiny_tds'
require 'activerecord-sqlserver-adapter'
..
my_table.create(:file_name => "abc.pdf", :file_data => File.open("abc.pdf", "rb").read)
For SQLServer 2000 support, go for 2.3.x version activerecord-sqlserver-adapter gem.
The following code seems to be just too much, for getting a single count value.
Is there a better, recommended way to fetch a single COUNT value using plain DBI?
sub get_count {
my $sth = $dbh->prepare("SELECT COUNT(*) FROM table WHERE...");
$sth->execute( #params );
my $($count) = $sth->fetchrow_array;
$sth->finish;
return $count;
}
This is shorter, but I still have two statements.
sub get_count_2 {
my $ar = $dbh->selectall_arrayref("SELECT ...", undef, #params)
return $ar->[0][0];
}
Easy enough to do in one line with no extra variables:
$count = $dbh->selectrow_array('SELECT count(*) FROM table WHERE...', undef, #params);
I don't know Perl, but if it's syntax is logical I would think this would work based on your 2nd example:
sub get_count {
return $dbh->selectall_arrayref("SELECT ...", undef, #params)->[0][0];
}
I probably wouldn't do this myself, but you could always make it a new top-level function of the DBH object you're using:
WARNING: untested code follows!
sub DBD::SQLite::db::count
{
my($dbh, $table, $where) = #_;
my($stmt) = "SELECT COUNT(*) FROM $table";
$stmt .= " WHERE $where" if $where;
my($count) = $dbh->selectrow_array($stmt);
return $count;
}
and then call it like this:
my($cnt) = $dbh->count('Employee', 'year_hired < 2000');
Besides polluting a namespace that's not yours, you'd also have to write this for every DB driver you use, though I'm sure your could work something up that allows you to construct and eval some code to auto-configure this for a given DBH object.