Hi,
I am generating an email and adding information from database, In the coding below, I am getting the records Id form one page to another with the help of array…and I am generating a sql query to get all data from database…I am stuck here:
if(!empty($_POST['check1']))
{
$result = array();
for($i=0; $i<sizeof($_POST['check1']); $i++)
{
$result[$i] = $_POST['check1'][$i];
}
//print "$i<br>";
//print "$result[0] $result[1] $result[2] $result[3] $result[4]";
$sql="SELECT * FROM `tblbusinessinfo` WHERE `biz_id` = '$result[0]' or `biz_id` = '$result[1]' or `biz_id` = '$result[2]' or `biz_id` = '$result[3]'"
$result=mysql_query($sql);
$total_records=mysql_num_rows($result);
In the above code, I don’t know how to get it in one variable… or 4 variables…I want to print that variable into my mail message…
Please help…
Well it depends on how you want to access the data.
$data = mysql_fetch_array($result,MYSQL_ASSOC);
Will put the data in an associative array, so you can access it as $data[‘fieldname’].
$data = mysql_fetch_row($result);
Will retrieve the data as an enumerated array and throw away the fieldnames, so $data[0] will be your first field, $data[1] will be the second, etc.
You probably want the first one, for sake of ease in your coding.
Side note: I really dont advise you reuse $result in the way that you have… the whole top half of the code is unneeded because you can just put $_POST[‘check1’][0] in your $sql= line. The below will achieve the same that your script does:
$sql="SELECT * FROM `tblbusinessinfo` WHERE `biz_id` = '".$_POST['check1'][0]."' or `biz_id` = '".$_POST['check1'][1]."' or `biz_id` = '".$_POST['check1'][2]."' or `biz_id` = '".$_POST['check1'][3]."'"
$result=mysql_query($sql);
$total_records=mysql_num_rows($result);
$data = mysql_fetch_array($result,MYSQL_ASSOC);
Actually, I suggest you keep the “top half”, with the exception that you add validation and sanitization to it before using the user supplied values in the query.