Call to undefined function mysqli_begin_transaction() error - php

I am using a php script that handles the backup & restore of my databases. There are several problems with it but I shall post 1 error at a time. When I click restore backup button I am getting the error in the title.
I am not experienced enough to troubleshoot this error and would appreciate some help from the experts. I have posted my code where the error occurs, but if you need to see anything else please let know. The error occurs at line 109. Thanks
PHP V5.3.13
MYSQL V5.5.24
APACHE V2.2.22
<?php
function restoredata($uploadedfile, &$ustartindex, &$uti, &$ulen, &$ucount, &$name, &$limit, $restoremethod)
{
global $host, $username, $passwd, $charset, $port, $upload_path, $backup_path;
$sql = '';
$zip = new ZipArchive();
$filename = $upload_path . '/' . $uploadedfile;
if (!is_file($filename)) {
$filename = $backup_path . '/' . $uploadedfile;
}
if ($zip->open($filename) !== TRUE) {
return "Cannot open file\n.";
}
try {
$dbName = $zip->getFromName('database.txt');
if ($restoremethod == '0') {
$conn = mysqli_connect($host, $username, $passwd, $dbName, $port);
}
else {
if ($uti == 0 && $ustartindex == 0) {
$conn = mysqli_connect($host, $username, $passwd, 'mysql', $port);
}
else {
$conn = mysqli_connect($host, $username, $passwd, $dbName, $port);
}
}
if (!$conn) {
return 'Could not connect ' . mysqli_error($conn);
}
if (!mysqli_set_charset($conn, $charset)) {
mysqli_query($conn, 'SET NAMES ' . $charset);
}
$tables = $zip->getFromName('tables.txt');
if ($tables === false) {
$zip->close();
return "Could'nt find tables";
}
$limit = intval($zip->getFromName('limit_info.txt'));
$tables = explode(',', $tables);
$ulen = count($tables);
if ($uti < $ulen) {
if ($uti == 0 && $ustartindex == 0) {
if ($restoremethod == '1') {
$sql = $zip->getFromName($dbName . '.sql');
if ($sql !== false) {
$sql = explode(';' . chr(10) , $sql);
for ($i = 0; $i < count($sql) - 1; $i++) {
$result = mysqli_query($conn, $sql[$i] . ';');
if (!$result) {
mysqli_close($conn);
$zip->close();
return 'Could not run query 1: ' . mysqli_error($conn);
}
}
}
mysqli_close($conn);
$conn = mysqli_connect($host, $username, $passwd, $dbName, $port);
if (!$conn) {
$zip->close();
return 'Could not connect ';
}
for ($i = 0; $i < $ulen; $i++) {
$name = $tables[$i];
$table = '`' . $name . '`';
$sql = $zip->getFromName($table . '.sql');
if ($sql !== false) {
$sql = explode(';' . chr(10) , $sql);
for ($j = 0; $j < count($sql) - 1; $j++) {
$result = mysqli_query($conn, $sql[$j] . ';');
if (!$result) {
mysqli_close($conn);
$zip->close();
return 'Could not run query 2: ' . mysqli_error($conn);
}
}
}
}
}
}
mysqli_autocommit($conn, FALSE);
// mysqli_begin_transaction ($conn, MYSQLI_TRANS_START_READ_ONLY );//READ ONLY transaction
mysqli_begin_transaction($conn, MYSQLI_TRANS_START_READ_WRITE); <---ERROR HERE
$name = $tables[$uti];
$table = '`' . $name . '`';
$ucount = intval($zip->getFromName($table . '.txt'));
$sql = $tables = $zip->getFromName($table . '/offset' . $ustartindex . '.sql');
$nerrors = 0;
$serrors = '';
if ($sql !== false) {
$sql = explode(';' . chr(10) , $sql);
for ($i = 0; $i < count($sql) - 1; $i++) {
$sql[$i] = str_replace('NULL', "''", $sql[$i]); //avoid NULL errors
$result = mysqli_query($conn, $sql[$i] . ';');
if (!$result) {
$serrors.= 'Could not run query in ' . $table . ' : ' . mysqli_error($conn) . '<br />';
$nerrors+= 1;
break;
}
}
$ustartindex+= $limit;
}
else {
$uti+= 1;
$ustartindex = 0;
}
if (!mysqli_commit($conn)) {
return "Transaction commit failed";
}
if ($nerrors > 0) {
mysqli_close($conn);
$zip->close();
return $serrors;
}
}
mysqli_close($conn);
$zip->close();
}
catch(Exception $e) {
return var_dump($e->getMessage());
}
return true;
}
?>
I will add that this is not my script but it does all all I need it to do including showing the progress of the backup, which visually is better than just a white page.
I would if possible like to sort these errors out and have a working script. Many thanks.

From http://php.net/manual/en/mysqli.begin-transaction.php, mysqli_begin_transaction is only supported from php 5.5.0 onwards. As your running 5.3 it won't work.
You could remove the transaction and it would work, especially as your originally trying to set a read-only transaction. Also remove the commit.
I would remove the following line as this would rely on transactions working as you'd expect.
mysqli_autocommit($conn, FALSE);
If you NEED to stay on 5.3 then...
mysqli_query($conn, "START TRANSACTION");

Related

PDO Overwrite params

I've made a simple code to make multiple inserts on the database:
<?php
if (isset($_POST['numb']) && isset($_POST['email1'])) {
$error = false;
$numb = $_POST['numb'];
for ($i = 1; $i <= $numb; $i++) {
if (!isset($_POST['email' . $i])) {
$error = true;
}
}
if ($error == false) {
include 'config.php';
try {
$connection = new PDO($dsn, $user, $pass);
$suc1 = true;
} catch (PDOException $e) {
echo $e->GetMessage();
$suc1 = false;
}
if ($suc1 == true) {
$sql = "INSERT INTO alunos(email, emailKey) VALUES ";
for ($i = 1; $i <= $numb; $i++) {
$BAMBAM[$i] = '(:email' . $i . ', :emailkey' . $i . ')';
$FELIPEFRANCO[$i] = sha1(microtime() . $_POST['email'. $i]);
if ($i != $numb) {
$BAMBAM[$i] = $BAMBAM[$i] . ',';
}
$sql = $sql . $BAMBAM[$i];
}
$insert = $connection->prepare($sql);
for ($i = 1; $i <= $numb; $i++) {
$param1 = 'email' . $i;
$value1 = $_POST['email' . $i];
$param2 = 'emailkey' . $i;
$value2 = $FELIPEFRANCO[$i];
echo '<script>alert("' . $param1 . ' -> ' . $value1 . '")</script>';
$insert->bindParam($param1, $value1, PDO::PARAM_STR);
$insert->bindParam($param2, $value2, PDO::PARAM_STR);
}
try {
$insert->execute();
$suc2 = true;
} catch (PDOException $e) {
echo $e->GetMessage();
$suc2 = false;
}
echo $sql;
} else {
header('Location: addAlunosForm.php?error=internal');
}
} else {
header('Location: addAlunosForm.php?error=fill');
}
} else {
header('Location: addAlunosForm.php?error=fill');
}
?>
There is a test script up there, and, in the alert, it says exactly thiis:
email1 -> 1#gmail.com
email2 -> 2#gmail.com
email3 -> 3#gmail.com
But in the databse, it inserts the 3rd value 3 times:
3
NULL
3#gmail.com
901d4043642394ca30ea83688d944987d266b698
NULL
NULL
4
NULL
3#gmail.com
901d4043642394ca30ea83688d944987d266b698
NULL
NULL
5
NULL
3#gmail.com
901d4043642394ca30ea83688d944987d266b698
NULL
NULL
Details:
$numb is the number of inserts

simplifying 6 mysql queries in 1 page

I've got a script that opens 6 MySQL queries in 1 php page. here's the script:
if(!isset($_SESSION["name"]) || empty($_SESSION["name"])) {
header("Location: http://www.google");
die();
} else {
$servername = "localhost";
$username = "user";
$password = "123";
// Create connection
$conn = new mysqli($servername, $username, $password);
$conn2 = new mysqli($servername, $username, $password);
$conn3 = new mysqli($servername, $username, $password);
$conn4 = new mysqli($servername, $username, $password);
$conn5 = new mysqli($servername, $username, $password);
$conn6 = new mysqli($servername, $username, $password);
if ($conn4->connect_error) {
die("Connection failed: " . $conn4->connect_error);
}
$sql4 = "SELECT * FROM tableT WHERE date(ftdate) = curdate()";
$result4 = $conn4->query($sql4);
$row4 = $result4->fetch_assoc();
$conn4->close();
date_default_timezone_set("Asia/Kuala_Lumpur");
if ($result4->num_rows < 1) {
if ($conn3->connect_error) {
die("Connection failed: " . $conn3->connect_error);
}
$date = date("Y-m-d");
$sql3 = 'INSERT INTO tableT (`id`, `date`) VALUES (Null, "' . $date . '")';
if ($conn3->query($sql3) === TRUE) {
header("Location: http://google.com");
die();
} else {
echo "Error: " . $sql3 . "<br>" . $conn3->error;
}
$conn3->close();
} Else {
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if ($conn2->connect_error) {
die("Connection failed: " . $conn2->connect_error);
}
$sql = "SELECT * FROM tableB WHERE something = '0'";
$sql2 = "SELECT * FROM tableP WHERE id=1";
$result = $conn->query($sql);
$conn->close();
$result2 = $conn2->query($sql2);
$row2 = $result2->fetch_assoc();
$conn2->close();
if ($conn5->connect_error) {
die("Connection failed: " . $conn5->connect_error);
}
$month = date("m");
$sql5 = "SELECT * FROM tableB WHERE month(date) = '" . $month . "' AND something = '1' ORDER BY fid";
$result5 = $conn5->query($sql5);
if ($result5->num_rows > 0) {
$x = 1;
$monthlyt = 0;
$y = 0;
$dailyt = 0;
$day = date("Y-m-d");
while($row5 = $result5->fetch_assoc()) {
$monthlyt = $row5[ftotal] + $monthlyt;
$x = $x + 1;
$dbdate = date("Y-m-d", strtotime($row5[fdate]));
if ($dbdate == $day) {
$y = $y + 1;
$dailyt = $row5[total] + $dailyt;
}
}
$damount = ($y / $row2[something]) * 100;
$dtotal = ($dailyt / $row2[some]) * 100;
$mtotal = ($monthlyt / $row2[blah]) * 100;
}
$conn5->close();
if ($conn6->connect_error) {
die("Connection failed: " . $conn6->connect_error);
}
$sql6 = 'UPDATE tableT SET something="' . $y . '", some="' . $blah. '" WHERE tdate="' . $day . '"';
if ($conn6->query($sql6) === TRUE) {
//done
} else {
echo "Error: " . $sql6 . "<br>" . $conn6->error;
}
$conn6->close();
}
}
this page loads a dashboard showing a few different tables and some average response time calculations.
it was working previously, but I got an error saying "website redirected you too many times" Try clearing your cookies. ERR_TOO_MANY_REDIRECTS from chrome today.
So i'm thinking, is there a way I can simplify this? or would this lead to errors?

Running MySQL queries in specific order

I wonder if someone could advise me here. I have a page where I am updating information in a database, from the previous page I post any new images (with a caption too) and I have a checkbox for deleting images. On the processing page I have the following code:
<?php
session_start();
$dbhost = 'removed';
$dbuser = 'removed';
$dbpass = 'removed';
$dbname = 'removed';
function reArrayFiles($file_post) {
$file_ary = array();
$file_count = count($file_post['name']);
$file_keys = array_keys($file_post);
for ($i=0; $i<$file_count; $i++) {
foreach ($file_keys as $key) {
$file_ary[$i][$key] = $file_post[$key][$i];
}
}
return $file_ary;
}
try {
$dbo = new PDO('mysql:host=localhost;dbname='.$dbname, $dbuser, $dbpass);
}catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
$query="SELECT * FROM `soldier_info` WHERE `soldier_id` = " . $_POST['soldier_id'] ;
foreach ($dbo->query($query) as $row) {
$soldier_pre_images = explode(",", $row['soldier_images']);
}
if($_POST['deletephoto']){
$imagestodelete=$row['soldier_images'];
$captionstodelete=$row['soldier_images_captions'];
foreach($_POST['deletephoto'] as $todelete){
$deleteexplode = explode("--",$todelete);
echo $deleteexplode;
$deleteexplodepath = $deleteexplode[0];
$deletecaption=$deleteexplode[1];
$imagesafterdelete= str_replace($deleteexplodepath.",","",$imagestodelete);
echo $imagesafterdelete;
$captionsafterdelete = str_replace($deletecaption."--","",$captionstodelete);
echo $captionsafterdelete;
$unlinkpath = str_replace('site url','..',$deleteexplodepath);
unlink($unlinkpath);
}
try {
$dbo = new PDO('mysql:host=localhost;dbname='.$dbname, $dbuser, $dbpass);
$dbo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$q = $dbo->prepare("UPDATE `soldier_info` SET `soldier_images`= :image, `soldier_images_captions`= :captions WHERE `soldier_id`= :id");
$q->execute(array(":image" => $imagesafterdelete, ":captions" => $captionsafterdelete, ":id" => $_POST['soldier_id']));
}catch (PDOException $e) {
$db_error = "Error!: " . $e->getMessage() . "<br/>";
die();
}
}
if ($_FILES['photo']) {
$file_ary = reArrayFiles($_FILES['photo']);
$capcount = 0;
foreach ($file_ary as $file) {
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $file["name"]);
$extension = end($temp);
if ((($file["type"] == "image/gif")||($file["type"] == "image/jpeg")||($file["type"] == "image/jpg")||($file["type"] == "image/pjpeg")||($file["type"] == "image/x-png")||($file["type"] == "image/png"))&&($file["size"] < 9000000) && in_array(strtolower($extension), $allowedExts)) {
if ($file["error"] > 0) {
$file_error = "Return Code: " . $file["error"] . "<br>";
} else {
$newfilename = "../assets/uploads/images/" . $_POST['soldier_id'] . "/" . $file["name"];
$file_fullname = "site urlv/assets/uploads/images/" . $_POST['soldier_id'] . "/" . $file["name"];
$new_caption = $_POST['image_captions'][$capcount];
if (file_exists($newfilename)) {
$file_exists = $file["name"] . " already exists. ";
} else {
if(is_dir("../assets/uploads/images/" . $_POST['soldier_id'])){
move_uploaded_file($file["tmp_name"],$newfilename);
if (strlen($uploaded_images)>1){
$uploaded_images = $uploaded_images . ",". $file_fullname;
}else{
$uploaded_images = $file_fullname;
}
if (strlen($uploaded_captions)>1){
$uploaded_captions = $uploaded_captions . "--". $new_caption;
}else{
$uploaded_captions = $new_caption;
}
}else{
mkdir("../assets/uploads/images/" . $_POST['soldier_id'], 0777, true);
move_uploaded_file($file["tmp_name"],$newfilename);
if (strlen($uploaded_images)>1){
$uploaded_images = $uploaded_images . ",". $file_fullname;
}else{
$uploaded_images = $file_fullname;
}
if (strlen($uploaded_captions)>1){
$uploaded_captions = $uploaded_captions . "--". $new_caption;
}else{
$uploaded_captions = $new_caption;
}
}
}
if (strlen($row['soldier_images'])>1){
$uploaded_images = $row['soldier_images'] . ",". $uploaded_images;
}else{
$uploaded_images = $uploaded_images;
}
}
} else {
$file_invalid = "The file is not a jpg/gif/png file, or is larger than 20mb. Please try again.";
}
$capcount = $capcount+1;
}
try {
$dbo = new PDO('mysql:host=localhost;dbname='.$dbname, $dbuser, $dbpass);
$dbo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$q = $dbo->prepare("UPDATE `soldier_info` SET `soldier_images`= :image, `soldier_images_captions`= :captions WHERE `soldier_id`= :id");
$q->execute(array(":image" => $uploaded_images, ":captions" => $uploaded_captions, ":id" => $_POST['soldier_id']));
}catch (PDOException $e) {
$db_error = "Error!: " . $e->getMessage() . "<br/>";
die();
}
}
$query="SELECT * FROM `soldier_info` WHERE `soldier_id` = " . $_POST['soldier_id'] ;
foreach ($dbo->query($query) as $row) {
$firstname = $row['firstname'];
$surname = $row['surname'];
$yearofbirth = $row['yearofbirth'];
$dateofdeath = date("d/m/Y", strtotime($row['dateofdeath']));
$ageatdeath = $row['ageatdeath'];
$regiment = $row['regiment'];
$battallion_brigade = $row['battallion_brigade'];
$Coybty = $row['Coy/bty'];
$rank = $row['rank'];
$solider_number = $row['soldier_number'];
$Next_of_Kin = $row['Next_of_Kin'];
$CWGC = $row['CWGC'];
$Place_Died = $row['Place_Died'];
$Connection_to_Wymeswold = $row['Connection_to_Wymeswold'];
$Cemetery = $row['Cemetery'];
$LRoH = $row['LRoH'];
$Grave = $row['Grave'];
$Memorial = $row['Memorial'];
$Pier_Face = $row['Pier_Face'];
$Other_Mem = $row['Other_Mem'];
$Other_Details = $row['Other_Details'];
$soldier_images = explode(",", $row['soldier_images']);
$soldier_images_captions = explode("--", $row['soldier_images_captions']);
}
$dbo = null;
?>
This isn't a live site right now and is in testing/dev. However the issue I am seeing is that, all the queries are running, but the page loads up as though the select has run first (by this I mean that the records returned are those from before the updates as processed. Can I force this select to run last so that it gets the latest records?
I think you get the 'original' data on the last select because you are running the queries on different connections. Hence they will live in isolated transactions, that might not be properly committed when the last select runs.
If you run them all in the same PDO object I think it will work.
(If you take POST parameters and concatenate them into SQL your server will be hacked. It's just a matter of time and you have the URL here for everyone to see.)

Parse error: syntax error, unexpected T_ELSE, expecting T_FUNCTION in /home/petit/public_html/system/database/mysqli.php on line 54

I have made an opencart transfer from a server to another and i am getting the following error
Parse error: syntax error, unexpected T_ELSE, expecting T_FUNCTION in /home/petit/public_html/system/database/mysqli.php on line 54
when i try to access my site or my administrator page.
Any idea why this is happening?
I have followed the procedure step by step.
This is mysqli.php
<?php
final class MySQLi {
private $mysqli;
public function __construct($hostname, $username, $password, $database) {
$this->mysqli = new mysqli($hostname, $username, $password, $database);
if ($this->mysqli->connect_error) {
trigger_error('Error: Could not make a database link (' . $this->mysqli->connect_errno . ') ' . $this->mysqli->connect_error);
}
$this->mysqli->query("SET NAMES 'utf8'");
$this->mysqli->query("SET CHARACTER SET utf8");
$this->mysqli->query("SET CHARACTER_SET_CONNECTION=utf8");
$this->mysqli->query("SET SQL_MODE = ''");
}
public function query($sql) {
$result = $this->mysqli->query($sql);
if ($this->mysqli->errno) {
//$mysqli->errno
}
if (is_resource($resource)) {
$i = 0;
$data = array();
while ($row = $result->fetch_object()) {
$data[$i] = $row;
$i++;
}
$result->close();
$query = new stdClass();
$query->row = isset($data[0]) ? $data[0] : array();
$query->rows = $data;
$query->num_rows = $result->num_rows;
unset($data);
return $query;
} else {
return true;
}
} else {
trigger_error('Error: ' . mysql_error($this->link) . '<br />Error No: ' . mysql_errno($this->link) . '<br />' . $sql);
exit();
}
}
public function escape($value) {
return $this->mysqli->real_escape_string($value);
}
public function countAffected() {
return $this->mysqli->affected_rows;
}
public function getLastId() {
return $this->mysqli->insert_id;
}
public function __destruct() {
$this->mysqli->close();
}
}
?>
Thank you in advance
there is else without if. the if block is closed. do it like -
if ($this->mysqli->errno) {
//$mysqli->errno
if (is_resource($resource)) {
$i = 0;
$data = array();
while ($row = $result->fetch_object()) {
$data[$i] = $row;
$i++;
}
$result->close();
$query = new stdClass();
$query->row = isset($data[0]) ? $data[0] : array();
$query->rows = $data;
$query->num_rows = $result->num_rows;
unset($data);
return $query;
} else {
return true;
}
} else {
trigger_error('Error: ' . mysql_error($this->link) . '<br />Error No: ' . mysql_errno($this->link) . '<br />' . $sql);
exit();
}

PHP - bind_result to array

I'm using a prepared statement for a query that returns multiple results, which I'd want to have in an array. But bind_result won't work with arrays, so this is how I do it:
$read_items = $db->stmt_init();
$read_items->prepare("SELECT item_id, item_name FROM items");
$read_items->execute();
$read_items->bind_result($var1, $var2);
while ($read_items->fetch()) {
$item_id[] = $var1;
$item_name[] = $var2;
}
Is there clearer/better/more elegant way to put results into $item_id and $item_name arrays?
As it's visible from above, I'm using $var1 and $var2 like "intermediates" - and constantly have feeling there must be a better way.
Thanks.
I can't take credit for it, but the PHP manual has a nice solution (pasted verbatim):
<?php
// blah blah...
call_user_func_array(array($mysqli_stmt_object, "bind_result"), $byref_array_for_fields);
// returns a copy of a value
$copy = create_function('$a', 'return $a;');
$results = array();
while ($mysqli_stmt_object->fetch()) {
// array_map will preserve keys when done here and this way
$results[] = array_map($copy, $byref_array_for_fields);
}
You can use the Argument unpacking(...) operator.
$results = array(null, null);
$read_items->bind_result(...$results);
while ($read_items->fetch()) {
$item_id[] = $result[0];
$item_name[] = $result[1];
}
I really don't see the point of putting your item id's and item names in different arrays, you're breaking up a single object in two collections for ??? no reason . yes.
So, going back to this, what you should be doing is :
$items[$i]['var1']=$var1;
$items[$i]['var2']=$var2;
But this of course is still not very good.
The correct solution is to have a function that maps your mysqli result to an array so you can directly start working on that, whatever the query, here's mine - works in a few db's but not all are up to date at the moment :
function connectmysqldb($database,$host,$port,$user,$password){
global $conn;
$conn=mysqli_connect($host, $user, $password, $database,$port);
if (!$conn){
die('Error: Could not connect: ' . mysqli_connect_error());
}
return $conn;
}
function connectpgdb($database,$host,$port,$user,$password){
$connectString = 'host=' . $host . ' port=' . $port . ' dbname=' . $database . ' user=' . $user . ' password=' . $password;
$conn = pg_connect ($connectString);
if (!$conn)
{
die('Error: Could not connect: ' . pg_last_error());
}
return $conn;
}
function connectmssqldb($dbname,$host,$port,$uid,$pw){
$connectionOptions = array("UID"=>$uid,"PWD"=>$pw);
$conn = sqlsrv_connect( $host, $connectionOptions);
if (!$conn)
{
die('Error: Could not connect: ' . print_r( sqlsrv_errors(), true));
}
return $conn;
}
function sqlec($query,$dbtype,$dbname,$host,$port,$uid,$pw){
switch($dbtype){
default:
$res="Database Type not Recognized : $dbtype";
break;
case "mysql":
global $conn;
if(!isset($conn)){
$conn=connectmysqldb($dbname,$host,$port,$uid,$pw);
}
$clone=mysqli_multi_query($conn,$query);
if(!$clone){
die('Error: ' . mysqli_error($conn));
}else{
$clonearray=array();
$i=0;
if ($clone = mysqli_store_result($conn)) {
while ($row = mysqli_fetch_assoc($clone)){
$clonearray[$i]=$row;
$i++;
}
mysqli_free_result($clone);
}
$res=$clonearray;
}
break;
case "postgres":
$conn=connectpgdb($dbname,$host,$port,$uid,$pw);
$clone=pg_query($conn,$query);
if (!$clone)
{
die('Error: ' . pg_last_error());
}else{
$clonearray=array();
$i=0;
while ($row = pg_fetch_row($clone))
{
$count = count($row);
$y = 0;
while ($y < $count)
{
$c_row = current($row);
$clonearray[$i][pg_field_name($clone,$y)] = $c_row;
next($row);
$y = $y + 1;
}
$i = $i + 1;
}
pg_free_result($clone);
pg_close($conn);
$res=$clonearray;
}
break;
case "mssql":
$conn=connectmssqldb($dbname,$host,$port,$uid,$pw);
$res=sqlsrv_query($conn,$query);
if (!$res)
{
die( "Error in statement execution.\n".print_r( sqlsrv_errors(), true));
}else{
$i=0;
$sqlsarray=array();
while( $row = sqlsrv_fetch_array( $res, SQLSRV_FETCH_ASSOC))
{
if($i==0){
$arrk=array_keys($row);
}
$j=0;
while($j<count($arrk)){
$sqlsarray[$i][$arrk[$j]]=$row[$arrk[$j]];
$j++;
}
$i++;
}
$res=$sqlsarray;
}
break;
}
return $res;
}
function local_sqlec($query){
global $db_server;
global $db_db;
global $db_port;
global $db_username;
global $db_password;
return sqlec($query,"mysql",$db_db,$db_server,$db_port,$db_username,$db_password);
}
Which I use like this :
$r=local_sqlec("SELECT test1,test2 FROM test");
$i=0;
echo "last id =".($r[0]['test1'])."<br>";
echo "Row count =".($r[0]['test2'])."<br>";
while($i<$r[0]['test2']){
echo "inserted id =".($r[0]['test1']+$i)."<br>";
$i++;
}

Categories