email protected>> // creates a table called redtesttable // adds some data to it // reads the data back from it // deletes the table // The name of the ODBC data source for the SQL server: $sqlserver = "test"; // The username and password to access the above server: $username = ""; $password = ""; // The name of the test table to create // WARNING the table will be dropped when the script finishes running $testtablename = "redtesttable"; // =================================== echo "
making connection to ODBC data source '$sqlserver':"; $conn = odbc_connect($sqlserver, $username, $password); if (!$conn){ echo "
failed to connect!"; exit; } //======================================= // Errors ignored for this one as it will fail // anyway if the table doesn't already exist echo "
dropping table '$testtablename'"; $sql = "drop table $testtablename;"; @$ret = odbc_exec($conn, $sql); if (!$ret) { echo "
failed to drop test table, carrying on anyway..."; } //======================================= echo "
creating a table '$testtablename'"; $sql = "create table $testtablename (ID int not null, "; $sql .= "test_a varchar(255), test_b varchar(255), test_c int);"; $ret = odbc_exec($conn, $sql); if (!$ret) { echo "
failed to make test table!"; exit; } //======================================= echo "
inserting data into table '$testtablename'
"; $sql = "insert into $testtablename (test_a, test_b, test_c) values (?, ?, ?);"; $sth = odbc_prepare($conn, $sql); if (!$sth) { echo "
failed to prepare query!"; exit; } for ($i =0; $i<30; $i++){ $a = "Test data $i"; $b = "$i test data"; $array = array($a, $b, $i); $ret = odbc_execute($sth, &$array); if (!$ret) { echo "
failed to insert data!"; exit; } } //======================================= echo "
reading data from table '$testtablename'"; $sql = "select * from $testtablename;"; $sth = odbc_exec($conn, $sql); if (!$sth) { echo "
failed to execute sql!"; exit; } echo odbc_result_all($sth); //======================================= echo "
dropping table '$testtablename'"; $sql = "drop table $testtablename;"; $ret = odbc_exec($conn, $sql); if (!$ret) { echo "
failed to drop test table!";
exit;
}

