Search the Community
Showing results for tags 'sql server'.
-
Hello I am a desktop / database programmer. I wish to write a program to read data from a database and populate a combobox in an html page. I have read that PHP can do this. Is this correct? I would prefer not to have to use a web server since this is a local app and there will be one page and it will be static excpet for a few selection criteria (like a combobox). All the examples I see are for web pages. If this can be done can someone point me in the right direction? Thanks JM
-
Hi Everyone, I have this php page that runs couple of MS SQL stored procedures to fetch me values from SQL Server, however I have run into a problem where not more then 2 stored procedures are executed at same time. For instance below mentioned code only fetches me values for 1st and 2nd SP's. If I need values from the 3rd stored procedure then I would have to comment either one of the first two stored procedure. Can someone please tell me what is that I am doing wrong. I am relatively new to PHP, so please be gentle. Any help would be highly appreciated. Thanks. <?php if($conn = mssql_connect('host', 'user', 'pass')) echo 'Connected to SQLEXPRESS<BR>'; if(mssql_select_db("database",$conn)) echo 'Selected DB: SomeDatabase<BR>'; $variable1=something; $sql_statement = mssql_init("stored_procedure1 '$variable1'", $conn); $result=mssql_execute($sql_statement); $x=0; while ($row = mssql_fetch_assoc($result)) { $column2[$x]= $row['column2']; $column1= $row['column1']; $x++; } echo "Value 1: $column1 </br>"; echo "Value 2: $column2[0] </br>"; echo "Value 3: $column2[1] </br>"; echo "Value 4: $column2[2] </br>"; echo "Value 5: $column2[3] </br>"; echo "Value 6: $column2[4] </br>"; echo "Value 7: $column2[5] </br>"; mssql_free_statement($sql_statement); $sql_statement = mssql_init("stored_procedure2 '$variable1'", $conn); $result=mssql_execute($sql_statement); $x=0; while ($row = mssql_fetch_assoc($result)) { $someval1[$x]= $row['somecolumn1']; $someval2[$x]= $row['somecolumn2']; $someval3= $row['somecolumn3']; $someval4= $row['somecolumn4']; $someval5= $row['somecolumn5']; $someval6= $row['somecolumn6']; $x++; } echo "Something1: $someval1[0]</br>"; echo "Something2: $someval3 </br>"; echo "Something3: $someval2[0] </br>"; echo "Something4: $someval4 </br>"; echo "Something5: $someval6 </br>"; echo "Something6: $someval5 </br>"; mssql_free_statement($sql_statement); $sql_statement = mssql_init("stored_procedure3 '$variable1'", $conn); $result=mssql_execute($sql_statement); $x=0; while ($row = mssql_fetch_assoc($result)) { $somevaluecheck= $row['somecolumncheck']; $x++; } echo "SomethingCheck: $somevaluecheck </br>"; mssql_free_statement($sql_statement); ?>
-
I am having the search form query a inner joined table from multiple other tables in MySQL. I am using a local intranet with Xampp/Apache with When I run the SQL in PHPMyAdmin, it runs Perfect and produces the results exactly as I want them to appear on my website. My database name is: loodt My tables are: ee, er, aa, adjuster and attorney. I have foreign keys on the ee table for the other tables (ee.er_id = er.id) etc... However, when I try to run a search on my website, it pops up an error saying "Could not query database for search results, MYSQL ERROR Table 'loodt.ee a" doesn't exist." So I tried taking away the shortcut letters [referenced below in code] (A, B, C, D< E, & F) and typing in ee.name, er.name, etc... instead of assigning these letters and it didnt work either. I was using the search engine already to query results from a single table before I started to modify it a few days ago, So I know it works and has to be an issue with the "temporary" table and my lack of knowledge of course. Here is the search engine javascript: $(function(){ //Hide the result list on load $('#results-list').hide(); //Click event when search button is pressed $('#search').click(function(){ doSearch(); }); //Keypress event to see if enter was pressed in text input $('#text').keydown(function(e){ if(e.keyCode == 13){ doSearch(); } }); }); > function doSearch() { var searchText = $('#text').val(); //Rehide the search results $('#results-list').hide(); $.ajax({ url: './search.php', type: 'POST', data: { 'text': searchText }, beforeSend: function(){ //Lets add a loading image $('#results-holder').addClass('loading'); }, success: function(data) { //Take the loading image away $('#results-holder').removeClass('loading'); //Was everything successful, no errors in the PHP script if (data.success) { //Clear the results list $('#results-list').empty(); //Display the results //Check to see if there are any results to display if(data.results.length > 0) { //Loop through each result and add it to the list $.each(data.results, function(){ //Give the list element a rel with the data results ID incase we want to act on this later, like selecting from the list $('#results-list').append( "<li rel='" + this.A_name + "'>" + this.D_name + " | " + this.A_file_no + " | " + this.A_claim_no + " | " + this.A_adj_no + " | " + this.F_acronym + " | " + this.A_doi + " | " + this.A_opened + " | " + this.A_status + " | " + this.A_redwells + " | " + this.E_firm_name + " | " + this.B_name + " | " + this.C_initials + " </li>"); }); } else { //If there are no results, inform the user - add 'no-results' class so we can style it differently $('#results-list').append("<li class='no-results'>Your search did not return any results</li>"); } //Show the results list $('#results-list').fadeIn(); } else { //Display the error message alert(data.error); } } }); } The javascript file above is called from my index page in the header, not sure if that is important. And this is the search.php file that is called inside the javascript code above. > <?php //Prepare an array to hold data we are going to send back to the jQuery $data = array( 'results' => array(), 'success' => false, 'error' => '' ); //Has the text been posted? if (isset($_POST['text'])) { //Connect to the database > $dbhost = 'localhost'; //hostname $dbuser = 'xxxx'; //database username $dbpass = 'xxxxx'; //database password $dbname = 'loodt'; //database name > //Create the connection to the mySQL database or catch the exception if there is an error $db = new mysqli($dbhost, $dbuser, $dbpass); > $db->select_db($dbname); > if($db->connect_errno > 0){ die('ERROR! - COULD NOT CONNECT TO mySQL DATABASE: ' . $db->connect_error); } > > //Escape the text to prevent SQL injection $text = $db->real_escape_string($_POST['text']); //Run a LIKE query to search for titles that are like the entered text > $q = "SELECT `A.name AS A_name`, `D.name AS D_name`, `A.file_no AS A_file_no`, `A.claim_no AS A_claim_no`, `A.adj_no AS A_adj_no`, `F.acronym AS F_acronym`, `A.doi AS A_doi`, `A.opened AS A_opened`, `A.status AS A_status`, `A.redwells AS A_redwells`, `C.initials AS C_initials`, `B.name AS B_name`, `E.firm_name AS E_firm_name` FROM `ee A` INNER JOIN `adjuster B` ON `A.adjuster_id` = `B.id` INNER JOIN `attorney C` ON `A.attorney_id` = `C.id` INNER JOIN `er D` ON `A.er_id` = `D.id` INNER JOIN `aa E` ON `A.aa_id` = `E.id` INNER JOIN `wcab F` ON `A.wcab_id` = `F.id` > WHERE `A_name` LIKE '%{$text}%' OR `A_file_no` LIKE '%{$text}%' OR `A_claim_no` LIKE '%{$text}%' OR `A_adj_no` LIKE '%{$text}%' OR `A_doi` LIKE '%{$text}%'";; $result = $db->query($q); //Did the query complete successfully? if (!$result) { //If not add an error to the data array $data['error'] = "Could not query database for search results, MYSQL ERROR: " . $db->error; } else { //Loop through the results and add to the results array while ($row = $result->fetch_assoc()) { $data['results'][] = array( 'A_name' => $row['A_name'], 'D_Name' => $row['D_name'], 'A_file_no' => $row['A_file_no'], 'A_claim_no' => $row['A_claim_no'], 'A_adj_no' => $row['A_adj_no'], 'F_acronym' => $row['F_acronym'], 'A_doi' => $row['A_doi'], 'A_opened' => $row['A_opened'], 'A_status' => $row['A_status'], 'A_redwells' => $row['A_redwells'], 'C_initials' => $row['C_initials'], 'B_name' => $row['B_name'], 'E_firmname' => $row['E_firmname'] ); } //Everything went to plan so set success to true $data['success'] = true; } } //Set the content type for a json object and ensure charset is UTF-8. NOt utf8 otherwise it will not work in IE (Darn IE! >.<) header("Content-Type: application/json; charset=UTF-8"); //json encode the data and cast to an object so we can reference items like this.id in the javascript instead of this['id'] etc. echo json_encode((object)$data); ?>
-
I have this import code and it works if I take the header out of the csv file, but I would like to keep the header in the file so I can keep a template for my co-workers to use so there is no issues with what columns are which. Here is my code: <?php /* Format the errors and die */ function get_last_error() { $retErrors = sqlsrv_errors(SQLSRV_ERR_ALL); $errorMessage = 'No errors found'; if ($retErrors != null) { $errorMessage = ''; foreach ($retErrors as $arrError) { $errorMessage .= "SQLState: ".$arrError['SQLSTATE']."<br>\n"; $errorMessage .= "Error Code: ".$arrError['code']."<br>\n"; $errorMessage .= "Message: ".$arrError['message']."<br>\n"; } } die ($errorMessage); } /* connect */ function connect() { if (!function_exists('sqlsrv_num_rows')) { // Insure sqlsrv_1.1 is loaded. die ('sqlsrv_1.1 is not available'); } /* Log all Errors */ sqlsrv_configure("WarningsReturnAsErrors", TRUE); // BE SURE TO NOT ERROR ON A WARNING sqlsrv_configure("LogSubsystems", SQLSRV_LOG_SYSTEM_ALL); sqlsrv_configure("LogSeverity", SQLSRV_LOG_SEVERITY_ALL); include '/includes/db_connect.php'; if ($connection === FALSE) { get_last_error(); } return $connection; } function query($connection, $query) { $result = sqlsrv_query($connection, $query); if ($result === FALSE) { get_last_error(); } return $result; } /* Prepare a reusable query (prepare/execute) */ function prepare ( $connection, $query, $params ) { $result = sqlsrv_prepare($connection, $query, $params); if ($result === FALSE) { get_last_error(); } return $result; } /* do the deed. once prepared, execute can be called multiple times getting different values from the variable references. */ function execute ( $stmt ) { $result = sqlsrv_execute($stmt); if ($result === FALSE) { get_last_error(); } return $result; } function fetch_array($query) { $result = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC); if ($result === FALSE) { get_last_error(); } return $result; } $connection = connect(); /* prepare the statement */ $query = "INSERT Records values ( ? , ? , ? )"; $param1 = null; // this will hold col1 from the CSV $param2 = null; // this will hold col2 from the CSV $param3 = null; // this will hold col3 from the CSV $params = array( &$param1, &$param2, &$param3 ); $prep = prepare ( $connection, $query, $params ); // $result = execute ( $prep ); //get the csv file $file = $_FILES["importrecords"]["tmp_name"]; /* Here is where you read in and parse your CSV file into an array. That may get too large, so you would have to read smaller chunks of rows. */ $csv_array = file($file); $_SESSION['records_row_count'] = 0; foreach ($csv_array as $row_num => $row) { $_SESSION['records_row_count']++; $row = trim ($row); $column = explode ( ',' , $row ); $param1 = $column[0]; $param2 = $column[1]; $param3 = $column[2]; // insert the row $result = execute ( $prep ); } /* Free statement and connection resources. */ sqlsrv_close($connection); header( "Location: import.php?successrecords=1" ); ?> How would I accomplish leaving the header in the csv file but skipping on the import? I tried adding a count, but that didn't work. Thanks for the help in advance!
-
I wonder if anyone can help me. I'm creating an application in PHP and am using a SQL server database on my local computer to develop the application. The only problem is I cannot connect to the database. My code looks like <?php $serverName = "LIAMJAY-PC\SQLEXPRESS"; //serverName\instanceName $connectionInfo = array( "Database"=>"ONEDB"); $conn = sqlsrv_connect( $serverName, $connectionInfo); if( $conn ) { echo "Connection established.<br />"; }else{ echo "Connection could not be established.<br />"; die( print_r( sqlsrv_errors(), true)); } ?> to connect to the database. However, everytime I run the code I keep getting a 18456 error code. Does anybody know what my problem is and if so, what is the solution???
- 2 replies
-
- php
- sql server
-
(and 1 more)
Tagged with:
-
I have a problem in connecting the wampp localhost to sql server , There is a lan network that it`s server is windows server 2008 with sql server 2008 , I want to connect to this sql by php in a client with windows 7 , but when I run mssql_connect function It shows that coudlent connect to this IP , It shows Reply when I ping server IP . Do I have to change anything in wampp or php application in localhost ? It is php 5.3 the latestversion of wampp . Other programmers can connect to this sql by C sharp and Delphi , Only me cant access that by php I use VmWare also , and install windows server 2008 and sql server 2008 , and create network between win7 and windows server by NAT , even in this way I cant access to sql by php , but there is reply in pinging servers IP .