Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-12-2011, 05:43 PM   PM User | #1
laural4705
New to the CF scene

 
Join Date: Jun 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
laural4705 is an unknown quantity at this point
PHP, MSSQL and $GLOBALS

Code:
<?php

$GLOBALS['text']=array (
	'foo1' => 'ValueOne',
	'foo2' => 'ValueTwo',
	'foo3' => 'ValueThree'
);

?>
Here is my global array, what I want is to do a find on my mssql database to pull in the global values from a table. I am trying to figure out if there is a way to do that... here is what I have (that gives errors) - I have done many revisions of the code and can't seem to get where there are no errors. Thanks for any help offered!

Code:
	$serverName = "MYSERVER";
	$connectionInfo = array( "Database"=>"MYDB", "CharacterSet" => "UTF-8","UID" => "MYUID", "PWD" =>"MYPWD");
	$conn = sqlsrv_connect( $serverName, $connectionInfo);

	if ( $conn === false ) {
		echo "Could not connect.<br>";
		die( print_r( sqlsrv_errors(), true));
	}

	$tsql1 = "SELECT term, native_term FROM [pwf].[dbo].[terms_data] 
WHERE 
	(id_language='$id_language');";

	$params1 = array();
	$options1 =  array( "Scrollable" => SQLSRV_CURSOR_KEYSET );

	/* Execute the query. */
	$stmt1 = sqlsrv_query($conn, $tsql1, $params1, $options1);
	if( $stmt1 === false ) { die( print_r( sqlsrv_errors(), true )); }

	$row_count = sqlsrv_num_rows( $stmt1 );

	if ($row_count === false) {
   		echo "Error in retrieveing row count."; }
		
	elseif ($row_count > 0) {

$GLOBALS['text']=array (

		while( $row = sqlsrv_fetch_array( $stmt1, SQLSRV_FETCH_ASSOC) ) {
				$term=$row["term"];
				$native_term=$row["native_term"];

	$term => $native_term,

}

);	
}

Last edited by laural4705; 10-12-2011 at 05:46 PM.. Reason: Fixing my code tags...
laural4705 is offline   Reply With Quote
Old 10-12-2011, 06:50 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,752
Thanks: 4
Thanked 2,468 Times in 2,437 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
This isn't valid syntax:
PHP Code:
$GLOBALS['text']=array (

        while( 
$row sqlsrv_fetch_array$stmt1SQLSRV_FETCH_ASSOC) ) {
                
$term=$row["term"];
                
$native_term=$row["native_term"];

    
$term => $native_term,

}

); 
You need to capture each of these in an array and merge it into the globals. You should also avoid using globals completely as well. There is no need to actually pull $row apart at all, its already an associative array. That can be added or merged to any other existing array. The problem you'll have is that only the last entry provided will win as it will otherwise overwrite. So you need to choose if $text will be a single dimension associative array, or if it will be a multi dimension array.
Fou-Lu is offline   Reply With Quote
Old 10-12-2011, 07:09 PM   PM User | #3
laural4705
New to the CF scene

 
Join Date: Jun 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
laural4705 is an unknown quantity at this point
Yes, I know the syntax is not valid, but I laid it out that way in order to show what I was trying to accomplish - the general idea anyway.

I think what you said about need a multidimensional array is where I need to look. What I am trying to accomplish is this:

I have a table that contains all of the text used on my website - each phrase with its own record, in each language we offer. So when a user logs into my site, a search is done using the language id, and the term is returned with the correct language for the user. I do this now with GLOBAL inc files for each language. I thought it would be nice if I could use only one GLOBAL inc file and populate it with the terms from my table.

I will try working with multi-dimensional arrays to see what I can figure out - if you have any suggestions I would love to hear them.

Thanks so much
laural4705 is offline   Reply With Quote
Old 10-13-2011, 03:40 AM   PM User | #4
laural4705
New to the CF scene

 
Join Date: Jun 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
laural4705 is an unknown quantity at this point
SOLVED IT! I used SESSIONS - thanks anyway

Code:
$tsql2 = "SELECT term, native_term FROM [mydb].[dbo].[terms] 
WHERE 
	(id_language='$id_language');";

	$params2 = array();
	$options2 =  array( "Scrollable" => SQLSRV_CURSOR_KEYSET );

	/* Execute the query. */
	$stmt2 = sqlsrv_query($conn, $tsql2, $params2, $options2);
	if( $stmt2 === false ) { die( print_r( sqlsrv_errors(), true )); }

	$row_count = sqlsrv_num_rows( $stmt2 );

if ($row_count === false) {
   		echo "Error in retrieveing row count."; }
		
		elseif ($row_count > 0) {

while( $row = sqlsrv_fetch_array( $stmt2, SQLSRV_FETCH_ASSOC) ) {

				$term=$row["term"];
				$native_term=$row["native_term"];

				$_SESSION[$term]=$native_term;
}


				}
laural4705 is offline   Reply With Quote
Reply

Bookmarks

Tags
globals, mssql, php

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:19 AM.


Advertisement
Log in to turn off these ads.