odbc_connect not working on unixODBC (LINUX) - php

I’ve included this code in my functions.php of current active theme and works perfectly (i.e: I upload the .mdb file on the server first and then read the tables from the file) on WINDOWS PLATFORM. It’s just as easy as we’re using MySQL DB.
$dbName = $uploadfile;
$conn = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=$dbName", "", "");
if($conn != null){
//echo "";exit;
// reading all the tables (data) from mdb file uploaded
$cdc_trans = odbc_exec($conn, "SELECT * FROM axs_web_cdc_trans");
$dealers = odbc_exec($conn, "SELECT * FROM axs_web_dealers");
$web_item = odbc_exec($conn, "SELECT * FROM axs_web_item");
$parties = odbc_exec($conn, "SELECT * FROM axs_web_parties");
$trans = odbc_exec($conn, "SELECT * FROM axs_web_trans");
$voch = odbc_exec($conn, "SELECT * FROM axs_web_voch");
// insert for axs_web_cdc_trans
while($row = odbc_fetch_array($cdc_trans)){
//break;
$cdc_trans_date = $row['CDC_TRANS_DATE'];
$agent_code = $row['AGENT_CODE'];
$party_user_code = $row['PARTY_USER_CODE'];
$nature = $row['NATURE'];
$quantity = $row['CDC_QTY'];
$item = $row['TRANS_ITEM_SYMBOL'];
$us_qty = $row['UNSETTLED_QTY'];
$sql = "INSERT INTO axs_web_cdc_trans (CDC_TRANS_DATE,AGENT_CODE,PARTY_USER_CODE, NATURE, CDC_QTY,TRANS_ITEM_SYMBOL,UNSETTLED_QTY) VALUES ('$cdc_trans_date', '$agent_code', '$party_user_code', '$nature', '$quantity', '$item', '$us_qty')";
mysql_query($sql) or die("Error: ".mysql_error());
}
// insert for axs_web_dealers
while($row = odbc_fetch_array($dealers)){
//break;
$dealer_code = $row['DEALER_CODE'];
$dealer_name = $row['DEALER_NAME'];
$cnic = $row['DEALER_NIC_NO'];
$dealer_add = $row['DEALER_ADDRESS'];
$dealer_cell = $row['DEALER_MOBILE_NO'];
$dealer_email = $row['DEALER_EMAIL'];
$check_user = "SELECT * FROM axs_web_dealers WHERE DEALER_CODE = $dealer_code";
$chk_res = mysql_query($check_user) or die("Error: ".mysql_error());
if(mysql_fetch_array($chk_res) > 0)
continue;
else{
$sql = "INSERT INTO axs_web_dealers (DEALER_CODE, DEALER_NAME, DEALER_NIC_NO, DEALER_ADDRESS, DEALER_MOBILE_NO, DEALER_EMAIL) VALUES ('$dealer_code', '$dealer_name', '$cnic', '$dealer_add', '$dealer_cell', '$dealer_email')";
mysql_query($sql) or die("Error: ".mysql_error());
}
}
// insert for axs_web_item
while($row = odbc_fetch_array($web_item)){
//break;
$symbol = $row['ITEM_SYMBOL'];
$sym_name = addslashes($row['ITEM_NAME']);
$rate = $row['ITEM_RATE'];
$high_rate = $row['ITEM_HIGH_RATE'];
$low_rate = $row['ITEM_LOW_RATE'];
$vol_qty = $row['ITEM_VOL_QTY'];
$cdc = $row['CDC'];
$sql = "INSERT INTO axs_web_item (ITEM_SYMBOL,ITEM_NAME,ITEM_RATE, ITEM_HIGH_RATE, ITEM_LOW_RATE,ITEM_VOL_QTY,CDC) VALUES ('$symbol', '$sym_name', '$rate', '$high_rate', '$low_rate', '$vol_qty', '$cdc')";
mysql_query($sql) or die("Error: ".mysql_error());
}
// insert for axs_web_parties
while($row = odbc_fetch_array($parties)){
//break;
$party_user_code = $row['PARTY_USER_CODE'];//continue;
$party_name = $row['PARTY_NAME'];
$agent_code = $row['AGENT_CODE'];
$party_email = $row['PARTY_EMAIL'];
$party_add = addslashes($row['PARTY_ADDRESS']);
$party_cell = $row['PARTY_MOBILE_NO'];
$party_cnic = $row['PARTY_NIC_NO'];
$check_user = "SELECT * FROM axs_web_parties WHERE PARTY_USER_CODE = $party_user_code";
$chk_res = mysql_query($check_user) or die("Error: ".mysql_error());
if(mysql_fetch_array($chk_res) > 0)
continue;
else{
$sql = "INSERT INTO axs_web_parties (PARTY_USER_CODE, PARTY_NAME, AGENT_CODE, PARTY_EMAIL, PARTY_ADDRESS, PARTY_MOBILE_NO, PARTY_NIC_NO) VALUES ('$party_user_code', '$party_name', '$agent_code', '$party_email', '$party_add', '$party_cell', '$party_cnic')";
mysql_query($sql) or die("Error: ".mysql_error());
}
}
// insert for axs_web_trans
while($row = odbc_fetch_array($trans)){
//break;
$agent_code = $row['AGENT_CODE'];
$party_usr_code = $row['PARTY_USER_CODE'];
$trade_date = $row['TRADE_DATE'];
$nature = $row['NATURE'];
$trans_qty= $row['TRANS_QTY'];
$trans_rate = $row['TRANS_RATE'];
$trans_item_symbol = $row['TRANS_ITEM_SYMBOL'];
$set_desc = $row['SET_DESC'];
$comm = $row['COMM'];
$trans_amt = $row['TRANS_AMT'];
$trans_posted = $row['TRANS_POSTED'];
$trade_desc = $row['TRADE_DESC'];
$comm_amt = $row['COMM_AMT'];
$cvt_wht_amt = $row['CVT_WHT_AMT'];
$fed_amt = $row['FED_AMT'];
$other_chrg_amt = $row['OTHER_CHRG_AMT'];
$sql = "INSERT INTO axs_web_trans (AGENT_CODE, PARTY_USER_CODE, TRADE_DATE, NATURE, TRANS_QTY, TRANS_RATE, TRANS_ITEM_SYMBOL, SET_DESC, COMM, TRANS_AMT, TRANS_POSTED, TRADE_DESC, COMM_AMT, CVT_WHT_AMT, FED_AMT, OTHER_CHRG_AMT) VALUES ('$agent_code', '$party_user_code', '$trade_date', '$nature', '$trans_qty', '$trans_rate', '$trans_item_symbol', '$set_desc', '$comm', '$trans_amt', '$trans_posted', '$trade_desc', '$comm_amt', '$cvt_wht_amt', '$fed_amt', '$other_chrg_amt')";
mysql_query($sql) or die("Error: ".mysql_error());
}
// insert for axs_web_voch
while($row = odbc_fetch_array($voch)){
//break;
$voch_no = $row['VOCH_NO'];
$voch_date = $row['VOCH_DATE'];
$agent_code = $row['AGENT_CODE'];
$party_user_code = $row['PARTY_USER_CODE'];
$narration= $row['NARRATION'];
$dr_amt = $row['DR_AMOUNT'];
$cr_amt = $row['CR_AMOUNT'];
$bal = $row['BALANCE'];
$index_no = $row['INDX_NO'];
$check_no = $row['CHEQUE_NO'];
$effect_code = $row['EFFECT_CODE'];
$sql = "INSERT INTO axs_web_voch (VOCH_NO, VOCH_DATE, AGENT_CODE, PARTY_USER_CODE, NARRATION, DR_AMOUNT, CR_AMOUNT, BALANCE, INDX_NO, CHEQUE_NO, EFFECT_CODE)
VALUES ('$voch_no', '$voch_date', '$agent_code', '$party_user_code', '$narration', '$dr_amt', '$cr_amt', '$bal', '$index_no', '$check_no', '$effect_code')";//exit;
mysql_query($sql) or die("Error: ".mysql_error());
}
}
}
?>
<form action="" method="post"
enctype="multipart/form-data">
<label for="file" style="margin-left: 18px;">Filename:</label>
<input type="file" name="file" id="file">
<br>
<input style="margin-left: 78px;" type="submit" name="submit" value="Uplaod DB">
</form>
<?php
}
?>
PHP INFO on WINDOWS:
https://www.dropbox.com/s/4qoqxu87ffwym84/PHP%20INFO%20on%20WINDOWS.png
The same code is not working on when I go to Linux ... odbc_connect() retuns NULL.
PHP INFO on LINUX:
https://www.dropbox.com/s/a3vo6pd5pn1h4ha/PHP%20INFO%20on%20Linux.png

I may be asking the obvious, but do you have a ODBC driver for MS Access installed on the Linux instance?

Related

Multiple Query Inside While Loop PHP

I try to run multiple queries inside while loop, and nothing happens, not showing error and not showing success. anyone can explain or help me solve this?
here's my code
<?php
include('config.php');
$sql = 'SELECT * FROM inventory_masuk_dummy';
$query = mysqli_query($conn, $sql);
while($row = mysqli_fetch_array($query)) {
$jumlah = $row['jml'];
$size = $row['size'];
$nama_barang = $row['nama_barang'];
$status = $row['status'];
$sql2 = "UPDATE inventory_dummy SET jumlah = jumlah+$jumlah WHERE nama_barang = '$nama_barang' AND size = '$size'";
$sql3 = "UPDATE inventory_masuk_dummy SET status = '2' WHERE status='1'";
while($row = mysqli_fetch_array($sql2) && $row = mysqli_fetch_array($sql3)) {
echo "Berhasil di ubah, anda akan segera dialihkan";
}
}
echo "Nothing...";
?>
The problem is your code does not execute the $sql2 and $sql3, you can execute it again with mysqli_query
<?php
include('config.php');
$sql = 'SELECT * FROM inventory_masuk_dummy';
$query = mysqli_query($conn, $sql);
while($row = mysqli_fetch_array($query)) {
$jumlah = $row['jml'];
$size = $row['size'];
$nama_barang = $row['nama_barang'];
$status = $row['status'];
$sql2 = "UPDATE inventory_dummy SET jumlah = jumlah+$jumlah WHERE nama_barang = '$nama_barang' AND size = '$size'";
$sql3 = "UPDATE inventory_masuk_dummy SET status = '2' WHERE status='1'";
if ( mysqli_query($conn, $sql2) && mysqli_query($conn, $sql3) ) {
echo "Berhasil di ubah, anda akan segera dialihkan";
}
}
echo "Nothing...";

How to draw an image into QR code with PHP

In my project I am trying to generate a QR code with an image in it based on stored blobs (1 Qr code + 1 logo) in my SQL table.
I have thw following code but can't seem to store the new image into my table.
Any help is appreciated.
<?php
//for connection
$sName = "localhost";
$sUser = "Username";
$sPass = "Password";
$sDb = "Database";
$Conn = new mysqli ($sName, $sUser, $sPass, $sDb);
//variables i pass from another app
$Email = mysqli_real_escape_string($Conn, $_POST["PassEmail"]);
$qContent = addslashes(file_get_contents($_FILES["file"]["tmp_name"]));
$qOverlay = addslashes (file_get_contents($_FILES["overlay"]["tmp_name"]));
$Sql = "SELECT * FROM `userprofile_login` INNER JOIN `userprofile_personalprofile` ON userprofile_login.userid = userprofile_personalprofile.userid WHERE userprofile_login.email = '".$Email."' ";
$Result = mysqli_query($Conn, $Sql);
if (mysqli_num_rows ($Result) > 0) {
$Row = mysqli_fetch_assoc ($Result);
$qCheckSql = "SELECT `userid` FROM `userprofile_qr` WHERE userid = '".$Row['userid']."' ";
$qCheckResult = mysqli_query($Conn, $qCheckSql);
$Row2 = mysqli_fetch_assoc($qCheckResult);
if (mysqli_num_rows ($qCheckResult) < 1) {
//if there 0 result from previous query, insert new data
$InsertSql = "INSERT INTO `userprofile_qr` (`userid`, `qrcode`, `overlay`)
VALUES ('".$Row['userid']."', '$qContent', '$qOverlay')";
$InsertResult = mysqli_query($Conn, $InsertSql);
} else {
//is there ia a result from the previous query, update data
$UpdateSql = "UPDATE `userprofile_qr` SET
`qrcode` = '$qContent',
`overlay` = '$qOverlay'
WHERE userid = '".$Row2['userid']."' ";
$UpdateResult = mysqli_query($Conn, $UpdateSql);
}
The first part works, the problem is the bottom part where I want to combine the QR code and the logo:
$qSql = "SELECT `qrcode`, `overlay` FROM `userprofile_qr` WHERE userid = '".$Row2['userid']."' ";
$qSelect = mysqli_query($Conn, $qSql);
include 'phpqrcode/qrlib.php';
$qRow = mysqli_fetch_assoc($qSelect);
$tImage = "<img src='data:image/png;base64,".base64_encode($qRow['qrcode'])."'/>";
$tOverlay = "<img src='data:image/png;base64,".base64_encode($qRow['overlay'])."'/>";
header("Content-type: image/png");
$aQr = imagecreatefromjpeg($tImage);
$QrWidth = imagesx($aQr);
$QrHeight = imagesy($aQr);
$aOverlay = imagecreatefrompng($tOverlay);
$OverlayWidth = imagesx($aOverlay);
$OverlayHeight = imagesy($aOverlay);
$OverlayQrWidth = $QrWidth / 3;
$OverlayQrHeight = $OverlayHeight / ($OverlayWidth / $OverlayQrWidth);
imagecopyresampled($aQr, $aOverlay, $OverlayQrWidth, $OverlayQrHeight, 0, 0, $OverlayQrWidth, $OverlayQrHeight, $OverlayWidth, $OverlayHeight);
imagepng($aQr);
$oImage = "<img src='data:image/png;base64,".base64_encode($aQr)."'/>";
$UpdateCombineSql = "UPDATE `userprofile_qr` SET
`combine` = '$oImage'
WHERE userid = '".$Row2['userid']."' ";
$CombineResult = mysqli_query($Conn, $UpdateCombineSql);
echo "Success";
}
?>

Comparison for in SQL row selection not working?

This is the code that is not working:
$query = "SELECT * FROM $table WHERE text_id > '$last_id'"; //SELECT NEW MESSAGES
$result = mysqli_query($connection,$query);
if ($result && mysqli_num_rows($result) > 0)
{
//THIS SHOULD NOT BE RUNNING
}
I've verified over and over in phpMyAdmin and the text_id in the table and $last_id are both the integer value '1'. That being said, the condition equates to true every time the code runs.
Am I messing this code up, or is my thinking improper?
Here is entire script:
<?php
session_start();
$alias = $_SESSION['username'];
$host = 'localhost';
$user = '*';
$pass = '*';
$database = 'vethergen_db_accounts';
$table = 'table_messages';
$last_id_table = 'table_chat_sync';
$connection = mysqli_connect($host, $user, $pass) or die ("Unable to connect!");
mysqli_select_db($connection,$database) or die ("Unable to select database!");
$last_id_query = "SELECT alias FROM $last_id_table WHERE alias = '$alias'";
$last_id_result = mysqli_query($connection,$last_id_query);
$last_id_rows = mysqli_fetch_array($last_id_result);
if ($last_id_rows['alias'] === $alias)
{
$last_id = $last_id_rows['last_id'];
$query = "SELECT * FROM $table WHERE text_id > '$last_id'"; //SELECT NEW MESSAGES
$result = mysqli_query($connection,$query);
if ($result && mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
if ($row['alias'] === "Vether")
{
echo '<p id = "chat_text">'.'<b>'.$row['alias'].'</b>'.': '.$row['text']."</p>";
echo '<p id = "time_stamp">'.$row['time'].'</p>';
echo '<p id = "chat_number">'.$row['text_id'].'</p>';
}
else
{
echo '<p id = "chat_text">'.'<b class = "bold_green">'.$row['alias'].'</b>'.': '.$row['text']."</p>";
echo '<p id = "time_stamp">'.$row['time'].'</p>';
echo '<p id = "chat_number">'.$row['text_id'].'</p>';
}
echo '<hr class = "chat_line"></hr>';
$last_row_id = $row['text_id'];
}
}
//UPDATE LAST SYNC ID
$update_query = "UPDATE $last_id_table SET last_id = '$last_row_id' WHERE alias = '$alias'";
mysqli_query($connection,$update_query);
}
else
{
$update_query = "INSERT INTO $last_id_table (alias, last_id) VALUES('$alias','-1')";
mysqli_query($connection,$update_query);
}
?>
You should change ;
WHERE text_id > '$last_id'
to
WHERE text_id > $last_id
text_id column is integer and can't be compared like string.

Error: You have an error in your SQL syntax near '' at line 1

I keep getting this error...but yet I cannot see any syntax that is inappropriate... any ideas?
Here's my PHP code. I know my other pages are correct as I can run all other parts of the code with no issue.
<?php
// this connects To database
$hostname="";
$username="";
$password="";
$dbname="";
mysql_connect($hostname,$username,$password) OR DIE ("Connection Failed");
mysql_select_db($dbname);
$action = $_REQUEST["action"];
if ($action == 'a') {
$custFirst = null;
$custLast = null;
$custAddress = null;
$custCity = null;
$custState = null;
$custZip = null;
$custEmail = null;
$custPhone = null;
} else {
$id = $_REQUEST["id"];
$query = "select * from custTab where custNo = $id";
$result = mysql_query($query)
or die(mysql_error());
$row = mysql_fetch_array($result);
$custFirst = $row['custFirst'];
$custLast = $row['custLast'];
$custAddress = $row['custAddress'];
$custCity = $row['custCity'];
$custState = $row['custState'];
$custZip = $row['custZip'];
$custEmail = $row['custEmail'];
$custPhone = $row['custPhone'];
} // end if
?>
Try putting quotes around the $id,
$query = "select * from custTab where custNo = '$id'";
This is dangerous and wrong depending on what the custNo field contains:
$id = $_REQUEST["id"];
$query = "select * from custTab where custNo = $id";
If the id is an integer, you should use:
$id = (int) $_REQUEST["id"];
$query = "select * from custTab where custNo = $id";
Otherwise you would have to quote it and escape the variable:
$id = mysql_real_escape_string($_REQUEST["id"]);
$query = "select * from custTab where custNo = '$id'";
But you really should switch to PDO / mysqli and prepared statements to avoid this problem altogether.

how can i save the file after SELECT ... INTO OUTFILE 'result.csv'

i use this query to dump table into csv file :
$sql = "SELECT *
INTO OUTFILE 'result.csv'
FIELDS TERMINATED BY ';'
FROM tableName";
i get a result.csv file in the db folder of mysql
how can i save it at root of my site ?
In place of result.csv provide the path where you want it to be saved:
$sql = "SELECT *
INTO OUTFILE '/path/to/your/site/result.csv'
FIELDS TERMINATED BY ';'
FROM tableName";
this code can export mysql table into csv file , i've tested it with large table
<?php
// tested with success
$db_name = "db_name";
$db_password = "pass";
$db_link = mysql_connect("localhost", "root", $db_password);
mysql_select_db($db_name, $db_link);
mysql_query("SET NAMES UTF8");
$table = "table_name";
function assoc_query_2D($sql, $id_name = false){
$result = mysql_query($sql);
$arr = array();
$row = array();
if($result){
if($id_name == false){
while($row = mysql_fetch_assoc($result))
$arr[] = $row;
}else{
while($row = mysql_fetch_assoc($result)){
$id = $row['id'];
$arr[$id] = $row;
}
}
}else
return 0;
return $arr;
}
function query_whole_table($table, $value = '*'){
$sql = "SELECT $value FROM $table";
return assoc_query_2D($sql);
}
$export_str = "";
$result = query_whole_table($table);
foreach($result as $record){
$export_str .= implode(";",$record) . "\n";
}
// add time to fileName
$date = time();
// output the file
// we can set a header to send it directly to the browser
file_put_contents($date.'_'.$table."_export.csv", $export_str);
?>

Categories