Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
|
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 3rd, 2003, 04:00 AM
Registered User
 
Join Date: Jul 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default mysql_fetch_array with a variable string

Hey I appreciate the help just before the weekend...

This snippet is a simplified version of what I'm running, but it produces the same error

>>parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING'<<

and refers to the 7th line of code below when I run it.

Here it is:

<?php
include 'connect.inc';
$a_office = 'El Dorado Hills';
$a_os = 'winme';
$srch = mysql_query( "SELECT os_id FROM os WHERE operating_sys = '$a_os'", $db);
$sql = "INSERT INTO agent (office_id, os_id) VALUES ('$array[\"os_id\"]')";
?>


Can anyone help, please???

Thanks again! :)
 
Old July 3rd, 2003, 04:07 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 132
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Might I suggest that you repost in one of the Open source/PHP forums on the site? This one is for site feedback, really, and while I don't doubt that someone here will be able to answer your question, there's more chance if you ask in the right place :)

Chris

There are two secrets to success in this world:
1. Never tell everything you know
 
Old July 3rd, 2003, 12:32 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by chrislepingwell
 Might I suggest that you repost in one of the Open source/PHP forums on the site? This one is for site feedback, really, and while I don't doubt that someone here will be able to answer your question, there's more chance if you ask in the right place :)
Uh... what? Was the original post moved or something? This is PHP-HowTo, is it not?

Anyway, aeres's post was cross-posted to PHP-DB. I'll cross-post my response here, too.

----
The problem is that PHP is trying to substitute an array variable in a string, but your index begins with the double-quote character. The only allowable tokens in a string index are a character string (T_STRING), another variable (T_VARIABLE), or a number (T_NUM_STRING).

Rewrite that line like this:

Code:
$sql = "INSERT INTO agent (office_id, os_id) VALUES ('$array[os_id]')";
PHP assumes anything within square brackets is the index to the array, since it's already parsing it from within string context.

For more information on how PHP performs variable substitution within strings, read the manual at:
  http://www.php.net/types.string
----


Take care,

Nik
http://www.bigaction.org/
 
Old July 9th, 2003, 09:46 AM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

You could also rewrite the line like so:

$sql = "INSERT INTO agent (office_id, os_id) VALUES ('{$array["os_id"]}')";

The curly braces tells PHP that is an array. Very handy. Especially so if you get to dealing with multi-dimensional arrays.

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old July 9th, 2003, 04:01 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by quesadilla5
The curly braces tells PHP that is an array. Very handy. Especially so if you get to dealing with multi-dimensional arrays.
Well, to be specific, an opening curly brace followed by a dollar sign tell PHP that it should break out of string parsing mode and enter it's regular variable parsing mode. You don't necessarily need to have an array variable within curly braces.


Take care,

Nik
http://www.bigaction.org/
 
Old August 11th, 2003, 02:25 AM
Registered User
 
Join Date: Jul 2003
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You have 2 problems.
1) Linux is giving away error on redirection of page.
2) some PHP functions are not running in LINUX environment.
Solution:
1) You have used a function die("error................"); i think that this function intends to write some output to the customer. There it should be in mind that whenever you want to call header() function you make sure that before calling to header() function, NOTHING is being written or sent to the browser. e.g. you cannot echo/print something in browser window if u want in next step to call header() function. Therefore, try to remove die("error................"); line from your code. and also for an added security, delete ALL the blank lines from your page that comes prior to header() call.

2) for this problem you have to re-compile PHP. Make sure that apache is installed. Make PHP a module of Apache. You have to read closely the installation/compilation instruction of PHP.

Regards,

Musharaf Choudhry
Sr. Software Engineer, ICNS Inc.
Manager Operations, GiftTokri.com
www.icns.com
www.GiftTokri.com

Musharaf Choudhry
 
Old August 14th, 2003, 01:17 AM
Authorized User
 
Join Date: Aug 2003
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

to complete the discussion about variables within a string, my opinion about the arrays is that they should always be concatened as variables:

$sql = "INSERT INTO agent (office_id, os_id) VALUES ('$array[os_id]')";

should be, in fact :

$sql = "INSERT INTO agent (office_id, os_id) VALUES ('".$array['os_id']."')";

although the index is being recognized without quotes, it should always have.

php/java developer
NTIC engineer
 
Old August 18th, 2003, 01:01 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Just to play devil's advocate, how would you handle variables within heredoc strings?
Code:
$msg_body = <<<EOM
Hello, $row[name]!

Our office tried to call you at $row[phone] several
times but you kept hanging up on us!

We've even tried sending letters to $row[address]
but these have gone unanswered.

Please get in touch with us!

EOM;

To each their own, of course, but it does (in my opinion) look a lot cleaner and shorter to just use curly-brace syntax within double-quoted and heredoc strings for variable substitution. This not only saves you from having lots of ugly concatenation operations, but might also be slightly more efficient -- I don't know if there's any performance penalty involved with concatenating multiple strings together instead of simply performing variable substitutions in place.



Take care,

Nik
http://www.bigaction.org/





Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling c# String variable help simonlm C# 2005 0 May 25th, 2007 04:01 AM
mysql_fetch_array and if .. else Jeff Smitherman Beginning PHP 3 November 21st, 2005 09:08 AM
C# String to Variable? MAtkins General .NET 4 October 2nd, 2005 04:47 PM
Problem with mysql_fetch_array insomne PHP Databases 0 September 27th, 2004 02:03 PM
mysql_fetch_array problem... aeres PHP Databases 2 July 4th, 2003 12:10 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.