How to get magento store? - php

I want to check the store type in Magento and run query if the store EN else another query , I have my code below but it doesn't work:
$name = $store->getName();
$connection = Mage::getSingleton('core/resource')->getConnection('core_read');
if($name=='EN')
{
$sql= "SELECT * FROM directory_country_region_EN WHERE name='$region' ";
}
else
{
$sql = "SELECT * FROM directory_country_region_SU WHERE name='$region' "; }

Try this :
$name = Mage::app()->getStore()->getCode();
$connection = Mage::getSingleton('core/resource')->getConnection('core_read');
if($name=='EN')
{
$sql= "SELECT * FROM directory_country_region_EN WHERE name='$region' ";
}
else
{
$sql = "SELECT * FROM directory_country_region_SU WHERE name='$region' "; }

Related

I want to assign query to $query variable according to the conditon mentioned in if block but it is giving me an error that null is provided

$query = NULL;
if($formtype == "patient"){
$query = mysqli_query($conn,"SELECT * FROM patient where email='$id'")or die(mysqli_error());
}
else if($formtype == "donor"){
$query = mysqli_query($conn,"SELECT * FROM donor where email='$id'")or die(mysqli_error());
}
$row = mysqli_fetch_array($query);
Here is the error:

perform two computation in two column and the answer will be save in another column

How can I perform a autocompute in my database ex. the value of Stock and Quantity(Quantity-Stock) the answer will be save in CarryO column
create.php
<?php
require_once 'dbconfig.php';
$con = mysql_connect("localhost","root","");
if($con)
{
mysql_select_db("testproduct",$con);
}
if($_POST)
{
$sql = mysql_query("SELECT * FROM tblproduct WHERE id = '".$_POST['pid']."'");
$prod = mysql_fetch_array($sql);
$pname = $prod['name'];
$actualprice = $prod['actualprice'];
$sellprice = $prod['sellprice'];
$stock = $prod['Stock'];
$gname = $_POST['gname'];
$saledate = $_POST['saledate'];
$quantity = $_POST['quantity'];
$profit = $_POST['profit'];
$carryO = $_POST['carryO'];
$sells = $_POST['sells'];
$expense = $_POST['expense'];
try{
$stmt = $db_con->prepare("INSERT INTO tblsales(pname,gname,saledate,quantity,actualprice,sellprice,carryO,sells,expense,profit,stock)
VALUES(:upname,:ugname,:usaledate,:uquantity,:uactualprice,:usellprice,:ucarryO,:usells,:uexpense,:uprofit,:ustock)");
$stmt->bindParam(":upname", $pname);
$stmt->bindParam(":ugname", $gname);
$stmt->bindParam(":usaledate", $saledate);
$stmt->bindParam(":uquantity", $quantity);
$stmt->bindParam(":uactualprice", $actualprice);
$stmt->bindParam(":usellprice", $sellprice);
$stmt->bindParam(":ucarryO", $carryO);
$stmt->bindParam(":usells", $sells);
$stmt->bindParam(":uexpense", $expense);
$stmt->bindParam(":uprofit", $profit);
$stmt->bindParam(":ustock", $stock);
if($stmt->execute())
{
echo "Successfully Added";
}
else{
echo "Query Problem";
}
}
catch(PDOException $e){
echo $e->getMessage();
}
}
?>
thanks for your help just new in php and please let me know if I can use your code or its only a example
Change this part:
$prod = mysql_query("SELECT * FROM tblproduct WHERE id = ".$_POST['pid']);
echo $prod;
$pname = [$prod['name']];
Into:
$sql = mysql_query("SELECT * FROM tblproduct WHERE id = '".$_POST['pid']."'");
$prod = mysql_fetch_array($sql);
$pname = $prod['name'];
You may want to try this.
$prod = mysql_query("SELECT * FROM tblproduct WHERE id = ".$_POST['pid'],$db_con); //$db_con must be your database connection
if(!$prod) { die("Database query failed: " . mysql_error()); } //always check if your query is properly done.
$pname = "";
while ($row = mysql_fetch_array($prod)) {
$pname = $row["name"]; }
also if you are fetching only one column which is the name then be specific to your query for fastest result. e.g. "SELECT name FROM tblproduct WHERE id = ".$_POST['pid']

function to check if data is on the database

I create a function to find all firstname and lastname in my database all I want if that data is already exist I just want to output, error message
my question is how to create a function to check if data is already exist?
this is my function to find all data of firstname and lastname.
function find_student_by_firstname($firstname){
global $con;
$safe_firstname = prep($firstname);
$sql = "SELECT * ";
$sql .= "FROM studeprofile ";
$sql .= "WHERE FirstName = '{$safe_firstname}' ";
$sql .= "LIMIT 1";
$student_set = mysqli_query($con, $sql);
confirm_query($student_set);
if($student = mysqli_fetch_assoc($student_set)){
return $student;
} else {
return null;
}
}
function find_student_by_lastname($lastname){
global $con;
$safe_lastname = prep($lastname);
$sql = "SELECT * ";
$sql .= "FROM studeprofile ";
$sql .= "WHERE LastName = '{$safe_lastname}' ";
$sql .= "LIMIT 1";
$student_set = mysqli_query($con, $sql);
confirm_query($student_set);
if($student = mysqli_fetch_assoc($student_set)){
return $student;
} else {
return null;
}
}
this is my current function to check if data is already exist.
function match_fistname_lastname($lastname, $firstname){
$student_firstname = find_student_by_firstname($lastname);
if($student_firstname){
find_student_by_lastname($lastname);
} else {
return false;
}
}
If you mean by "data is already exist" that a person is in the database that matches to firstname and lastname, you don't have to execute two queries.
Use the and in mysql like this:
function find_student($firstname, $lastname){
global $con;
$safe_firstname = prep($firstname);
$safe_lastname = prep($lastname);
$sql = "SELECT * ";
$sql .= "FROM studeprofile ";
$sql .= "WHERE FirstName = '{$safe_firstname}' and LastName = '{$safe_lastname}' ";
$sql .= "LIMIT 1";
$student_set = mysqli_query($con, $sql);
confirm_query($student_set);
if($student = mysqli_fetch_assoc($student_set)){
return $student;
} else {
return null;
}
}

PHP convert mysql_connection to PDO

I am trying to convert code that uses mysql_connect to pdo, however I do not know what to do after this. should put in place "mysql_result".
$query = "select * from tbl_usuarios where login = '$cliente_username' and senha = '$cliente_password'";
$result = $conexao->query($query);
$number = $result->fetch();
if ($number==0) { ?><script>alert('Dados incorretos! Tente novamente.');</script>
<?php
exit;
} else {
$_SESSION['usuario_id'] = mysql_result($result,0,'id');
$_SESSION['usuario_nome'] = mysql_result($result,0,'nome');
?><script>document.location = '../principal.php'</script><?php
}
mysql_close($conexao);
}
?>
mysql_result($result,0,'id')
becomes
$number->id
and
mysql_result($result,0,'nome')
becomes
$number->nome
maybe you should add a limit 1 to you query:
$query = "select * from tbl_usuarios where login = '$cliente_username' and senha = '$cliente_password' limit 1";

How do I modify this MySQL INSERT Query for multiple rows?

I am attempting to insert records for Artists, Songs and Record Labels, whilst checking that the data does not already exist in the Database.
The following code is from Mike Fenwick.
<?php
$query = "SELECT id FROM table WHERE unique1=value1 AND unique2=value2…";
$select_result = mysql_query($query);
if (!mysql_num_rows($select_result)) {
$query = "INSERT INTO table SET unique1=value1 AND unique2=value2…";
$insert_result = mysql_query($query);
$id = mysql_insert_id();
}
else {
$row = mysql_fetch_assoc($select_result);
$id = $row['id'];
}
return $id;
?>
I need to modify this to check if three unique values exist already (from 3 separate tables), and if not, insert them. Here is my attempt:
<?php
$query = "SELECT id FROM artistsTable WHERE artistName='Beyonce'";
$select_result = mysql_query($query);
if (!mysql_num_rows($select_result)) {
$query = "INSERT INTO table SET artistName='Beyonce' AND artistImage='beyonce.jpg'";
$insert_result = mysql_query($query);
$artistID = mysql_insert_id();
}
else {
$row = mysql_fetch_assoc($select_result);
$artistID = $row['artistID'];
}
return $artistID;
$query = "SELECT id FROM recordLabelTable WHERE labelName='Columbia Records'";
$select_result = mysql_query($query);
if (!mysql_num_rows($select_result)) {
$query = "INSERT INTO table SET labelName='Columbia Records'";
$insert_result = mysql_query($query);
$labelID = mysql_insert_id();
}
else {
$row = mysql_fetch_assoc($select_result);
$labelID = $row['labelID'];
}
return $labelID;
$query = "SELECT id FROM songTable WHERE trackTitle='Crazy in Love' AND artistID=".$artistID." AND labelID=".$labelID."";
$select_result = mysql_query($query);
if (!mysql_num_rows($select_result)) {
$query = "INSERT INTO songTable SET trackTitle='Crazy in Love' AND artistID=".$artistID." AND labelID=".$labelID."";
$insert_result = mysql_query($query);
$songID = mysql_insert_id();
}
else {
$row = mysql_fetch_assoc($select_result);
$songID = $row['songID'];
}
return $songID;
?>
I'm assuming that there must be a more efficient way to do this. Any ideas would be much appreciated.
Using basic inset / ignore syntax you could do something like this.
A couple of inserts to put in the artist details and label details, then an INSERT based on a SELECT:-
<?php
$query = "INSERT IGNORE INTO artistTable (artistName, artistImag) VALUES('Beyonce', 'beyonce.jpg')";
$insert_result = mysql_query($query);
$query = "INSERT IGNORE INTO labelTable (labelName) VALUES('Columbia Records')";
$insert_result = mysql_query($query);
$query = "INSERT IGNORE INTO songTable (trackTitle, artistID, labelID)
SELECT 'Crazy in Love', a.artistID, b.labelID
FROM artistTable a
INNER JOIN labelTable b
ON a.artistName = 'Beyonce'
AND a.artistImag = 'beyonce.jpg'
AND b.labelName = 'Columbia Records'";
$insert_result = mysql_query($query);
$songID = mysql_insert_id();
return $songID;
?>
As #LoganWayne says, you probably should use MySQLi .
<?php
/* ESTABLISH CONNECTION */
$con=mysqli_connect("Host","Username","Password","Database"); /* REPLACE NECESSARY DATA */
if(mysqli_connect_errno()){
echo "Error".mysqli_connect_error();
}
/* FOR artistsTable TABLE */
$query = "SELECT id FROM artistsTable WHERE artistName='Beyonce'";
$select_result = mysqli_query($con,$query); /* EXECUTE QUERY */
if (mysqli_num_rows($select_result)==0) { /* IF QUERY'S RESULT IS 0 */
$query = "INSERT INTO table SET artistName='Beyonce' AND artistImage='beyonce.jpg'";
$insert_result = mysqli_query($con,$query); /* EXECUTE INSERT QUERY */
} /* END OF IF */
else {
while($row = mysqli_fetch_array($select_result)){
$artistID = mysqli_real_escape_string($con,$row['artistID']); /* ESCAPE STRING */
} /* END OF WHILE LOOP */
} /* END OF ELSE */
/* FOR recordLabelTable TABLE */
$query = "SELECT id FROM recordLabelTable WHERE labelName='Columbia Records'";
$select_result = mysqli_query($con,$query); /* EXECUTE SELECT QUERY */
if (mysqli_num_rows($select_result)==0) { /* IF QUERY'S RESULT IS 0 */
$query = "INSERT INTO table SET labelName='Columbia Records'";
$insert_result = mysqli_query($con,$query); /* EXECUTE INSERT QUERY */
}
else {
while($row = mysqli_fetch_array($select_result)){
$labelID = mysqli_real_escape_string($con,$row['labelID']); /* ESCAPE STRING */
}
}
/* FOR songtable TABLE */
$query = "SELECT id FROM songTable WHERE trackTitle='Crazy in Love' AND artistID='$artistID' AND labelID='$labelID'";
$select_result = mysqli_query($con,$query); /* EXECUTE SELECT QUERY */
if (mysqli_num_rows($select_result)==0) {
$query = "INSERT INTO songTable SET trackTitle='Crazy in Love' AND artistID='$artistID' AND labelID='$labelID'";
$insert_result = mysqli_query($con,$query); /* EXECUTE QUERY */
} /* END OF IF */
else {
while($row = mysqli_fetch_array($select_result)){
$songID = mysqli_real_escape_string($con,$row['songID']);
} /* END OF WHILE LOOP */
}
?>
SUMMARY:
Used at least MySQLi instead of deprecated MySQL.
Replaced fetch_assoc() function with fetch_array() function.
Used mysqli_real_escape_string() function to prevent some of SQL injections.
Cleaned your code. You have misplaced apostrophes(') and double quotes(") hanging around.

Categories