Pages: [1] :: one page |
|
Author |
Thread Statistics | Show CCP posts - 0 post(s) |
Mr Twinkie
Black Serpent Technologies The-Culture
15
|
Posted - 2015.09.22 07:11:21 -
[1] - Quote
I'm trying to check an api for a character id using PHP.
This is the code I'm using
http://pastebin.com/A4DgqtsM (even in code tags this **** forum yells at me for posting code)
Obviously $id is defined earlier, but for some reason this isn't working?
MAMBA Head of IT
|
Bloemkoolsaus
Viperfleet Inc. Official Winners Of Takeshi's Castle
199
|
Posted - 2015.09.22 09:07:11 -
[2] - Quote
if ($charidcheck = NULL) You are assigning null to $charidcheck, the if will return true because it'll be succesfully assigned.
Instead, use this to check if the var is null:
if ($charidcheck == NULL)
Note that php can get a bit weird with null because of the way it handles data types. Empty strings for exemple will also qualify as null. If you want to check if it's actually NULL instead of just empty, use:
if ($charidcheck === NULL)
|
Mr Twinkie
Black Serpent Technologies The-Culture
15
|
Posted - 2015.09.22 09:44:37 -
[3] - Quote
Bloemkoolsaus wrote:if ($charidcheck = NULL) You are assigning null to $charidcheck, the if will return true because it'll be succesfully assigned. Instead, use this to check if the var is null: if ($charidcheck == NULL) Note that php can get a bit weird with null because of the way it handles data types. Empty strings for exemple will also qualify as null. If you want to check if it's actually NULL instead of just empty, use: if ($charidcheck === NULL)
Bah... literally wasted 2 hours on this and going from = to == fixed it haha. Thanks!
MAMBA Head of IT
|
Yogi Berimor
RGSU Goonswarm Federation
1
|
Posted - 2015.09.22 23:22:34 -
[4] - Quote
The best way to avoid such mistakes in the future is to use reverse assertion syntax like this:
if(NULL == $var) { //some code }
|
Mr Twinkie
Black Serpent Technologies The-Culture
15
|
Posted - 2015.09.22 23:52:36 -
[5] - Quote
Yogi Berimor wrote:The best way to avoid such mistakes in the future is to use reverse assertion syntax like this: if(NULL == $var) { //some code }
This will raise an error should you put an assigment (" = ") instead of assertion (" == " / " === "). It might take some time to get used to it but it is worth the effort. It is also considered as best practice
Ya still getting into the good habits, self taught myself php while deployed over the winter so my code isn't the cleanest.
MAMBA Head of IT
|
Mr Twinkie
Black Serpent Technologies The-Culture
15
|
Posted - 2015.09.22 23:57:44 -
[6] - Quote
Another question. I'm trying to setup a cron to check if the character is in corp using the charid and the characterinfo.xml.aspx
The charid is stored in a mysql db and my issue is I'm not sure how to get the cron to run down the rows and check every single row.
The current code is - http://pastebin.com/JiQmdaDq
will it automatically loop thru? or am I missing something to make it do that
MAMBA Head of IT
|
Yogi Berimor
RGSU Goonswarm Federation
1
|
Posted - 2015.09.23 02:35:10 -
[7] - Quote
Add a new column to your characters table named 'UpdatedAt'(or something like that) of type "DateTime". Change
$query = 'SELECT charid FROM services.members;';
To
$query = 'SELECT charid FROM services.members ORDER BY UpdatedAt DESC;';
(You can even consider adding a LIMIT here)
Then wrap your other stuff in a loop
// Corp Check
while( $row = mysqli_fetch_row($result) ) { $url = 'https://api.eveonline.com/eve/CharacterInfo.xml.aspx'; $url .= '?characterID=' . urlencode($result); $xml = makeApiRequest($url); $corpid = (int) $xml->result->corporationID; if ($corpid == '875512489') { $query2 = 'UPDATE services.members SET role = "MAMBA" WHERE charid = $result LIMIT 1'; } else { $query2 = 'UPDATE services.members SET role = "OOC" WHERE charid = $row[0] LIMIT 1'; } $result2 = mysqli_query($con,$query2); if (!$result2) { die('Invalid query: ' . mysql_error()); }
}
Havent tested this code, might be some syntax issues, but you get the idea.
I really recommend you to read about PDO and use that instead of mysqli bs. Also, have a look at https://github.com/3rdpartyeve/phealng - this lib handles all the boring stuff and is pretty easy to use.
P.S. This reminds me i need to speed up the release of my Starlight project. |
Mr Twinkie
Black Serpent Technologies The-Culture
15
|
Posted - 2015.09.23 03:15:21 -
[8] - Quote
Throws
"Invalid query: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '[0] LIMIT 1' at line 1"
if I put $row[0] in quotes it no longer throws the error but it also doesn't do anything in the db.
MAMBA Head of IT
|
Jack Hayson
Atztech Inc. Ixtab.
292
|
Posted - 2015.09.23 10:08:21 -
[9] - Quote
Mr Twinkie wrote:Throws
"Invalid query: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '[0] LIMIT 1' at line 1"
if I put $row[0] in quotes it no longer throws the error but it also doesn't do anything in the db.
If you want php to interpret variables inside strings you need to use double quotes " not single quotes '.
so either
$query2 = "UPDATE services.members SET role = \'OOC\' WHERE charid = $row[0] LIMIT 1"; or
$query2 = 'UPDATE services.members SET role = \'OOC\' WHERE charid = '.$row[0].' LIMIT 1'; |
Mr Twinkie
Black Serpent Technologies The-Culture
15
|
Posted - 2015.09.24 04:00:13 -
[10] - Quote
Jack Hayson wrote:Mr Twinkie wrote:Throws
"Invalid query: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '[0] LIMIT 1' at line 1"
if I put $row[0] in quotes it no longer throws the error but it also doesn't do anything in the db. If you want php to interpret variables inside strings you need to use double quotes " not single quotes '. so either $query2 = "UPDATE services.members SET role = \'OOC\' WHERE charid = $row[0] LIMIT 1"; or $query2 = 'UPDATE services.members SET role = \'OOC\' WHERE charid = '.$row[0].' LIMIT 1';
Ya figured it out after like 3 hours :P
MAMBA Head of IT
|
|
BB-8
24th Imperial Crusade Amarr Empire
0
|
Posted - 2015.09.27 15:37:20 -
[11] - Quote
would be cool if there was an php dev channel to discuss issues like this so that when the guy wastes 2 hours over a syntax error he doesnt have to wait even longer for a forum post.
#eve-dev on coldfront is dead. |
Vast Space
Perkone Caldari State
3
|
Posted - 2015.09.27 18:19:22 -
[12] - Quote
BB-8 wrote:would be cool if there was an php dev channel to discuss issues like this so that when the guy wastes 2 hours over a syntax error he doesnt have to wait even longer for a forum post.
#eve-dev on coldfront is dead.
www.stackoverflow.com www.w3schools.com/php/ |
Dragonaire
Here there be Dragons
76
|
Posted - 2015.10.01 20:30:41 -
[13] - Quote
Something I would suggest to anyone especially learning a new programming language is to find a good editor with syntax highlighting it'll make a big difference in finding errors like these. Even better is a good IDE which can include plug-ins for code inspections which can catch even more things and help enforce good coding practices. For a simple code editer I like Notepad++(Windows) but an IDE is much better and a couple I've used are Komodo-edit (free, cross-platform) and the one I current use which is PhpStorm (free trail, cross-platform) from JetBrain. It's code style settings and inspections are very good. It also understands more than just PHP and will help with the SQL syntax etc as well. For some coding standards for PHP you should look at the PSR ones. PHPStorm even let's you set it up to correct the formatting for you on commits etc.
Also as someone pointed out except if you are just doing for the learning it's better not to be re-inventing the wheel when it comes to the Eve API. The are existing libraries like Pheal-ng or my own Yapeal which deal with all the misc cruft and inconsistent there are in the APIs so you don't have to. Even if your just trying to learn you would probably find looking at other people's code useful especially where we're solving the same problems you are trying to work on.
Finds camping stations from the inside much easier.
Designer of Yapeal for the Eve API.
Check out the Yapeal PHP API Library thread.
|
|
|
|
Pages: [1] :: one page |
First page | Previous page | Next page | Last page |