2

Suppose I want a hashtag in a twitter-like post to be stored in its own hashtags table. The issue I'm running into is that it appears as though this loop is storing the string "Array" in the hashtag name column, instead of the actual match. This appears to be an issue with the fact that the returned array is multidimensional?

preg_match_all("/\S*#(?:\[[^\]]+\]|\S+)/", $body, $matches);
//for each loop array returned in matches
//create_hashtag()

//ISSUE with dimensionality of array
foreach($matches[0] as $v){
    create_hashtag($dbh, $v);
    attach_hashtag($dbh, $pid, $v);
}

As you can see, I tried to address this by calling the arrays at the 0th spot, but that didn't work.

Maybe the issue is in the called functions:

function attach_hashtag($dbh, $pid, $tagname) {
//insert into Tagged

try {
$sql = 'INSERT INTO Tagged (hashtag_name, post_id)
        VALUES (:name, :pid);';
$stmt = $dbh->prepare($sql);
$stmt->bindParam(':name', $tagname);
$stmt->bindParam(':pid', $pid);
$stmt->execute(); 

$array = [
    'status' => 1,
    ];
    return $array;
} catch (Exception $ex){
$array = [
    'status' => 0,
    ];
    return $array;
}}

And

function create_hashtag($dbh, $tagname) {

try {
$sql = 'INSERT INTO Hashtags (name)
        VALUES (:name);';
$stmt = $dbh->prepare($sql);
$stmt->bindParam(':name', $tagname);
$stmt->execute(); 

$array = [
    'status' => 1,
    ];
    return $array;
} catch (Exception $ex){
$array = [
    'status' => 0,
    ];
    return $array;
}
    //insert into Hashtags}

EDIT: I used var_dump to make sure the query works.

 //var_dump($pid); returns proper value
preg_match_all("/\S*#(?:\[[^\]]+\]|\S+)/", $body, $matches);
//for each loop array returned in matches

//echo $matches['Array']; - why does this return "Array"
//ISSUE with dimensionality of array
foreach($matches['Array'] as $v){
    create_hashtag($dbh, $v);
    attach_hashtag($dbh, $pid, $v); //inserts into tagged, 
}
Krpcannon
  • 111
  • 8
  • foreach($matches['Array'] as $v){ - What should this give? If you wan't to know, what's in an array, never use echo. Output $matches with var_dump or print_r. – Seb Nov 28 '16 at 11:33
  • @Krpcannon I would like to help you find a resolution with this question. Can you do a `var_export($matches)`, post that data to your question, and then ping me? – mickmackusa Mar 29 '17 at 02:33

1 Answers1

0

I did a quick test with some "invented" body:

$body = 'fasd #has kfewf #tag lkfds #test 22 #fin';
preg_match_all("/\S*#(?:\[[^\]]+\]|\S+)/", $body, $matches);
var_dump($matches);

And the result is:

array(1) {
  [0]=>
  array(4) {
    [0]=>
    string(4) "#has"
    [1]=>
    string(4) "#tag"
    [2]=>
    string(5) "#test"
    [3]=>
    string(4) "#fin"
  }
}

So your $matches[0] should be ok. Now could you check, if your mysql queries are successfull? Please check the return value of $stmt->execute(). If it's wrong, than you have a problem with your query.

$result = $stmt->execute(); 
if ($!result) {
   $error = $stmt->errorInfo();
   print_r($error);
}
Seb
  • 1,521
  • 1
  • 12
  • 19