Link to home
Start Free TrialLog in
Avatar of DS928
DS928Flag for United States of America

asked on

Notice: Array to String Conversion

I am getting this error.  What could be causing it?

Database error: Invalid SQL: SELECT * FROM customer_order where order_id= 112167
 Notice: Array to string conversion in /home/content/d/s/t/dstr3/html/MH/Clean/includes/database.php on line 114
 
nMySQL Error: 1064 (You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '> Notice: Array to string conversion in /home/content/d/s/t/dstr3/htm' at line 1)
nSession halted.

It's on the attached page.

I think it's here.
$lines[] = "#2*". $order["order_type_id"] ."*".date('M j, Y @ g:i A',strtotime($order["order_date"])).";"
			. $order["delivery_time"] .' '. date("d-m-y", strtotime($order["order_date"])).";*"
			. $order["order_id"]."*".implode('%%', $items)."*".str_replace(',', ' ', $order["last_name"]).";"
			. str_replace( array(',', ';') , ' ', $order["street_name"])."%%".$order["phone"].";*"
			. str_replace(';', ' ', $order["comments"])."%%%%%%#";
			}

Open in new window


and here...
$lines = "%232*". $order["order_type_id"] ."*".$order["order_id"] ."*". implode(';', $items)."*"
	.str_replace(',', ' ', $order["last_name"]).";"
	.str_replace( array(',', ';') , ' ', $order["street_name"]).";"
	.$order["delivery_time"] .' '. date("d-m-Y", strtotime($order["order_date"])).";"
	.$order["phone"]."*".str_replace(';', ' ', $order["comments"])."%23";

Open in new window

database.php
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

You need to 'echo' or somehow display the line to see what is being put in there to find the error.  MySQL only sees the results, not the PHP variables and functions.
var_dump() is your friend.

A Notice is not the same as an Error.  PHP may allow you to suppress Notice messages.  But don't do that -- instead find the cause of the Notice and correct it!
Yes, but the MySQL Error can't be suppressed and is caused by the PHP 'Notice' putting in invalid text in the SQL statement.  Apparently putting an array in where a string is expected.
Avatar of DS928

ASKER

Dave, I think I found the problem.   I need to build this statement futher.  It's all gummed up, right now.

$lines = "%232*". $order["order_type_id"] ."*".$order["order_id"] ."*". implode(';', $items)."*"
	.str_replace(',', ' ', $order["first_name"] + $order["last_name"]).";"
	.str_replace(',', ' ', $order["street_number"] + $order["street_name"] +$order["apt"]).";"
	.str_replace(',', ' ', $order["cross_street"]).";"
	.str_replace(',', ' ', $order["city"] + $order["state"] +$order["zip"]).";"
	.$order["phone"].";"
	.$order["delivery_time"] .' '. date("d-m-Y", strtotime($order["order_date"])).";"
	.str_replace(';', ' ', $order["comments"])."%23";

Open in new window


which ties in with this XML

//#2*delivery policy*ordered time;requested for;*".$order["order_id"]."*2 Hamburgers with pickles%%3 French Fries%%2 Cokes with Lemon%%2 Ice Cream Sundaes*John Paul Jones;25 Camden Way Apt 4F%%@ East 25th Street%%NYC, New York 10003%%(212) 254-2520;*Throw rock. No doorbell.#

And produces this result
DELIVERY:
Ordered On:
June 6, 2013 @ 5:00 PM

Requested For:
June 6, 2013 @ 5;30PM

Order No. 112749
2 Hamburgers and
French fries

Customer Info:
John Paul Jones
25 Camden Way Apt 4F
@ East 25th Street
NYC, New York 10003
(212) 254-2520

Special Notes:
Throw rock.  No doorbell.
Order Accepted
Actually you might want to deconstruct that statement so you can find which part is causing the problem.  Compound statements are almost always the "signature" of an inexperienced programmer, and the results they achieve are almost always suboptimal.

A really good book to help you get started with PHP is available here.  Give yourself a month of study and you'll be a year ahead on any PHP project, I promise!
http://www.amazon.com/PHP-MySQL-Web-Development-Edition/dp/0321833899
Avatar of DS928

ASKER

Thanks Ray.  I am not sure how to do this otherwise.  I am trying to follow these crazy examples that I have so this printer works properly and this seems to be what they are doing.  Any help is appreciated.  Thank you.
Avatar of DS928

ASKER

Came up to this but getting wrong parameter count.

$lines = "%232*". $order["order_type_id"] ."*".$order["order_id"] ."*". implode(';', $items)."*"
			.str_replace('', $order["first_name"] + $order["last_name"]).";"
			.str_replace('', $order["street_number"] + $order["street_name"] +$order["apt"]).";"
			.str_replace('', $order["cross_street"]).";"
			.str_replace('', $order["city"] + $order["state"] +$order["zip"]).";"
			.$order["phone"].";"
			.$order["delivery_time"] .' '. date("d-m-Y", strtotime($order["order_date"])).";"
			.str_replace(';',' ', $order["comments"])."%23";

Open in new window

In your post ID: 39303246 above, I don't see any correlation between the PHP, the XML, and the results.
Avatar of DS928

ASKER

At this point forget the XML.  I think I have to get this str_replace working.


$lines = "%232*". $order["order_type_id"] ."*".$order["order_id"] ."*". implode(';', $items)."*"
			.str_replace('', $order["first_name"] + $order["last_name"]).";"
			.str_replace('', $order["street_number"] + $order["street_name"] +$order["apt"]).";"
			.str_replace('', $order["cross_street"]).";"
			.str_replace('', $order["city"] + $order["state"] +$order["zip"]).";"
			.$order["phone"].";"
			.$order["delivery_time"] .' '. date("d-m-Y", strtotime($order["order_date"])).";"
			.str_replace(';',' ', $order["comments"])."%23";

Open in new window

I would break it into separate lines like below and use '.=' to concatenate them.  That also might give a line number for the errors you get.  

But the first thing I noticed was the '+' symbols in several lines.  In PHP strings, concatenation is done with the '.' between the strings.  But stranger yet, the str_replace() functions are wrong.  str_replace takes 3 parameters.  1. the item you want to replace.  2. the item you want to replace it with.  3. the string you want to replace it in.  In the post above that I took this code from, the first item in several of the str_replace lines is an empty string.  Nothing in other words.
$lines = "%232*". $order["order_type_id"] ."*";
$lines .= $order["order_id"] ."*". implode(';', $items)."*";
$lines .= str_replace('', $order["first_name"] + $order["last_name"]).";"
$lines .= str_replace('', $order["street_number"] + $order["street_name"] +$order["apt"]).";";
$lines .= str_replace('', $order["cross_street"]).";";
$lines .= str_replace('', $order["city"] + $order["state"] + $order["zip"]).";"
$lines .= $order["phone"].";";
$lines .= $order["delivery_time"] .' ';
$lines .= date("d-m-Y", strtotime($order["order_date"])).";";
$lines .= str_replace(';',' ', $order["comments"])."%23";

Open in new window

http://php.net/manual/en/function.str-replace.php
Avatar of DS928

ASKER

Lines 3,4,5,6 throw back wrong parameters.  Just for the heck of it I am putting up a working example, however the fields are wrong for my needs.

$lines[] = "#2*". $order["OrderPolicyId"] ."*".$order["OrderId"] ."*". implode(';', $items)."*".number_format($order["DeliveryCost"], 2, '.'," ").";". number_format(($order["HandlingFee"] + $order["CCFee"]), 2, '.'," ").";".$discount_total.";".number_format($total, 2, '.'," ") .";*".$order["is_verified"].";".str_replace(',', ' ', $order["name"]).";". str_replace( array(',', ';') , ' ', $order["OrderAdd1"]).";". $order["DeliveryTime"] .' '. date("d-m-y", strtotime($order["OrderDate"])).";".$customer_prev_total.";".$PayStatus.";".$order["PaymentMethod"].":".$order["AuthorizationCode"].";".$order["phone"].";*".str_replace(';', ' ', $order["CustComments"])."#";

Open in new window

The number one difference in that code is that the '+' signs are adding numbers, not concatenating strings.  I knew that 3 thru 6 were wrong but I don't know what you're trying to do so I didn't try to fix them.  What are you wanting to do in those lines?
This is a textbook example of why and how to use the SSCCE.

Please see the code snippet at https://www.experts-exchange.com/questions/28177099/Notice-Array-to-String-Conversion.html?anchorAnswerId=39303439#a39303439

$lines = "%232*". $order["order_type_id"] ."*".$order["order_id"] ."*". implode(';', $items)."*"
			.str_replace('', $order["first_name"] + $order["last_name"]).";"
			.str_replace('', $order["street_number"] + $order["street_name"] +$order["apt"]).";"
			.str_replace('', $order["cross_street"]).";"
			.str_replace('', $order["city"] + $order["state"] +$order["zip"]).";"
			.$order["phone"].";"
			.$order["delivery_time"] .' '. date("d-m-Y", strtotime($order["order_date"])).";"
			.str_replace(';',' ', $order["comments"])."%23";

Open in new window

A careful reading of the code show us this:

Variables for the output: $lines (string)
Variables for the input:  $order, $items (arrays)

Please use var_dump() to print out the contents of the input variables.

Then using the information you can see in the output of var_dump(), please create - by hand - an exact data string showing what you want to get in the output variable.  You will find that you can highlight / copy / paste from the var_dump() output, and this process will be more reliable than trying to type the data elements.

Armed with that information we may be able to help you with the code to build the $lines string.
Avatar of DS928

ASKER

Thanks Ray. How do I do this?  Dave I am trying to put these fields together..

first_name   last_name
Result: Dave Baldwin

street_number  street_name  apt
Result: 10 Downing Street  4A

city  state  zip
Result: New York City, New York  10003
How do I do this?
Eh?  Please use var_dump() to print out the contents of the input variables.  Is that what you need help with?  If so, it would look like this:

echo '<pre>';
var_dump($order);
var_dump($items);
die();

Then you would copy and paste from that screen output.  This is not intended to produce finished code, only to produce the visual display of the input array variables that you need to manipulate in order to create the output $lines string variable.  We need to be able to see the existing input data and the desired output data in order to know what programming to write.
ASKER CERTIFIED SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DS928

ASKER

OK I did the changes and this is what var_dump returns...

 Invalid SQL: SELECT * FROM customer_order where order_id= 112202
array(22) {
  [\"order_id\"]=>
  string(6) \"112202\"
  [\"status\"]=>
  string(1) \"0\"
  [\"cust_id\"]=>
  string(1) \"0\"
  [\"rest_id\"]=>
  string(1) \"2\"
  [\"order_type_id\"]=>
  string(1) \"0\"
  [\"street_number\"]=>
  string(0) \"\"
  [\"street_name\"]=>
  string(13) \"Mulberry Lane\"
  [\"street_cross\"]=>
  string(13) \"Twin Oak Road\"
  [\"city\"]=>
  string(3) \"NYC\"
  [\"state\"]=>
  string(2) \"NY\"
  [\"apt\"]=>
  string(2) \"4F\"
  [\"zip\"]=>
  string(5) \"10003\"
  [\"comments\"]=>
  string(12) \"Make it Fast\"
  [\"delivery_status\"]=>
  string(1) \"0\"
  [\"order_date\"]=>
  string(19) \"2013-07-06 12:53:40\"
  [\"delivery_time\"]=>
  string(6) \"8:00PM\"
  [\"latest_process_status\"]=>
  string(7) \"pending\"
  [\"summary\"]=>
  string(10) \"no summery\"
  [\"first_name\"]=>
  string(3) \"Tom\"
  [\"last_name\"]=>
  string(5) \"Jones\"
  [\"phone\"]=>
  string(12) \"212.222.2222\"
  [\"message\"]=>
  NULL
}
array(1) {
  [0]=>
  string(11) \" 3 Hot Dogs\"
}

nMySQL Error: 1064 (You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '{
  [\"order_id\"]=>
  string(6) \"112202\"
  [\"status\"]=>
  string(1) \"0\"
 ' at line 1)
nSession halted.

Seems street_nmber is not returning anything.  Should have been "27"
How did you decide that the street number should be 27?  MySQL SELECT * gets all of the columns.  The WHERE clause designated the row(s).  In the row processed, the street number appears to be empty.  Could you have more than one row with order_id=112202?
Avatar of DS928

ASKER

From this page Ray is where the values are coming from.

http://menuhead.com/makeorder.php

No there should only be the one row with that record.
Does var_dump() actually return all of those back-slashes?  I've never seen anything like that.
Avatar of DS928

ASKER

Yes.  I did a cut and paste.  Still trying to figure how this all ties in with the printer and the XML
Your original question is about a MySQL error, not the printer and the XML.  Unless converting the XML to use in the database is part of the problem.
Avatar of DS928

ASKER

Not sure if it's part of the problem or not.  There seems to be no roadmap for this.  How are they tied together?
Please write and run the absolute minimalist script that will give us the SSCCE.  This script will have no CSS formatting, no JavaScript, no HTML at all.  It will only run the query and var_dump() the row that is returned.  Connect, Select, Query, var_dump() and nothing else.  No own-code PHP function calls at all.  You need to reduce this to the basic elements so we can begin to find where the problem is getting introduced.  The only roadmap I know for problems that are too hard to solve... simplify the problem.

Please post the script and the var_dump() output, then we may be able to move forward.

And if this seems like the process is confusing or is taking too long, consider hiring a professional programmer to help you.  For $1,000 you might be able to save a month of your life.  Just a thought.
Avatar of DS928

ASKER

Thanks Ray.  My pockets aren't that deep right now. LOL!  I think ifyou look at this file.
database.php I ve included the latest one.  The problem is here...

An array to a string conversion.

function feed_update($rest_id, $order_id=false){
$feed_data = feed_data($rest_id);
$fp = fopen("feeds/demo_restaurant.txt", "w+");
fwrite($fp, $feed_data);  <------------(ITS SAYING THIS LINE)------------------	
fclose($fp);
}

Open in new window


Everything elese is returning the correct info.
database.php
Sorry...
Connect, Select, Query, var_dump() and nothing else.  No own-code PHP function calls at all.  You need to reduce this to the basic elements...
We're not communicating, so I have to sign off.  Best of luck with your project.  Maybe I can help some other time.