I found a couple of results similar to what I'm about to ask but unfortunately none of them provided a solution or direction to the problem I'm facing.
I'm reviewing a large SQL Server 2008 database and I'm running some blanket queries such as running a SELECT TOP(5) on every table to get an idea of the contents.
I've encountered some Binary(16) objects in the results in PHP and of course PHP isn't able to print a readable version of the id easily. I don't have the option of adding the MSSQL extension to make use of the mssql_guid_string function and I can't perform the convert in SQL as the select needs to be as generic as possible to work for every table. I'm doing a foreach on a list of tables to return the data and print it.
So my question is: Does anyone know of an alternative to mssql_guid_string in PHP??
The pseudo of what I'm trying to do would be;
1. SELECT TOP(5) * FROM table1
2. WHILE $row = sqlsrv_fetch_array{...
3. foreach $row as $col print $col
The id's in SQL look something like this -> 0xB0826E8A84CA6C418254E28BC0F749CF
When printed in PHP they look like this -> X÷Eòv˜H½XšÔÛé«Ù
Any help/thoughts would be greatly appreciated.
Steve
From the PHP side you can dump to hexadecimal and add a prefix:
echo '0x' . strtoupper(bin2hex($col));
From SQL Server you can probably cast to string (no idea about that).
Related
I thought I had seen this addressed but I cannot find it after 2 hours of googling.
I have a REAL data type in my database, I stored the value 0.00054
When I select it in SQL studio, I get 0.00054 no problem.
When I select it in php using pdo, I get 0.00053999998
Here is from SQL studio also:
select val_real, convert(decimal(38,18),val_real) from myTable
result: 0.00054, 0.000539999979082490
From PHP using PDO result:
5.3999998E-4, .000539999979082490
It looks like php is converting the type before I can get to it. I am pulling my hair out. How can I fix this?
Thank you
PHP isn't converting anything, as also proven by your SQL query:
select val_real, convert(decimal(38,18),val_real) from myTable
result: 0.00054, 0.000539999979082490
The problem is that the value 0.00054 cannot be accurately represented using a real, so you get the closest value to it, which is 0.000539999979082490.
Read this: http://floating-point-gui.de/ for more information.
If you need to store data verbatim from what a user enters, use a string type or a decimal if you want numbers.
I wonder if there is a way to output the EXPLAIN data from an MYSQL statement to PHP.
For studying I need to write a small application in PHP that outputs data via a SELECT query from a MYSQL DB. I have to output the EXPLAIN data, like searched rows etc., from this query as well. If I just set EXPLAIN in front of the SELECT, I get an Error.
Unfortunately I couldn't find a satisfying answer on the internet.
Due to I'm new to PHP, I would appreciate if someone can give me an example or smth like this.
Thank you guys!
You should be able to grab it using standard array tools. By using $result->fetch_array(MYSQLI_NUM); we can just grab the first return without a column name
$sql = 'EXPLAIN SELECT * FROM table WHERE condition = "condition"';
$result = $mysqli->query($sql);
$row = $result->fetch_array(MYSQLI_NUM);
echo $row[0];
I have a php script running on linux which queries a MSSQL Server Express database running on my dev machine (windows) using the php mssql driver.
I'm able to connect and select a database, and get no errors.
I put the query directly into a query window in MS SQL Server Management Studio and I get the correct data back. However, if I run the same query from my PHP script, the data that comes back has a bunch of "replacement characters", described here: http://www.fileformat.info/info/unicode/char/0fffd/index.htm
It looks like this: �
For instance, when querying for a list of IDs, eg "SELECT DISTINCT PageId FROM Pages", the query window is showing a list of IDs in the following format:
C961277D-D8BE-4337-82CF-003F6E7951E2
However, when I run the same query in PHP, this is the result:
'���K�#�t��#/
I AM getting the same number of results, so the SQL Server seams to be interpreting the query correctly, but in the results, the character length is wrong, none of the characters match, and the format is wrong. Anybody have any idea what is going on?
Thank you for your help.
Relevant code here:
$this->connection = mssql_connect(
$this->configuration['servername'],
$this->configuration['username'],
$this->configuration['password']);
$query = 'SELECT DISTINCT PageId FROM sf_CmsCtrlLinks';
$result = mssql_query($query);
while ($row = mssql_fetch_object($result)){
// Results in "page ID: 7��"�O�5,���"
echo "page ID: " . $row->PageId;
}
SQL Server returns GUIDs to PHP in binary format.
If you want it as a readable format, you can just make MSSQL pass it back to you as a string;
SELECT DISTINCT CONVERT(VARCHAR(38), PageId) as PageId FROM pages
If you don't need them readable, not casting them and just using them as an "opaque" id should work without problems.
I can't test it myself, but mssql_guid_string() may also work to convert them to readable form.
I'm using a PHP webservice where I have performed a simple SELECT query, and stored it
$result = run_query($get_query);
I now need to perform further querying on the data based on different parameters, which I know is possible via MySQL in the form:
SELECT *
FROM (SELECT *
FROM customers
WHERE CompanyName > 'g')
WHERE ContactName < 'g'
I do know that this performs two Select queries on the table. However, what I would like to know is if I can simply use my previously saved query in the FROM section of the second section, such as this, and if my belief that it helps performance by not querying the entire database again is true:
SELECT *
FROM ($result)
WHERE ContactName < 'g'
You can make a temp table to put the initial results and then use it to select the data and in the second query. This will work faster only if your 1-st query is slow.
PHP and SQL are different languages and very different platforms. They often don't even run in the same computer. Your PHP variables won't interact at all with the MySQL server. You use PHP to create a string that happens to contain SQL code but that's all. In the end, the only thing that counts is the SQL code you sent to the server—how you manage to generate it is irrelevant.
Additionally, you can't really say how MySQL will run a query unless you obtain an explain plan:
EXPLAIN EXTENDED
SELECT *
FROM (SELECT *
FROM customers
WHERE CompanyName > 'g')
WHERE ContactName < 'g'
... but I doubt it'll read the table twice for your query. Memory is much faster than disk.
Thanks for the responses, everyone. Turns out what I was looking for was a "query of query", which isn't supported directly by PHP but I found a function over here which provides the functionality: http://www.tom-muck.com/blog/index.cfm?newsid=37
That was found from this other SO question: Can php query the results from a previous query?
I still need to do comparisons to determine whether it improves speed.
If I understand your question correctly you want to know whether saving the "from" part of your SQL query in a php variable improves the performance of you querying your SQL server, then the answer is NO. Simply because the variable keeping the value is inserted into the query.
Whether performance is gained in PHP, the answer is most probable yes; but depends on the length of the variable value (and how often you repeat using the variable instead of building a new complete query) whether the performance will be notable.
Why not just get this data in a single query like this?
SELECT *
FROM customers
WHERE CompanyName > 'g'
AND ContactName < 'g'
G'day,
I'm not familiar with MySQL and this will probably be an easy question!
I am trying to mod a Joomla plugin and am working with this code that works well for a similar function:
$q="SELECT `".$naming."` AS naming FROM `#__users` WHERE `id`='".$jomsocial_event->creator."' ";
$db->setQuery($q);
$eventcreatorname = $db->loadResult();
$eventcreator = ''.addslashes($eventcreatorname).'';
What I need to do is lookup the field id in the table community_groups and return the matching field name. What I have is (note that $jomsocial_event->contentid contains the group ID):
$q="SELECT `".$naming."` AS naming FROM `#__community_groups` WHERE `id`='".$jomsocial_event->contentid."' ";
$db->setQuery($q);
$eventgroupname = $db->loadResult();
$eventgroup = ''.addslashes($eventcreatorname).'';
It returns nothing as the query is wrong; what should it be for my usage?
I'd work backwards from the database.
i.e. turn on SQL logging and look at what's actually arriving in the database. Tweak as necessary by playing with the resulting SQL until you get what you want (and expect) and then implement that in your code.
Take a look at your generated query in the debugging from Joomla.
Run it against mysql directly and see where it goes wrong.
Also, I'd use the JDatabaseQuery API because you are much less likely to get errors with quoting etc. It looks to me like you are treating id as a string not an integer.