Quantcast
Channel: Microsoft Drivers for PHP for SQL Server forum
Viewing all articles
Browse latest Browse all 391

Insert records into SQL Server table using PHP

$
0
0

I hope this is the correct forum for PHP questions related to sql server.

I would like to pull data for all the cities in my county from the census API into a table in sql server.  Right now, it inserts a record, but it is blank, that is the first issue.

Other question, the variable:

$homepage

returns all the places in my county, will I have to iterate through these returned values to insert into the table ?  Do I have to direct to a file first to read them in ?

Finally, I was trying to use a WHERE clause in my

$sql

variable such as: WHERE $place LIKE '42425'";, but it throws the following error:

Array ( [0] => Array ( [0] => 42000 [SQLSTATE] => 42000 [1] => 156 [code] => 156 [2] => [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Incorrect syntax near the keyword 'WHERE'. [message] => [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Incorrect syntax near the keyword 'WHERE'. ) )

I don't see where the issue is.

PHP is not my primary language, but I cobbled the following code together based on what I could find.  Any tips would be greatly appreciated.

Thanks !

<html><head></head><body><?php
	echo "Start <br /> <br />";
	//connect to database
	$servername = "servername";
	$connectionInfo = array( "Database"=>"database", "UID"=>"username", "PWD"=>"password" );
	$conn = sqlsrv_connect( $serverName, $connectionInfo );
	echo "DB conn made <br /> <br />";
	if( $conn ) {
		echo "Connection established. <br /> <br />";
	}else{
		echo "Connection could not be established. <br /> <br />";
		die( print_r( sqlsrv_errors(), true));
	}

	$homepage = file_get_contents("https://api.census.gov/data/2020/dec/responserate?get=NAME,DRRALL,CRRINT,RESP_DATE,CRRALL,GEO_ID,DRRINT&for=place:*&in=state:13");
	//Too much info
	//echo $homepage."<br /> <br />";
	$content = json_decode($homepage, true);
	echo "Total Records: ".count($content)."<br /> <br />";
	$totalrecords = count($content)."<br /> <br />";
	echo $totalrecords;
	$NAME = $content['NAME'];
	$DRRALL = $content['DRRALL'];
	$CRRINT = $content['CRRINT'];
	$RESP_DATE = $content['RESP_DATE'];
	$CRRALL = $content['CRRALL'];
	$GEO_ID = $content['GEO_ID'];
	$DRRINT = $content['DRRINT'];
	$state = $content['state'];
	$place = $content['place'];
	echo "Fields assigned <br /> <br />";
	$trunc = "TRUNCATE TABLE Demog_CensusResponse_GACities_2020";
	$stmt2 = sqlsrv_query( $conn, $trunc );
	//Create SQL query
    $sql = "INSERT INTO Demog_CensusResponse_GACities_2020(NAME, DRRALL, CRRINT, RESP_DATE, CRRALL, GEO_ID, DRRINT, state, place)
    VALUES('$NAME', '$DRRALL', '$CRRINT', '$RESP_DATE', '$CRRALL', '$GEO_ID', '$DRRINT', '$state', '$place')";
	//WHERE $place LIKE '42425'";
	echo "Query defined <br /> <br />";
	//$params = array($content);
	//$stmt = sqlsrv_query( $conn, $sql, $params );
	//for ($x = 0; $x <= $totalrecords; $x++) {

	//Execute SQL query
	$stmt = sqlsrv_query( $conn, $sql );
	if( $stmt === false ){
		echo "Something went wrong with the query, check error below <br /> <br />";
		die( print_r( sqlsrv_errors(), true));
	}
	else {
		echo "Statement executed <br /> <br />";
	}
	//}
	/* Execute the query. */  
	//if (sqlsrv_query($conn, $tsql, $params)) {  
    //echo "Statement executed.\n";  
	//} else {  
    //echo "Error in statement execution.\n";  
    //die(print_r(sqlsrv_errors(), true));  
	//}  

	//else{
	//	echo "No error on insert <br /> <br />";
	//}
	//while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
     // echo $row['NAME'].", ".$row['DRRALL'].", ".$row['CRRINT'].", ".$row['RESP_DATE'].", ".$row['CRRALL'].", ".$row['GEO_ID'].", ".$row['DRRINT'].", ".$row['state'].", ".$row['place']."<br />";
	//}
	echo "Done ? Check table <br /> <br />";
	$sql2 = "SELECT * FROM Demog_CensusResponse_GACities_2020";
	$stmt2 = sqlsrv_query( $conn, $sql2 );
	echo $stmt2."<br /> <br />";
	sqlsrv_free_stmt( $stmt );
	sqlsrv_close( $conn );
	echo "Everything closed and freed <br /> <br />";
	?></body></html>




Viewing all articles
Browse latest Browse all 391

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>