Jump to content

Help with databse and if statement


confused_aswell

Recommended Posts

Hi

 

I am wanting to know how one assigns a string to a particular column in a database? For example, if a field in the address column is blank (ie no entry) how would you assign a string to that column and field?

 

I am trying to practice with if statements and just want to know to do this. Any help or pointers would be appreciated.

 

Thanks,

 

Phil

 

SELECT name, address, city FROM users;

 

 

if(address == "NULL") {

echo "address";

}else {

echo "city";

}

Link to comment
https://forums.phpfreaks.com/topic/127043-help-with-databse-and-if-statement/
Share on other sites

Im not sure if this will work, but i would of went for somthing like this

not sure if you need $post on the varibles or not

<?php 
SELECT * FROM table_name;

$adress = ['collum_name']
$city = ['collum_name']  


if(address == "NULL") {
echo "$address";
}else {
echo "$city";
}

?>


<?php

//First check to see if col is empty
$res = mysql_query("SELECT column FROM testtable WHERE id = 1") or die(mysql_error());
$e = mysql_fetch_row($res);
if($e[0] == NULL || $e[0] == ""){
$update = mysql_query("UPDATE testtable SET column = 'Example' where id = 1") or die(mysql_error());
}

?>

Hi

 

Here is the script I am working on, I need to be able to write a if statement so that if entry_suburb is empty then it would be filled with the entry_city field. I was trying to use another example to learn what to do, but I think if might need more information than that.

 

Would appreciate any help please,

 

Phil

 

PS the script works fine if you take the if statement out

 

<?php$link = mysql_connect($host, $user, $pass) or die("Can not connect." . mysql_error());
mysql_select_db($db) or die("Can not connect.");

$replace = array(
'address_book_id' => '',
'customers_name' => 'Customer Job',
'entry_firstname' => 'First Name',
'entry_lastname' => 'Last Name',
'entry_street_address' => 'Billing Address1',
'entry_suburb' => 'Billing Address2',
'entry_city' => 'Billing Address3',
'entry_state' => 'Billing Address4',
'entry_postcode' => 'Billing Address5',
'customers_email_address' => 'Email',
'customers_telephone' => 'Phone'

);
function split_num($num){ 
    $num = substr($num, 0, 5) . ' ' . substr($num, 5); 
    return $num; 
} 



$values = mysql_query("SELECT zen_orders.customers_name, zen_address_book.entry_firstname, zen_address_book.entry_lastname, zen_address_book.entry_street_address, zen_address_book.entry_suburb, zen_address_book.entry_city, zen_address_book.entry_state, zen_address_book.entry_postcode, zen_orders.customers_telephone, zen_orders.customers_email_address
FROM (zen_address_book INNER JOIN zen_orders ON zen_address_book.customers_id = zen_orders.customers_id) INNER JOIN zen_customers ON (zen_address_book.address_book_id = zen_customers.customers_default_address_id) AND (zen_address_book.customers_id = zen_customers.customers_id)


");
$field = entry_suburb;
if($field == "NULL") {
echo "entry_suburb";
}else {
echo "entry_city";
}

$i=0;
while ($rowr = mysql_fetch_assoc($values)) { 
    if(!preg_match('#\x20#', $rowr['customers_telephone'], $match)){ // does not find a space... 
       $rowr['customers_telephone'] = split_num($rowr['customers_telephone']); 
    } 
    if($i==0) { 
        foreach(array_keys($rowr) as $title) 
        $csv_output .= '"'.str_replace(array_keys($replace), $replace, $title).'",'; 
        $csv_output .= "\n"; 
    } 

    foreach ($rowr as $key => $value) { 
        $csv_output .= '"'.$value.'",'; 

    } 
    $csv_output .= "\n"; 
    $i++; 
} 


$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
print $csv_output;
exit;

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.