2

I've been bashing my head against the wall for a couple of days on this, because all indications are that this SHOULD work, so I'm obviously missing something simple.

First I am using a function to grab an array of user submitted comments

function get_id_comment_info($id) {
global $connection;

 $pdo = new PDO('mysql:host='.DB_SERVER.'; dbname='.DB_NAME, DB_USER, DB_PASS);
 $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec('SET NAMES "utf8"');     

$sql = "SELECT parks.parkid, parks.state, pcomment.parkid, pcomment.comment, pcomment.logname
FROM parks
LEFT JOIN pcomment
ON parks.parkid=pcomment.parkid
WHERE parks.id = '$id'";
$result = $pdo->query($sql);
$cominfo = $result->fetchAll();

return $cominfo;        
}

Then, inside of a foreach loop which processes each park by id, I am working with the data:

foreach ($display_result as $parkrow) { 
$id = $parkrow['parkid'];
$comments = get_id_comment_info($id);

$cleancom = str_replace("'", '', $comments);

print_r($cleancom);

.
.
.
}

The output for $cleancom is:

Array ( [0] => Array ( [parkid] => 462 [0] => 462 [state] => KS [1] => KS [2] => 462 [comment] => I have noticed some others here say don't waste your time, but I think it's ok. Yes, it's smaller than most, but there is fun to be had if you are an avid rider. [3] => I have noticed some others here say don't waste your time, but I think it's ok. Yes, it's smaller than most, but there is fun to be had if you are an avid rider. [logname] => John32070 [4] => John32070 ) )

It does not remove the '. If I use preg_replace then the output is simply blank. I am missing something simple, but I just can't see it!

user1483042
  • 131
  • 3
  • 14
  • 2
    ahem `global $connection;` and you're using `$pdo`. – Funk Forty Niner May 18 '16 at 01:44
  • possible duplicate of [PHP: “Notice: Undefined variable” and “Notice: Undefined index”](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Funk Forty Niner May 18 '16 at 01:45
  • Okay, I've been learning this as I go, and I don't know why that is bad. Can you explain? And is it what is keeping this from working? – user1483042 May 18 '16 at 01:45
  • your query won't happen. use `global $pdo;` - I can't see how that ever worked, not with that different variable as global. and where is `$display_result` coming from? – Funk Forty Niner May 18 '16 at 01:46
  • It may be stored as a unicode sequence or some other character depending on the database engine, the collation format, and other settings. Make sure to check the source code of the output to ensure what is going on, you know, view-source in the browser – MiltoxBeyond May 18 '16 at 01:47
  • Check for curly quotes instead of standard apostrophes, or that the data wasn't escaped somehow before entry into the database. I'm assuming your variable mixup between `$pdo` and `$connection` was a copy/paste error, since you said you got results. – miken32 May 18 '16 at 01:49
  • Neither the `global $connection` nor the `global $pdo` would make a difference. It is calling a function then returns the result set. That is not the issue. – MiltoxBeyond May 18 '16 at 01:50
  • View source of the page maybe it's `'` or `'` instead of `'`. – AbraCadaver May 18 '16 at 01:50
  • @MiltoxBeyond oh you think so eh? ok, whatever. I'm out of this question. – Funk Forty Niner May 18 '16 at 01:50
  • OK, I'm pulling this information from a database in phpAdmin. I'm sure that I am saying this wrong, but given that it is coming from the database, how do I check for &apos or curley quotes? It is just a ' in the database? and @Fred, MiltoxBeyond is correct, it is calling a function and is working. – user1483042 May 18 '16 at 01:53
  • The global only matters if you actually use it. But yeah the most likely culprit is the encoding of the value. Either a `&` html code or `\u` Unicode value – MiltoxBeyond May 18 '16 at 01:53
  • @Fred-ii- They do a `$pdo = new PDO(...` in the function. – AbraCadaver May 18 '16 at 01:54
  • I've just been hacking this site together over the years, I don't know all the things I don't know. How do I check for the Unicode value? – user1483042 May 18 '16 at 01:54
  • View source of the page. If it is then use http://php.net/manual/en/function.html-entity-decode.php or something. – AbraCadaver May 18 '16 at 01:57
  • When I view source, I get this: Array ( [0] => Array ( [parkid] => 466 [0] => 466 [state] => KS [1] => KS [2] => 466 [comment] => If you don't think KS has forests, come here and see. Pretty scenery and some good riding and something for every style rider. [3] => If you don't think KS has forests, come here and see. Pretty scenery and some good riding and something for every style rider. [logname] => John32070 [4] => John32070 ) ) – user1483042 May 18 '16 at 01:58
  • Is the fact that the collation in the database UTF8_general_ci relevant? – user1483042 May 18 '16 at 02:02
  • But they aren't accessing the $pdo from outside the function. – MiltoxBeyond May 18 '16 at 02:03
  • do the following: `print_r($comment);` I am running your code here and it's working fine. that previous code will help me try to debug what's wrong – Webeng May 18 '16 at 02:09
  • @Webeng print_r($comment) returns nothing. – user1483042 May 18 '16 at 02:13
  • @Webeng if you meant print_r($comments) this is what I get:Array ( [0] => Array ( [parkid] => 462 [0] => 462 [state] => KS [1] => KS [2] => 462 [comment] => I have noticed some others here say don't waste your time, but I think it's ok. Yes, it's smaller than most, but there is fun to be had if you are an avid rider. [3] => I have noticed some others here say don't waste your time, but I think it's ok. Yes, it's smaller than most, but there is fun to be had if you are an avid rider. [logname] => John32070 [4] => John32070 ) ) – user1483042 May 18 '16 at 02:14
  • I posted my results and a suggestion. i could add to the answer and make a function that should work for you aswell, just 1 moment – Webeng May 18 '16 at 02:19
  • I updated my answer, let me know if it now works – Webeng May 18 '16 at 02:35

2 Answers2

1

I have tested the str_replace() function and got the following results:

$myArray = array("h'i", array("hi'hi", "yo"));
print_r($myArray);

//RESULT: Array ( [0] => h'i [1] => Array ( [0] => hi'hi [1] => yo ) ) 

After applying the filter:

$filtered = str_replace( "'", "", $myArray);
print_r($filtered );

//RESULT: Array ( [0] => hi [1] => Array ( [0] => hi'hi [1] => yo ) )

What the previous results show is that any string values inside the top array are indeed being converted (h'i converted to hi) while any values that form part of the multidimensional array (any arrays within arrays) are not hi'hi remained the same. This seems to be how the function was created to work.

One solution is:

$cleancom = [];
foreach ($comments as $key => $value)
{
    $cleancom[$key] = str_replace( "'", "", $value );   
}

The reason the previous code works is because str_replace() is never being applied to a multidimensional array, while in your previous code it was. The previous code will not work however if you have something like an array within an array within another array, so if that ends up being the case in the future, I suggest you try finding a solution with callback functions.

Let me know if this worked for you.

Webeng
  • 7,050
  • 4
  • 31
  • 59
  • That sounds like a good solution, it will take me some real time to figure out how to do it. I'm not familiar with callback functions, but I'm off to learn how! – user1483042 May 18 '16 at 02:21
  • THANK YOU. I still have so much to learn, and I've been putting this thing together Frankenstein style for years. I just wish I could take some programming classes and learn it all beginning to end instead of trying to figure out something new every day. But I will take the weekend and learn about callback functions. That looks like something I need to know. Yes it worked. You're my hero! – user1483042 May 18 '16 at 02:43
0

Want to say something else, you should put the ids in an array, then use MySQL in() function, it only need to execute once, while your method will execute many times.

Specs
  • 103
  • 1
  • 7