0

I'm moving my website from one server to another. I have exported the old php database and imported it to the new server. database name and login stayed the same (for now). There are 2 tables currently in the database. For one my php request works perfectly fine, for one it does not and I have no idea what might be wrong. The login files do not throw errors so they seem to work fine. Can anyone help? here is the code that does NOT work:

    <?php
    ini_set('display_errors', 1);

//phpinfo();
    require '.ht-loginWriting.php';
    $myquery="SELECT ID, title, shortDescription, text,date, referenceProject, workshop, otherTag FROM writings";
    $query=mysql_query($myquery);
    if(! $myquery){
        echo mysql_error();
        die;
    }
    $data=array();
    for($x=0; $x < mysql_num_rows($query); $x++){
        $data[]=mysql_fetch_assoc($query);
    }
    echo json_encode($data);


?>

.ht-loginWriting.php

    <?php
    $dbhost = 'localhost:3036';
    $dbuser = 'username';
    $dbpass = 'pw';
    $database= 'urbanalphabets';
    $table  = 'writings';
    $conn = mysql_connect($dbhost, $dbuser, $dbpass);
    $db_found = mysql_select_db($database);

if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>

and this is the code that works:

    <?php
    ini_set('display_errors', 1);

//phpinfo();
    require '.ht-loginChangeLog.php';

    $myquery="SELECT ID, title, time, text, tag_iOS, tag_web, tag_android, tag_other FROM changeLog";
    $query=mysql_query($myquery);

    if(! $myquery){
        echo mysql_error();
        die;
    }

    $data=array();

    for($x=0; $x < mysql_num_rows($query); $x++){
        $data[]=mysql_fetch_assoc($query);
    }
    echo json_encode($data);


?>

.ht-loginChangeLog.php

<?php
    $dbhost = 'localhost:3036';
    $dbuser = 'username';
    $dbpass = 'pw';
    $database= 'urbanalphabets';
    $table  = 'changeLog';
    $conn = mysql_connect($dbhost, $dbuser, $dbpass);
    $db_found = mysql_select_db($database);


if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>

after using the function suggested to convert to utf-8 the follwing ajax request throws an error:

$.ajax ( {
       url: theURL,
       dataType: "json",
       success: function ( data, textStatus, jqXHR ) {
          console.log(data);
          console.log("sucess");
           receivedData=data;
           console.log(receivedData[0].ID, receivedData[0].title, receivedData[0].shortDescription, receivedData[0].text, receivedData[0].date, "length", receivedData.length);
           initialize();
       },
       error:function(  jqXHR,  textStatus,  errorThrown ){
                  console.log("failing");
                  console.log(jqXHR);
                  console.log(errorThrown);
                  console.log(textStatus);
       }
   } );

error:

 Object {readyState: 4, getResponseHeader: function, getAllResponseHeaders: function, setRequestHeader: function, overrideMimeType: function…}
abort: function (e){var t=e||w;return u&&u.abort(t),k(0,t),this}
always: function (){return i.done(arguments).fail(arguments),this}
complete: function (){if(l){var t=l.length;(function i(t){x.each(t,function(t,n){var r=x.type(n);"function"===r?e.unique&&p.has(n)||l.push(n):n&&n.length&&"string"!==r&&i(n)})})(arguments),n?o=l.length:r&&(s=t,c(r))}return this}
done: function (){if(l){var t=l.length;(function i(t){x.each(t,function(t,n){var r=x.type(n);"function"===r?e.unique&&p.has(n)||l.push(n):n&&n.length&&"string"!==r&&i(n)})})(arguments),n?o=l.length:r&&(s=t,c(r))}return this}
error: function (){if(l){var t=l.length;(function i(t){x.each(t,function(t,n){var r=x.type(n);"function"===r?e.unique&&p.has(n)||l.push(n):n&&n.length&&"string"!==r&&i(n)})})(arguments),n?o=l.length:r&&(s=t,c(r))}return this}
fail: function (){if(l){var t=l.length;(function i(t){x.each(t,function(t,n){var r=x.type(n);"function"===r?e.unique&&p.has(n)||l.push(n):n&&n.length&&"string"!==r&&i(n)})})(arguments),n?o=l.length:r&&(s=t,c(r))}return this}
getAllResponseHeaders: function (){return 2===b?a:null}
getResponseHeader: function (e){var t;if(2===b){if(!c){c={};while(t=Tn.exec(a))c[t[1].toLowerCase()]=t[2]}t=c[e.toLowerCase()]}return null==t?null:t}
overrideMimeType: function (e){return b||(p.mimeType=e),this}
pipe: function (){var e=arguments;return x.Deferred(function(n){x.each(t,function(t,o){var a=o[0],s=x.isFunction(e[t])&&e[t];i[o[1]](function(){var e=s&&s.apply(this,arguments);e&&x.isFunction(e.promise)?e.promise().done(n.resolve).fail(n.reject).progress(n.notify):n[a+"With"](this===r?n.promise():this,s?[e]:arguments)})}),e=null}).promise()}
progress: function (){if(l){var t=l.length;(function i(t){x.each(t,function(t,n){var r=x.type(n);"function"===r?e.unique&&p.has(n)||l.push(n):n&&n.length&&"string"!==r&&i(n)})})(arguments),n?o=l.length:r&&(s=t,c(r))}return this}
promise: function (e){return null!=e?x.extend(e,r):r}
readyState: 4
responseText: "14[{"ID":"17","title":"Christmas Mlab demo day","shortDescription":"first ever public presentation of the prototype","text":"Unfortunately I was not there myself so there is not much I can say about it.","date":"2012-12-19 14:10:26","referenceProject":"1","workshop":"1","otherTag":"1"},{"ID":"22","title":"the App was born","shortDescription":"During the Multitouch Interaction Course at Media Lab Helsinki the very first prototype of the Urban Alphabets App was programmed using Open Frameworks.","text":"","date":"2012-12-10 12:18:28","referenceProject":"0","workshop":"0","otherTag":"0"},{"ID":"34","title":"

MUCH MORE CONTENT HERE

","date":"2014-04-07 11:20:54","referenceProject":"0","workshop":"0","otherTag":"0"}]"
setRequestHeader: function (e,t){var n=e.toLowerCase();return b||(e=v[n]=v[n]||e,y[e]=t),this}
state: function (){return n}
status: 200
statusCode: function (e){var t;if(e)if(2>b)for(t in e)m[t]=[m[t],e[t]];else C.always(e[C.status]);return this}
statusText: "OK"
success: function (){if(l){var t=l.length;(function i(t){x.each(t,function(t,n){var r=x.type(n);"function"===r?e.unique&&p.has(n)||l.push(n):n&&n.length&&"string"!==r&&i(n)})})(arguments),n?o=l.length:r&&(s=t,c(r))}return this}
then: function (){var e=arguments;return x.Deferred(function(n){x.each(t,function(t,o){var a=o[0],s=x.isFunction(e[t])&&e[t];i[o[1]](function(){var e=s&&s.apply(this,arguments);e&&x.isFunction(e.promise)?e.promise().done(n.resolve).fail(n.reject).progress(n.notify):n[a+"With"](this===r?n.promise():this,s?[e]:arguments)})}),e=null}).promise()}
__proto__: Object
 projectBlog.php:122
SyntaxError {stack: (...), message: "Unexpected token ["}
message: "Unexpected token ["
stack: (...)
get stack: function () { [native code] }
set stack: function () { [native code] }
__proto__: Error
 projectBlog.php:123
parsererror
elixenide
  • 44,308
  • 16
  • 74
  • 100
suMi
  • 1,536
  • 1
  • 17
  • 30
  • have you checked your query in mysql/phpmyadmin? – ɹɐqʞɐ zoɹǝɟ Apr 13 '14 at 14:47
  • test your array is it not empty .... `var_dump($data);` – Azeem Hassni Apr 13 '14 at 14:47
  • yes in phpmyadmin the query works fine – suMi Apr 13 '14 at 14:53
  • may be you have something wrong in this `.ht-loginWriting.php` – ɹɐqʞɐ zoɹǝɟ Apr 13 '14 at 14:56
  • inserting var_dump($data) just before the json_encode prints all the entries on screen... – suMi Apr 13 '14 at 14:58
  • thanks for the suggestion. I have tested this and the login-files don't throw any errors as mentioned above. so that is not the problem – suMi Apr 13 '14 at 14:58
  • Files starting with .ht might be denied in your apache settings. – Anatoliy Kim Apr 13 '14 at 15:19
  • Also, naming your table fields 'text' and 'date' might not have been the best decision - there are syntax problem in some MySQL versions, because text and date are also variable types. – Anatoliy Kim Apr 13 '14 at 15:21
  • That's not the problem. Inserting the code from the login-file into the php file doesn't change the problem. Also: the same naming convention works in one file but not in another. does not make sense. – suMi Apr 13 '14 at 15:23
  • same goes for the variable names. they work in the "changeLog" table, so why wouldn't they in the "writings" table? – suMi Apr 13 '14 at 15:23
  • Replace the first query with SELECT * FROM writings; and see what happens - if it works, it is a naming problem. – Anatoliy Kim Apr 13 '14 at 15:24
  • I have also tried querying * FROM writings but still the json is empty – suMi Apr 13 '14 at 15:24
  • see last comment, done that, json still empty – suMi Apr 13 '14 at 15:25
  • How do you call these files? – Anatoliy Kim Apr 13 '14 at 15:27
  • the filenames are requestAllWritings.php (the one that doesn't work) and requestChangeLog.php (the one that does work) – suMi Apr 13 '14 at 15:29
  • Can you edit the question and include the files, which you include? The .ht- starting ones. Maybe there is some difference in there? And, if your use var_dump($data) before outputing json_encode - does it have the info in the first file or not? – Anatoliy Kim Apr 13 '14 at 15:30
  • Also use echo mysql_num_rows in the first file - see how many rows are returned. – Anatoliy Kim Apr 13 '14 at 15:32
  • var_dumb($data) returns all the content that should be returned (as mentioned above also... – suMi Apr 13 '14 at 15:34
  • echo mysql_num_rows($query) returns 14 which is correct also – suMi Apr 13 '14 at 15:35
  • Well, it seems that json_encode kills your $data then... Also, why use mysqli_connect_errno and mysql_connect?? It will always return null to you, whether connected or not. And you don't have to use two separate files to connect to the same database - use one, table name doesn't matter in php connecting to mysql. – Anatoliy Kim Apr 13 '14 at 15:37
  • I haven't said I'm the most advanced php user. however, this very code used to work on my old server and now it doesn't any more – suMi Apr 13 '14 at 15:38
  • having it tracked down to this point the question rather is: why/how is json_encode killing my data and even more important: how to fix it?? – suMi Apr 13 '14 at 15:40
  • got you an answer separately ;) – Anatoliy Kim Apr 13 '14 at 15:41

1 Answers1

0

Well, after much comments were done here is your answer (most likely)

Link.

In general, it is an encoding problem of json_encode. And you need to have everything in UTF8.

You can look at a good solution for converting string to UTF-8 here.

Edit:

I've thought about it. While moving database to a new server you might have accidentally messed the encodings. It happens some times.

You can export dump of your database, convert into UTF-8 in some program, like NotePad++, and restore it back to the server. That's a quick fix.

Also, you might consider to start using some frameworks, like CodeIgniter or Yii or Symphony, which will do most of the work for you. Coding in pure php is difficult and using a framework, where there are examples on everything might be easier for you, unless you are trying to learn how to code in php.

Community
  • 1
  • 1
Anatoliy Kim
  • 768
  • 4
  • 13
  • I don't understand the problem really. I checked the sql file that I uploaded, it doesn't consist non-utf-8 characters. So using the function suggested in the first answer in the link you posted above solves the problem in the .php file but the ajax-request in my javascript still fails – suMi Apr 13 '14 at 15:54
  • Do you have Firebug extension of Firefox? Its great for testing network requests. In your AJAX code, use success function and output data to the log with console.log(data) command. Example: $.post('url', postdata, success: function(data) {console.log(data);}) – Anatoliy Kim Apr 13 '14 at 15:58
  • "14[{"ID <- as you can see you forgot to remove some echo. probably the one which outputs number of rows in the result. – Anatoliy Kim Apr 13 '14 at 20:30