php-general Digest 15 Jul 2010 11:48:00 -0000 Issue 6847

Topics (messages 306897 through 306907):

Re: Malformed UTF-8 Data in JSON
        306897 by: Jim Lucas

Re: Static Class Member References
        306898 by: Daniel Kolbo

Re: Malformed UTF-8 Data in JSON [SOLVED]
        306899 by: Dave M G
        306902 by: Benjamin Hawkes-Lewis

How to alter the schema of a database to introduce new features or change the 
current features
        306900 by: Slith One
        306901 by: Paul M Foster
        306903 by: Pete Ford
        306904 by: Ashley Sheridan
        306905 by: Pete Ford

Re: Problem when adding special characters to an XML file
        306906 by: te0t3l
        306907 by: Richard Quadling

Administrivia:

To subscribe to the digest, e-mail:
        [email protected]

To unsubscribe from the digest, e-mail:
        [email protected]

To post to the list, e-mail:
        [email protected]


----------------------------------------------------------------------
--- Begin Message ---
Dave M G wrote:
> PHP Users,
> 
> I'm decoding some JSON data in PHP to convert it into an array.
> 
> However, it's not working, and json_last_error() is returning a value of
> "4", which I believe means "Malformed UTF-8 characters, possibly
> incorrectly encoded".
> 
> I try at every turn in every setting to ensure that all my code and
> connections are in UTF-8.
> 
> But how can I be sure it's valid UTF-8? I've tried using the PHP command
> utf8_encode() on the string, but that hasn't changed anything.
> 
> And can I trust the error message?
> 
> Any help or advice would be much appreciated.
> 
> By the way, here is the code I'm currently testing with. I'm just
> encoding and then decoding a string right in the PHP just to get it to
> work before I even try getting the data from anywhere else.
> 
> $myData ='{"display_name":"Test
> Guy","email":"[email protected]","timeout":"1279145273"}';
> $myArray1 = json_encode($myData);
> $myArray2 = utf8_encode (stripslashes($myArray1));
> $myArray = json_decode($myArray2, true);
> $jsonerror = json_last_error();
> 

Were you meaning to strip the slashes??  That will break things...  They are
their for a reason.  :)

Try it without that stripslashes() and see what happens.

Also, if on that error page they are using a bitwise numbering scheme it would
not be the last one, it would be the third error message

Value   Constant
1       JSON_ERROR_NONE
2       JSON_ERROR_DEPTH
4       JSON_ERROR_CTRL_CHAR
8       JSON_ERROR_SYNTAX
16      JSON_ERROR_UTF8

Just to be sure, echo each of the above constants and see what the value is.

And, if my suspicion is correct it is going to be because when you stripslashes
and then try and decode it, it breaks because the escaped characters are not
longer escaped.

My suggestion would be to UTF*_encode() each piece of data before you stuff it
into your json string.  Then one you have built your json string from the
encoded data, run it through json_encode() and you should be fine.

2 pennies for my thoughts please... :)

-- 
Jim Lucas

A: Maybe because some people are too annoyed by top-posting.
Q: Why do I not get an answer to my question(s)?
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?

--- End Message ---
--- Begin Message ---

David Harkness wrote:
> Ah, so assigning a reference to a variable already holding a reference
> changes that variable's reference only in the same way that unsetting a
> reference doesn't unset the other variables referencing the same thing, yes?
> 
> $a = 5;
> $b = &$a;
> print $a;
>> 5
> unset($b);  // does not affect $a
> print $a;
>> 5
> 
> // and according to Mike's previous message
> $b = &$a;
> $c = 10;
> $b = &$c;  // does not affect $a
> print $a
>> 5
> 
> That makes a lot of sense. If it didn't work this way there would be no easy
> way to untangle references. In the case of
> 
>     foreach($array as $key => &$value) { ... }
> 
> the first value in the array would continuously be overwritten by the next
> value.
> 
> 1. $value gets reference to first array value
> 2. on each step through the loop, the first array value would be overwritten
> by the next value in the loop since $value is forever tied to it by the
> initial reference assignment.
> 
> That would be a Bad Thing (tm).
> 
> Thanks for the clarification, Mike.
> 
> David
> 

The big "aha" moment for was when realizing that when assigning a
reference to a variable you only change its reference and not any other
variable that may also have shared the same reference.  Yuck that's a
mouth full.  This happened when Mike said, " NOTE: since we are
assigning a new reference to a variable which is already a reference,
ONLY this reference changes".

Yes, i agree with you David (on both of your points).  Thanks for the
example using the unset.  This further clarified/solidified my
understanding.

Now, with this new understanding, I also wish to comment that if i
assign (without reference) $this, i don't have to be too worried about
bloating the memory, b/c i'm only assigning/copying the identifer or
*handle* and not the actual object itself.

In case, someone reads this in the archive the link is:
http://php.net/manual/en/language.oop5.references.php

Mike, thank you a ton.

Regards.
`

--- End Message ---
--- Begin Message ---
Jim,

Thank you for responding.

Yes, stripslashes() was the problem. I've removed it and the code works.

However, it seems that when I send JSON data from a Javascript file, stripslashes() is necessary. That's why I had it there. I'm not entirely sure what's going on there, so obviously more experimentation is needed.

In any case, your suggestion has got me on the next step, so thanks for that tip. I'll add 2 cents to your tab!

--
Dave M G

--- End Message ---
--- Begin Message ---
On 15 Jul 2010, at 04:12, Dave M G wrote:
> Yes, stripslashes() was the problem. I've removed it and the code works.
> 
> However, it seems that when I send JSON data from a Javascript file, 
> stripslashes() is necessary. That's why I had it there. I'm not entirely sure 
> what's going on there, so obviously more experimentation is needed.

Presumably, thanks to your PHP settings, you need stripslashes() on all $_GET, 
$_POST, and $_COOKIE input.

http://www.php.net/manual/en/info.configuration.php#ini.magic-quotes-gpc

This does not apply to input from other sources.

--
Benjamin Hawkes-Lewis

--- End Message ---
--- Begin Message ---
I'm developing an app using Zend Framwork using Git for version control.

What is the best approach for updating the schema and the database
when one of us makes an update to the db structure?

currently, we have to blow out the tables and recreate them manually
to reflect the new updates.

--- End Message ---
--- Begin Message ---
On Wed, Jul 14, 2010 at 09:28:53PM -0700, Slith One wrote:

> I'm developing an app using Zend Framwork using Git for version control.
> 
> What is the best approach for updating the schema and the database
> when one of us makes an update to the db structure?
> 
> currently, we have to blow out the tables and recreate them manually
> to reflect the new updates.

I'm probably being naive, but don't you have an ALTER TABLE sql
statement available to you?

Also, for what it's worth, I don't build tables manually (at the command
line or whatever). I always create a script which will build the tables
I need. If, for some crazy reason, I do have to restart from scratch,
it's a simple matter to alter that script and re-run it.

Paul

-- 
Paul M. Foster

--- End Message ---
--- Begin Message ---
On 15/07/10 06:03, Paul M Foster wrote:
On Wed, Jul 14, 2010 at 09:28:53PM -0700, Slith One wrote:

I'm developing an app using Zend Framwork using Git for version control.

What is the best approach for updating the schema and the database
when one of us makes an update to the db structure?

currently, we have to blow out the tables and recreate them manually
to reflect the new updates.

I'm probably being naive, but don't you have an ALTER TABLE sql
statement available to you?

Also, for what it's worth, I don't build tables manually (at the command
line or whatever). I always create a script which will build the tables
I need. If, for some crazy reason, I do have to restart from scratch,
it's a simple matter to alter that script and re-run it.

Paul


Scripting is the way to go for database changes: every time I have to make a schema change I write an SQL script to do the job, including any manipulation of data required. Then I make a copy of the real data and test the hell out of the change script before going live with it. You can commit the database script to your source control at the time you commit the code changes, and then when you update the live system you run any new scripts at the same time.



--
Peter Ford, Developer                 phone: 01580 893333 fax: 01580 893399
Justcroft International Ltd.                              www.justcroft.com
Justcroft House, High Street, Staplehurst, Kent   TN12 0AH   United Kingdom
Registered in England and Wales: 2297906
Registered office: Stag Gates House, 63/64 The Avenue, Southampton SO17 1XS

--- End Message ---
--- Begin Message ---
On Thu, 2010-07-15 at 09:07 +0100, Pete Ford wrote:

> On 15/07/10 06:03, Paul M Foster wrote:
> > On Wed, Jul 14, 2010 at 09:28:53PM -0700, Slith One wrote:
> >
> >> I'm developing an app using Zend Framwork using Git for version control.
> >>
> >> What is the best approach for updating the schema and the database
> >> when one of us makes an update to the db structure?
> >>
> >> currently, we have to blow out the tables and recreate them manually
> >> to reflect the new updates.
> >
> > I'm probably being naive, but don't you have an ALTER TABLE sql
> > statement available to you?
> >
> > Also, for what it's worth, I don't build tables manually (at the command
> > line or whatever). I always create a script which will build the tables
> > I need. If, for some crazy reason, I do have to restart from scratch,
> > it's a simple matter to alter that script and re-run it.
> >
> > Paul
> >
> 
> Scripting is the way to go for database changes: every time I have to make a 
> schema change I write an SQL script to do the job, including any manipulation 
> of 
> data required. Then I make a copy of the real data and test the hell out of 
> the 
> change script before going live with it.
> You can commit the database script to your source control at the time you 
> commit 
> the code changes, and then when you update the live system you run any new 
> scripts at the same time.
> 
> 
> 
> -- 
> Peter Ford, Developer                 phone: 01580 893333 fax: 01580 893399
> Justcroft International Ltd.                              www.justcroft.com
> Justcroft House, High Street, Staplehurst, Kent   TN12 0AH   United Kingdom
> Registered in England and Wales: 2297906
> Registered office: Stag Gates House, 63/64 The Avenue, Southampton SO17 1XS
> 


ALTER TABLE is the way to go. If in doubt, look at the SQL phpMyAdmin
produces when you make the changes in there.

Thanks,
Ash
http://www.ashleysheridan.co.uk



--- End Message ---
--- Begin Message ---
On 15/07/10 09:14, Ashley Sheridan wrote:
ALTER TABLE is the way to go. If in doubt, look at the SQL phpMyAdmin
produces when you make the changes in there.

Thanks,
Ash
http://www.ashleysheridan.co.uk



Yeah, scripting "ALTER TABLE" commands ... :)

--
Peter Ford, Developer                 phone: 01580 893333 fax: 01580 893399
Justcroft International Ltd.                              www.justcroft.com
Justcroft House, High Street, Staplehurst, Kent   TN12 0AH   United Kingdom
Registered in England and Wales: 2297906
Registered office: Stag Gates House, 63/64 The Avenue, Southampton SO17 1XS

--- End Message ---
--- Begin Message ---
Hi again,

Now I download the editing XML file and open it in windows with notepad and
save with utf-8 encode, then upload the file again and can see the correct
characters, what's the problem? I'm specifying the correct encoding when
process the form and editing the XML file:

$XML = new DOMDocument('1.0', 'UTF-8');

Any idea please...
Thanks,

te0

--- End Message ---
--- Begin Message ---
On 15 July 2010 12:01, te0t3l <[email protected]> wrote:
> Hi again,
>
> Now I download the editing XML file and open it in windows with notepad and
> save with utf-8 encode, then upload the file again and can see the correct
> characters, what's the problem? I'm specifying the correct encoding when
> process the form and editing the XML file:
>
> $XML = new DOMDocument('1.0', 'UTF-8');
>
> Any idea please...
> Thanks,
>
> te0
>

Can you do a binary comparison between the 2 files (assuming you have
an original).

FC file1.xml file2.xml /b

Or can you zip them both and I can take a look at them for you (email
me directly).

--- End Message ---

Reply via email to