Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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 06:46 PM. Reason: Fixing my code tags...

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.

  • #3
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #4
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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;
    }
    
    
    				}


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •