Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
gibbed
Apr 10, 2006

duck monster posted:

Wrong
Submit a bug report to PHP then and hope they agree with you.

Adbot
ADBOT LOVES YOU

supster
Sep 26, 2003

I'M TOO FUCKING STUPID
TO READ A SIMPLE GRAPH

duck monster posted:

Wrong, and remember that Python does a fair bit of type cohercion via magic methods where possible.

code:
>>> x = None
>>> print x
None
>>> print x + 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
>>>
This is correct behavior

Also a bit of research is telling me javascript doesn't behave the way many here seem to think it behaves.

http://saladwithsteve.com/2008/02/javascript-undefined-vs-null.html
Python is strongly typed. PHP is weakly typed. You're comparing apples to oranges.

Python doesn't evaluate 1 + "1" to 2 (it throws a very similar error). PHP does. Does this make one right and the other wrong? No. These are characteristics that define the languages.


edit: and for the record, not even your own example is giving the behavior you're looking for. Python is throwing a run-time error. It is not evaulating 1 + null to null.

supster fucked around with this message at 05:56 on Feb 2, 2009

duck monster
Dec 15, 2004

supster posted:

Python is strongly typed. PHP is weakly typed. You're comparing apples to oranges.

Python doesn't evaluate 1 + "1" to 2 (it throws a very similar error). PHP does. Does this make one right and the other wrong? No. These are characteristics that define the languages.

Mind you I'll add that 1 + "1" = 2 is loving terrible, especially if your expecting the correct answer of 50. (The ordinal position of ASCII "1" is 49). We are reverting back from Pascal to Cobol here folks, thats how regressive it is.

Weak typing is the worst thing ever, especially when it lets so many plainly wrong things pass without complaint. But for comparison, check out VB's evaluation of Nulls. VB is a loving awful language thats also weak typed, but it knows about Null and Not a number, at least if my memory serves me right.

waffle iron
Jan 16, 2004
And when it comes down to it, do the null checks yourself or write a helper function that does all that for you. It's not especially hard to get the behavior you're looking for.

The implicit conversions are defined in the documentation for PHP, so it's not like you have to figure this out on your own.

Edit: Next you'll be complaining about the lack of unsigned ints in Javascript and PHP.

waffle iron fucked around with this message at 06:04 on Feb 2, 2009

supster
Sep 26, 2003

I'M TOO FUCKING STUPID
TO READ A SIMPLE GRAPH

duck monster posted:

Mind you I'll add that 1 + "1" = 2 is loving terrible, especially if your expecting the correct answer of 50. (The ordinal position of ASCII "1" is 49). We are reverting back from Pascal to Cobol here folks, thats how regressive it is.

Weak typing is the worst thing ever, especially when it lets so many plainly wrong things pass without complaint. But for comparison, check out VB's evaluation of Nulls. VB is a loving awful language thats also weak typed, but it knows about Null and Not a number, at least if my memory serves me right.
Are you loving retarded? If you're here to complain about PHP being weakly typed, this isn't the thread to do it in. There is nothing wrong with weakly typed languages if you use it properly and understand how typing works.

duck monster
Dec 15, 2004

supster posted:

Are you loving retarded? If you're here to complain about PHP being weakly typed, this isn't the thread to do it in. There is nothing wrong with weakly typed languages if you use it properly and understand how typing works.

Yes, it would be a terrible idea for a php coder to discuss a fault in PHP in the PHP thread.

Anyway, it gets more fun..

At the same time is evaluating null as zero in math,
code:
$v = Null;
var_dump(isset($v));
will evaluate as false, because casting to null causes the interpreter to unset the variable. So with this in mind let me clarify my complaint, it is not the "Nothing" sense of "Null" thats evaluating to zero, its the "undefined" sense of null. That is to say, Null un-defines a variable, and its not Null, but the the undefined variable that evaluates as zero, so it quite *explicitely* defines "unknown" as zero, and that is obviously incorrect maths. That is what you have to compare with other languages, and I cant think of a language, including Javascript that doesnt at least throw an exception when attempting arithmatic in a variable that hasnt even been allocated yet.

duck monster fucked around with this message at 06:30 on Feb 2, 2009

gibbed
Apr 10, 2006

duck monster posted:

throw an exception when attempting arithmatic in a variable that hasnt even been allocated yet.

quote:

Notice: Undefined variable: test in test.php on line 2
code:
<?php
	1 + $test;

duz
Jul 11, 2005

Come on Ilhan, lets go bag us a shitpost


duck monster posted:

Yes, it would be a terrible idea for a php coder to discuss a fault in PHP in the PHP thread.

Anyway, it gets more fun..

At the same time is evaluating null as zero in math,


The last PHP Sucks thread died about two months ago, please go start up a new one instead of making GBS threads all over this one.

supster
Sep 26, 2003

I'M TOO FUCKING STUPID
TO READ A SIMPLE GRAPH
Honestly what I find hillarious is that it's not even remotely a fault of the language. You could have picked one of dozens, but you picked something that is exactly how a weakly typed language is supposed to behave. And then when you realized that you resorted to arguing that weakly typed languages are bad in general.

Maybe you can hit up the C/C++ thread next.

karms
Jan 22, 2006

by Nyc_Tattoo
Yam Slacker

duck monster posted:

Mind you I'll add that 1 + "1" = 2 is loving terrible, especially if your expecting the correct answer of 50. (The ordinal position of ASCII "1" is 49).
:spergin:

No scripting language ever is going to evaluate a character to its ordinal position implicitly. What the hell man

Internet Headache
May 14, 2007

Mercator posted:

:spergin:

No scripting language ever is going to evaluate a character to its ordinal position implicitly. What the hell man
Especially when it's a string type and not a char type.

I don't know why people cite things that made PHP as popular as it is as the reasons why it sucks. Loose typing, type juggling, monolithic function library, and "magic" behavior probably led to its widespread usage, even if the principles go against everything you were taught in CS. If you want PHP to act like X language just use X language.

Internet Headache fucked around with this message at 12:03 on Feb 2, 2009

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe
Some of duck monster's objections are ones I can identify with. I've had to diagnose and fix lots of problems in my site that were caused by absent-mindedly using variables that weren't defined in the current scope, which were then treated as zero for numerical calculation purposes. It would have been so much simpler if it had instead stopped working and printed an error message, or at least printed a message. (Yes I know you can get it to print a "notice" but I didn't originally know how to do that.)

There are lots of things I hate about PHP, but as I'm just a fiddler and not a real programmer, I tend to assume there are reasons for them that make sense in some context. Mind you, I am glad it has (what seems to me) the sense to interpret "1" as 1 and not 49 in an integer context.

Ferg
May 6, 2007

Lipstick Apathy

Mercator posted:

:spergin:

No scripting language ever is going to evaluate a character to its ordinal position implicitly. What the hell man

He's firebombing at this point, don't bother trying to understand his point of view.

Stephen
Feb 6, 2004

Stoned
When I run this query:
code:
DELETE st 
FROM spaces.stores AS st 
WHERE st.id = 4
In PHPMyAdmin, the query is executed successfully.

However when I run this query in my PHP script, I get an error:
"Unknown table 'st' in MULTI DELETE"

Any ideas what could cause this?

spiritual bypass
Feb 19, 2008

Grimey Drawer
I don't know what exactly is causing that, but if I was typing it then it would look like
code:
DELETE FROM spaces.stores st WHERE st.id = 4
I wish I could provide a better explanation, but your syntax just looks wrong to me.

Stephen
Feb 6, 2004

Stoned

royallthefourth posted:

I don't know what exactly is causing that, but if I was typing it then it would look like
code:
DELETE FROM spaces.stores st WHERE st.id = 4
I wish I could provide a better explanation, but your syntax just looks wrong to me.
Well it was originally a multi-table delete that I stole syntax directly from the MySQL Delete Reference page.
code:
DELETE st, loc, sp, m FROM spaces.stores AS st
LEFT JOIN spaces.locations AS loc ON loc.store_id = st.id 
LEFT JOIN spaces.spaces AS sp ON sp.store_id = st.id 
LEFT JOIN spaces.markers AS m ON m.space_id = sp.id 
WHERE st.id = 4
But it has the same result if I get rid of all the joins, so I assumed it would just be easier to read/troubleshoot in short form.

Edit:
code:
DELETE spaces.stores, spaces.locations, spaces.spaces, spaces.markers FROM spaces.stores 
LEFT JOIN spaces.locations ON spaces.locations.store_id = spaces.stores.id 
LEFT JOIN spaces.spaces ON spaces.spaces.store_id = spaces.stores.id 
LEFT JOIN spaces.markers ON spaces.markers.space_id = spaces.spaces.id 
WHERE spaces.stores.id = 4
Now works in PHP as well. Oh well, gently caress aliases.

Stephen fucked around with this message at 23:36 on Feb 2, 2009

Zorilla
Mar 23, 2005

GOING APE SPIT

royallthefourth posted:

I don't know what exactly is causing that, but if I was typing it then it would look like
code:
DELETE FROM spaces.stores st WHERE st.id = 4
I wish I could provide a better explanation, but your syntax just looks wrong to me.

In the infinite genius of how PHP parses things, it's probably totally unaware of the "st" alias until it reaches "spaces.stores AS st", which is why royallthefourth's syntax works instead.

supster
Sep 26, 2003

I'M TOO FUCKING STUPID
TO READ A SIMPLE GRAPH

Zorilla posted:

In the infinite genius of how PHP parses things, it's probably totally unaware of the "st" alias until it reaches "spaces.stores AS st", which is why royallthefourth's syntax works instead.
PHP doesn't parse SQL :confused:




edit: \/\/ Seems to be a repeating occurance in this thread...

supster fucked around with this message at 02:41 on Feb 3, 2009

Golbez
Oct 9, 2002

1 2 3!
If you want to take a shot at me get in line, line
1 2 3!
Baby, I've had all my shots and I'm fine

Zorilla posted:

In the infinite genius of how PHP parses things, it's probably totally unaware of the "st" alias until it reaches "spaces.stores AS st", which is why royallthefourth's syntax works instead.

Finding things wrong with PHP is like shooting dead fish in a barrel with a shotgun, but I think you missed the barrel with this one.

Zorilla
Mar 23, 2005

GOING APE SPIT
Yeah, thinking about it again, I should have realized I was waaay off.

Golbez
Oct 9, 2002

1 2 3!
If you want to take a shot at me get in line, line
1 2 3!
Baby, I've had all my shots and I'm fine
I've decided it's time to switch from mysql to mysqli. I've got the basics down, but I'm unsure as to how to get results from a prepared query. The tutorials I've found say use mysqli_stmt->bind_result; I personally don't like this, as it (I think) implies a certain order in the select that, while yes, I can enforce it, I don't want to. I might one day add or remove something from a select, and don't want to have to change the order of the variables in the ->bind_result.

I want something similar to mysql_fetch_assoc, which doesn't care what order things are in. Also, based on the documentation on the PHP site, I don't see how to get to a Result object through a prepared query, only through a simple one. Maybe I don't need to, but I can see it exists. This problem is likely because the documentation, while pretty good on a per-function basis, is pretty lovely for the overall extension.

So let's say I have something like this in my old system:

code:
$query = "SELECT Category, Location FROM Main WHERE ID = ".$id;
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
echo 'Category: ',$row['Category'],'<br>';
echo 'Location: ',$row['Location'],'<br>';
The way I understand it presently, in mysqli I've gotten this far:

code:
$db = new mysqli($host,$user,$password,$database);
$query = "SELECT Category, Location FROM Main WHERE ID = ?";
$stmt = $db->prepare($query);
$stmt->bind_param('i',$id);
$stmt->execute();
... what now? How do I get an associative array out of this? Or should I care, and just learn to live with the buffered results?

gibbed
Apr 10, 2006

Golbez posted:

... what now? How do I get an associative array out of this? Or should I care, and just learn to live with the buffered results?
Wow, that's uh, pretty crappy way of returning results. Is PDO an option?

Golbez
Oct 9, 2002

1 2 3!
If you want to take a shot at me get in line, line
1 2 3!
Baby, I've had all my shots and I'm fine

gibbed posted:

Wow, that's uh, pretty crappy way of returning results. Is PDO an option?

Well that's just it, I'm assuming I'm missing a better way of returning results, but the best the PHP documentation will give me is a bland list of functions, rather than answering the simple question, "What methods does one use to get results from a prepared query?" I'd rather avoid PDO, since I don't need the abstraction it offers; this will be once and always on a MySQL database.

gibbed
Apr 10, 2006

Golbez posted:

Well that's just it, I'm assuming I'm missing a better way of returning results, but the best the PHP documentation will give me is a bland list of functions, rather than answering the simple question, "What methods does one use to get results from a prepared query?" I'd rather avoid PDO, since I don't need the abstraction it offers; this will be once and always on a MySQL database.
True, but from what I can tell from the mysqli documentation, PDO would probably be a better option (not to mention you don't have to install the other drivers, just pdo and pdo_mysql).

Internet Headache
May 14, 2007
I don't know of a way to get an associative array with the standard mysqli library. I think you're stuck with mysql_stmt->bind_result() unless you use an abstraction layer like pdo.

php:
<?
$db = new mysqli($host,$user,$password,$database);
$query = "SELECT Category, Location FROM Main WHERE ID = ?";
$stmt = $db->prepare($query);
$stmt->bind_param('i',$id);
$stmt->execute();

$stmt->bind_result($category, $location);

while ($stmt->fetch()) { // sets the value of result column 0 to $category, result column 1 to $location
    printf("%s %s\n", $category, $location);
}
?>

Golbez
Oct 9, 2002

1 2 3!
If you want to take a shot at me get in line, line
1 2 3!
Baby, I've had all my shots and I'm fine
Then is my distaste for using bind_result unique, or do other people think it's a bit weird? Then again, you need to have the proper order in bind_param, so I guess it's no big reach to force it in bind_result.

On a related note, when doing a simple query in mysqli, you get a Result object returned. I don't see that happening with a parameterized query; you're just always working with the Statement object. am I right?

Internet Headache
May 14, 2007
Yeah, if you're working with prepared statements you will always work with a statement object.

Lankiveil
Feb 23, 2001

Forums Minimalist

Supervillin posted:

Yep, do this to whatever your input variable is before you escape stuff yourself:
code:
$input = preg_replace('/\\\\+\'/', "'", $input);
That'll take care of previously escaped, double escaped, even quintuple escaped apostrophes, which you can then singly escape yourself.

Or if you want to unescape quotes and apostrophes:
code:
$input = preg_replace('/\\\\+(\'|")/', '\\1', $input);

Great, this looks like exactly what I'm after. Thanks!

BaconRelaxer
Sep 19, 2004

I have a site that needs a little help with some login codes They've never really worked and I'm at my whit's end now.. I simply need a login/logout code that when users log in they see members only links. When they log out the links need to go away.

The codes I'm presently using DO connect to my MySQL database and once logged in you see the links. However the logout is worthless and doesn't DO anything. However on the login page it doesn't tell you you're already logged in. It will just let you do it again. I honestly just need a whole new batch of codes. It's ridiculous.

Sessions and cookies are all really foreign to me. I've been searching sites for months and can't find what I'm looking for. The original codes were done by my friend years ago and have been broken for almost that long.

Sorry for the wall of code.

php:
<?

$CookieData = unserialize(stripslashes($_COOKIE['phpbb
2mysql_data']));
$myID = $CookieData['userid'];
echo "<!-- ";
var_dump($_COOKIE);
echo " -->";

?>

(id.php blah blah included on the overall header)

letting me do...


<?php if (is_numeric($myID)){?>
Member Linkage?
<? }else { ?>
Non member linkage!

<? } ?>

So that's what I'm working with as far as members only links, however, when I log in with my present login codes it doesn't...do this.

Logout:
<?
include "include/session.php";
session_unset();
session_destroy();
?>
<?
echo "<center><font face='Verdana' size='2' >Successfully logged out. <br><br> <a href=login.php>Login</a></font></center>";
?>

Login:


<form action='loginck.php' method=post>
<font face='verdana, arial, helvetica' size='2' align='center'> Login ID <BR>

<input type ='text' class='bginput' name='username' >
<BR><BR>
&nbsp;Password <BR>

<input type ='password' class='btn' name='password' >

<input type='submit' value='Submit'>
</form>

newsession:
<?php

//error_reporting(E_ERROR | E_PARSE | E_CORE_ERROR);
$session['userid']=session_id();
$session['username']=$name;
//echo $session['username'];
?>

session:
<?php
session_start();
session_register("session");
?>

loginck:
<?
include "include/session.php";

$dbservertype='mysql';
$servername='xxxx.perfora.net';

// username and password to log onto db server
$dbusername='xxxx';
$dbpassword='xxxx';
// name of database
$dbname='xxxx';

////////////////////////////////////////
////// DONOT EDIT BELOW /////////
///////////////////////////////////////
connecttodb($servername,$dbname,$dbusername,$dbpassword);
function connecttodb($servername,$dbname,$dbuser,$dbpassword)
{
global $link;
$link=mysql_connect ("$servername","$dbuser","$dbpassword");
if(!$link){die("Could not connect to MySQL");}
mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());
}
//////////////////////////////
?>
<?
$userid=mysql_real_escape_string($userid);

$name=mysql_real_escape_string($name);
$password=mysql_real_escape_string($password);

if($rec=mysql_fetch_array(mysql_query("SELECT * FROM Signup WHERE username='$username' AND Password = '$password'"))){
if(($rec['username']==$username)&&($rec['Password']==$password)){
include "include/newsession.php";
echo "<p class=data> <center>Welcome back<br></center>";
print "<script>";
print " self.location='index.php';"; // Comment this line if you don't want to redirect
print "</script>";

}
}
else {

session_unset();
echo "<font face='Verdana' size='2' color=red>Wrong Login. Use your correct Name and Password and Try <br><center><input type='button' value='Retry' onClick='history.go(-1)'></center>";

}
?>

edit: fixed tags

BaconRelaxer fucked around with this message at 07:09 on Feb 8, 2009

Zorilla
Mar 23, 2005

GOING APE SPIT

Darrish posted:

Sorry for the wall of code.

Use [php] for code blocks. I can't read what you posted as it appears now.

cannibustacap
Jul 7, 2003

Brrrruuuuuiinnssss
I am running an SQL query and feeding the results in an array that will result in lots of repeated data.

What PHP function detects repeated data and deletes it from the array?

cka
May 3, 2004
Probably this: http://us2.php.net/array_unique

cannibustacap
Jul 7, 2003

Brrrruuuuuiinnssss

cka posted:

Probably this: http://us2.php.net/array_unique

God I love PHP, after doing all the dirty work in college with C++ and poo poo, and then working in Assembly, this is hilarious.


Thanks a ton!!

Summit
Mar 6, 2004

David wanted you to have this.
I'm developing a small browser game, and one of the components of the game is that players can set events to be completed later. Specifically a player could say "I want to build this structure" and then an entry would be added to a table specifying the Time, Type, etc. of the event to be completed later. I already have the functionality for creating and completing an event written, and it works fine. However, at some point I'll need these events to be completed automatically (currently I run the update script manually). I've been investigating different ways that I can get commercial web hosting to accomplish this such as cron jobs, but I'm unsure of how to proceed.

Is it possible to set a cron job within PHP? Could I get the event and then have the script set up a cron job to complete it at a later time? It's of course possible to set cron jobs to run, but so far I've only seen it where I can set it manually. I haven't been able to find much research on this topic.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

Cloud Dog posted:

Is it possible to set a cron job within PHP? Could I get the event and then have the script set up a cron job to complete it at a later time? It's of course possible to set cron jobs to run, but so far I've only seen it where I can set it manually. I haven't been able to find much research on this topic.

I would add it to some sort of 'event queue' table and setup a cron job to run a php script every x minutes/seconds or whatever is suitable for your needs, and that script looks at the event queue and sees if there is anything it needs to do.

Summit
Mar 6, 2004

David wanted you to have this.

fletcher posted:

I would add it to some sort of 'event queue' table and setup a cron job to run a php script every x minutes/seconds or whatever is suitable for your needs, and that script looks at the event queue and sees if there is anything it needs to do.

That's actually very possible, and it's how I add it set up originally (and could easily go back to). I was concerned that some hosts might not appreciate me running a cron job every 15 seconds (I think that'd be the minimum to achieve decent feedback), but perhaps they don't pay attention to that sort of thing.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

Cloud Dog posted:

That's actually very possible, and it's how I add it set up originally (and could easily go back to). I was concerned that some hosts might not appreciate me running a cron job every 15 seconds (I think that'd be the minimum to achieve decent feedback), but perhaps they don't pay attention to that sort of thing.

Well, not knowing much about your game, I'd say that at the point you start pissing them off with the amount of resources your event script is using you've probably already outgrown shared hosting anyways, and you'll want to move on to a VPS or dedicated server.

Roctor
Aug 23, 2005

The doctor of rock.

Cloud Dog posted:

That's actually very possible, and it's how I add it set up originally (and could easily go back to). I was concerned that some hosts might not appreciate me running a cron job every 15 seconds (I think that'd be the minimum to achieve decent feedback), but perhaps they don't pay attention to that sort of thing.

I don't know anything about your game, but I can share a train of thought that I've just run down if you want to avoid crons.

Say you want to build a structure with a time delay on it. Rather than saying 'add this structure to the completed structures table at X time' you would add it to the table with an activation_time field or something.

Now when you select structures from the table you have to modify your queries to only take into account buildings that are 'built', ie, have an activation_time that is <= NOW().

I don't know if any of this is helpful, but I thought I'd share. Obviously this might not apply, and it could add a level of complexity or inefficiency to your queries depending on how your db is structured.

cannibustacap
Jul 7, 2003

Brrrruuuuuiinnssss
How do I create a matrix in PHP? Just like in matlab you can do [1 2; 3 4]. Is there an equivalent in PHP?

Are there matrix functions I can do (perhaps classes I can download somewhere that will let me do matrix functions)?

Adbot
ADBOT LOVES YOU

b0lt
Apr 29, 2005

cannibustacap posted:

How do I create a matrix in PHP? Just like in matlab you can do [1 2; 3 4]. Is there an equivalent in PHP?

Are there matrix functions I can do (perhaps classes I can download somewhere that will let me do matrix functions)?

array(array(1,2),array(3,4)); does it sort of

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply