So, I created a query in PhpMyAdmin to pick one random online member that is part of a certain group. It works fine in PhpMyAdmin and does exactly what I want. However, when I run this query using PHP it does not return anything. I simply get 'NULL' when I use var_dump($result).
$sql= "SELECT
ow_base_user_online.userId,
ow_base_authorization_user_role.roleId
FROM
ow_base_user_online
INNER JOIN ow_base_authorization_user_role ON ow_base_authorization_user_role.userId = ow_base_user_online.userId
WHERE
ow_base_authorization_user_role.roleId = 14
ORDER BY
RAND()
LIMIT 1";
$result = OW::getDbo()->queryForList($sql);
Please, does anyone have any ideas?
Please does anyone have any ideas?
Yes, use error reporting from your mysql query and see what it returns. Check PHP errors too (can get auth warnings to show something daft like bad user/password etc).
Also, for testing, try removing the class you use and stick the DB connection code above that query and query all direct from a test script. If it works fine, then your query is ok, as is the connection and credentials.
From there you can work backwards checking the class you have.
Related
)
I've implemented the oci Extension, and i can connect to the database.
but i can't do anything with the database :-(
on every query i get table or view not found.
i've tried to write a quick test to proof that it works in php:
<?php
$conn = oci_connection("user", "pass", "localhost/xe");
$stdi = oci_parse($conn, "SELECT COUNT(*) FROM tests WHERE STATUS =110");
echo oci_execute($stdi);
and get the ORA-00942 Error.
I wrote the same statement inside the sqlDevelopper:
SELECT COUNT(*) from tests where status = 110;
And get a result.
it seems like i have the wrong "database" but as i know oracle doesn't work with seperate databases like MySQL.
I run Oracle Express 11 and have already granted all to the user.
Thanks
Problem is solved.
I've granted the wrong user the rights.
Now with the right user everything is fine :-)
I am trying to move my web site to Azure, and so to rely on their SQL servers, while before I was using Apache/MySql.
I have some difficulties trying to convert the queries, as they do not work anymore.
The following works if i connect to the MySql db, but it fails if I change the connection to the Azure SQL.
string(178) "SELECT * FROM schemedb.users WHERE CreatedBy=20 ORDER BY dateCreation Desc" string(177) "SELECT IdUsr FROM schemedb.userssettings WHERE User=20 ORDER BY Id Desc"
All connections to the Azure SQL server are correct, as a simpler query before these gets executed fine. But then I got stuck here.
The php error console says
Fatal error: Call to a member function setFetchMode() on a non-object in ...
To understand where the problem was I used var_dump on the queries, to see where they were failing, turns out that if I change them to:
string(178) "SELECT * FROM schemedb.users ORDER BY dateCreation Desc" string(177) "SELECT IdUsr From schemedb.userssettings Order By Id Desc"
These queries are then valid, so I guess that the problem is the where clause then, but I have no clue of what's the problem.
-- EDIT
I've made some test, turns out that if I manually write the query, it works as expected.
What I mean is that the above mentioned query is build through several php custom methods like so:
." WHERE ".$this->buildWhere()." ORDER BY ". etc.
But if I type there WHERE CreatedBy = 20, then it works.
My best guess is that it is a format problem, since in SQL Azure the data type in the query must be of the same type of the one declared in the column.
However, I cant' understand what's wrong, since my custom method is writing the value vithout ', therefore as a number, not a string...
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.
I tried doing this,
//returns the last updated id
$val = $DB->GetOne("SELECT id FROM table_name ORDER BY id DESC LIMIT 0,1");
But it doesn't work unless I do $DB->GetRow(). So, does this mean that I need to have where clause for the GetOne() to work?
I would imagine that since you are querying for one result, the WHERE clause is essential. If you attempt to get one result without defining what you are looking for, then I don't imagine it will work in the way you intend. Refer to the PHP Code Examples from the ADOdb documentation.
The code in the question will work but there was a bug in the adodb mysql driver where it will return false (instead of NULL) when no results are found. The bug was fixed in 2010 . So you may be experiencing the bug if you're running an old version of adodb.
GetOne returns the content of the first field in the first row of the result set.
Also saying "it's not working" doesn't say much.
Try printing $DB->ErrorMsg() to see what's wrong.
I always see MySQL queries structured like this, even with the simplest of queries:
$query = "SELECT * FROM table_name WHERE id = '1'";
$result = mysql_query($query);
Is there any value, time or otherwise in doing the above over a simple one line query such as?
$result = mysql_query("SELECT * FROM table_name WHERE id = '1'";
I've always used the former, as when I started using PHP I thought it best to simply copy everyone else, however now that I'm working on a fairly large (relative to my other work) application, I could cut out a significant amount of arbitrary coding if there's really no difference.
Generally, I like separating them because I can easily do print $query; when I'm trying to figure out why my query won't work. It's mostly a matter of personal preference.
With the first, it's easier to log the SQL in case something went wrong (and it's easier to spot typos, like a missing bracket).
Actually there is no difference at all except for one thing. If you put your query in seperate variable then you can reuse that. If any changes occur in query you just need to edit the query in variable
You can always create a custom function that takes in a query and runs it, and pass additional optional arguments in case you want it to return the query, or log the query to track any issues etc.
Is there any value, time or otherwise in doing the above over a simple one line query such as?
sure, as MSpreij said, you can examine your query in case of error.
I always see MySQL queries structured like this, even with the simplest of queries:
$query = "SELECT * FROM table_name WHERE id = '1'";
$result = mysql_query($query) or trigger_error(mysql_error()." ".$query);
this simple construct will save you a dozen whines in stackoverflow "what does it mean mysql_fetch_array(): supplied argument is not a valid MySQL result"
notice the additional part. This is called programming.
It will print your query automatically, in case of error, along with mysql error message. Or log it into error log in a live server with appropriate settings.
Without writing print $query; manually.
However, both methods are ugly as hell.
One should never use raw API functions at all.
It's ugly, unmaintainable, bloated and repetitive.
One should develop a library, a function to make a call more intelligent and less repetitive
$data = mydb_get_row("SELECT * FROM table_name WHERE id = 1");
having all the processing inside. with error handling, query logging, parameter checking and api functions calling.
yet as a true one-liner, not a silly one you were trying to achieve.