Code not continuing through loop

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 586 Views - Last Post: 18 April 2019 - 08:35 AM Rate Topic: -----

#16 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2290
  • View blog
  • Posts: 6,981
  • Joined: 15-January 14

Re: Code not continuing through loop

Posted 17 April 2019 - 03:41 PM

Quote

Line 77 is where I am attempting to print the array to the screen. I used print_r to call it. After the notice, it says Array, like it is trying to print the array, but there is nothing after it.

That's because you're converting the array to a string, and when you do that in PHP it just uses the string "Array". Don't do this:

print_r("$chunker");


do this:

print_r($chunker);

Was This Post Helpful? 0
  • +
  • -

#17 Prawnja   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 15-April 19

Re: Code not continuing through loop

Posted 17 April 2019 - 03:46 PM

I have done that Artificial Soldier, but the array is still coming back empty, AMD I can't figure out why?
Was This Post Helpful? 0
  • +
  • -

#18 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2290
  • View blog
  • Posts: 6,981
  • Joined: 15-January 14

Re: Code not continuing through loop

Posted 17 April 2019 - 06:13 PM

Work backward, print everything out until you see what you're not doing correctly. Verify everything that you're using, don't assume what anything is set to. Print all of the variables that you're using at every step until you see why it's not working correctly.
Was This Post Helpful? 0
  • +
  • -

#19 Prawnja   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 15-April 19

Re: Code not continuing through loop

Posted 17 April 2019 - 08:35 PM

OK, thank you everyone for your help!!!!

I have gotten the program ran through down to the part where it needs to be inserted to the mysql table.
I have everything put into an array called $pusher.
I am trying to implode the array to a string so that it will insert into the table.
Here is my code:

<?php


//Connecting to database
$servername = "localhost";
$username = "root";
$password = "GreenSun10";
$databasename = "studio";

$conn = mysqli_connect($servername, $username, $password, $databasename);

//Check Connection
//if (!$conn) {

//die("Connection failed: " . mysqli_connect_error());

//}

//echo "Connected Successfully";

//Scanning Directory
$dollar = glob("C:\Users\user\Desktop\CSV\XH\*");

//field Check
//if ($dollar != null){

//print_r("Data Collected");
//print_r($dollar);

//}

//else{
	//die("Data Not Collected!");

//}

foreach($dollar as $value) {

	//opening $dollar field in array
	$doc = fopen($value, "r");
	while (($data = fgetcsv($doc, 1002, "|")) !== FALSE){

		//remove first field
		$fruit = array_shift($data);
		print_r("Shifted");
		//print_r($data);

		//remove last string
		array_pop($data);
		print_r("Popped");
		//print_r($data);

		//remove last string again
		array_pop($data);
		print_r("Popped");
		//print_r($data);

		//remove last string a third time
		array_pop($data);
		print_r("Popped");
		//print_r($data);

		//split array into smaller arrays
		$chunker = array_chunk($data, 1);
		print_r("Chunked Out");
		//print_r($chunker);

		//splitting the thumbnails
		$thumb = implode(";" , $chunker[5]);
		//print_r($thumb);
		$thumper = explode(";", $thumb);
		//print_r($thumper);
		
		//making an array of keywords
		$keyword = implode(";" , $chunker[6]);
		print_r($keyword);
		$keyser = explode(";", $keyword);
		print_r($keyser);

		//popping last two off of $chunker
		array_pop($chunker);
		array_pop($chunker);
		//print_r($chunker);

		//merging all the arrays
		$pusher = array_merge($chunker, $thumper, $keyser);
		print_r($pusher);

		$s = implode($pusher);

		$query = "INSERT INTO Quarantine (Url, EmbedUrl, Title, Duration, DateAdded, Thumb1, Thumb2, Thumb3, Thumb4, Thumb5, Thumb6, Thumb7, Thumb8, Thumb9, Thumb10, Keyword1, Keyword2, Keyword3, Keyword4, Keyword5, Keyword6, Keyword7, Keyword8, Keyword9, Keyword10) VALUES$s";

		mysqli_query($conn, $query);
		//Field Check
		print_r("Data Entered Into Quarantine");
	}

	//closing dollar variable
	fclose($doc);

}

//closing connection
mysqli_close($conn);

?>




And here is what I get when I run it:

( ! ) Notice: Array to string conversion in C:\wamp\www\XH.php on line 89
Call Stack
# Time Memory Function Location
1 0.0130 386368 {main}( ) ...\XH.php:0
2 0.0490 414496 implode ( ) ...\XH.php:89

( ! ) Notice: Array to string conversion in C:\wamp\www\XH.php on line 89
Call Stack
# Time Memory Function Location
1 0.0130 386368 {main}( ) ...\XH.php:0
2 0.0490 414496 implode ( ) ...\XH.php:89

( ! ) Notice: Array to string conversion in C:\wamp\www\XH.php on line 89
Call Stack
# Time Memory Function Location
1 0.0130 386368 {main}( ) ...\XH.php:0
2 0.0490 414496 implode ( ) ...\XH.php:89

( ! ) Notice: Array to string conversion in C:\wamp\www\XH.php on line 89
Call Stack
# Time Memory Function Location
1 0.0130 386368 {main}( ) ...\XH.php:0
2 0.0490 414496 implode ( ) ...\XH.php:89

( ! ) Notice: Array to string conversion in C:\wamp\www\XH.php on line 89
Call Stack
# Time Memory Function Location
1 0.0130 386368 {main}( ) ...\XH.php:0
2 0.0490 414496 implode ( ) ...\XH.php:89
Data Entered Into Quarantine

it still doesn't put anything into the table, and I know its because of the array to string errors I'm getting. My question is, how can I put the array into a string?

Thank you!!!

This post has been edited by ArtificialSoldier: 18 April 2019 - 08:30 AM
Reason for edit:: fixed botched [code][/code] tags

Was This Post Helpful? 0
  • +
  • -

#20 andrewsw   User is offline

  • quantum multiprover
  • member icon

Reputation: 6792
  • View blog
  • Posts: 28,050
  • Joined: 12-December 12

Re: Code not continuing through loop

Posted 18 April 2019 - 12:51 AM

Those look like warnings rather than errors.

Your insert statement is incomplete, it terminates abruptly with just VALUES$s
Was This Post Helpful? 0
  • +
  • -

#21 Prawnja   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 15-April 19

Re: Code not continuing through loop

Posted 18 April 2019 - 02:50 AM

I printed out the value for $s, and it prints out array five times and then prints out the rest of the array into strings. I'm not sure why it can't pass the first 5 arrays into strings.
What should be after the $s in the query?
Was This Post Helpful? 0
  • +
  • -

#22 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2290
  • View blog
  • Posts: 6,981
  • Joined: 15-January 14

Re: Code not continuing through loop

Posted 18 April 2019 - 08:35 AM

You're trying to implode an array of arrays, so when implode tries to join each element with a string, it converts each array to a string. Like I said before, when PHP converts an array to a string it ends up with the string "Array."

Quote

What should be after the $s in the query?

An insert query looks like this:

INSERT INTO table (col1, col2, col3) VALUES ('val1', 'val2', 'val3')


The parentheses around the list of values are important, and if the values are strings they need to be quoted. This is probably a good opportunity to learn about prepared statements so that you don't need to know how to put the values in the query, you just pass the values to MySQL separately from building the query. It also protects your database against SQL injection attacks.

https://www.php.net/...-statements.php
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2