php如何获取多个记录并在查询中显示

时间:2015-08-01 06:47:45

标签: php mysql

我有合作伙伴表,我需要通过短信发送回复。所以我的表包含公司名称,城市,移动和密码。如果有人通过短信向我发送密码,我想用2或3条记录(多个)回复客户。使用此代码,我只能发送一条记录。

$sql = "SELECT * FROM reg_dealer WHERE pincode = '$myvalue'";
$q = mysql_query($sql);
$rows= mysql_num_rows($q);
$res= mysql_fetch_array($q);

$firm_name=$res['firm_name'];
$city=$res['city'];
$mobiledealer=$res['mobile'];
$pincode=$res['pincode'];

if($res['pincode']=='')
{
$mobile = $_GET['mobile'];  
$reply_message = "Greeting. We will get back to you soon on the nearest Partner details.";
$reply_message1 = urlencode($reply_message);
mysql_query("INSERT INTO history(sender,message,datetime,datenew,code,status,reply_message,url)VALUES('$mobile','$message','$datetime','$datenew','$number','Dealer Does Not Exist','$reply_message','$s')");//insert data in to table
}
// part 2
else
{
$mobile = $_GET['mobile'];  
$reply_message = "Greetings. Please find the nearest Partner - ".$firm_name.", ".$city.", +".$mobiledealer.".";
$reply_message1 = urlencode($reply_message);
mysql_query("INSERT INTO history(sender,message,datetime,datenew,code,status,reply_message,url)VALUES('$mobile','$message','$datetime','$datenew','$number','Dealer Details Sent','$reply_message','$s')");//insert data in to table
}    

2 个答案:

答案 0 :(得分:1)

使用while循环。

$sql = "SELECT * FROM reg_dealer WHERE pincode = '$myvalue'";
$q = mysql_query($sql);
$rows= mysql_num_rows($q);
while($res= mysql_fetch_array($q))
{

$firm_name=$res['firm_name'];
$city=$res['city'];
$mobiledealer=$res['mobile'];
$pincode=$res['pincode'];

if($res['pincode']=='')
{
$mobile = $_GET['mobile'];  
$reply_message = "Greeting. We will get back to you soon on the nearest Partner details.";
$reply_message1 = urlencode($reply_message);
mysql_query("INSERT INTO history(sender,message,datetime,datenew,code,status,reply_message,url)VALUES('$mobile','$message','$datetime','$datenew','$number','Dealer Does Not Exist','$reply_message','$s')");//insert data in to table
}
// part 2
else
{
$mobile = $_GET['mobile'];  
$reply_message = "Greetings. Please find the nearest Partner - ".$firm_name.", ".$city.", +".$mobiledealer.".";
$reply_message1 = urlencode($reply_message);
mysql_query("INSERT INTO history(sender,message,datetime,datenew,code,status,reply_message,url)VALUES('$mobile','$message','$datetime','$datenew','$number','Dealer Details Sent','$reply_message','$s')");//insert data in to table
}  
}  
  

更新新要求:

$mobile = $_GET['mobile'];
$sql = "SELECT * FROM reg_dealer WHERE pincode = '$myvalue'";
    $q = mysql_query($sql);
    $rows= mysql_num_rows($q);
    while($res= mysql_fetch_array($q))
    {

    $firm_name=$res['firm_name'];
    $city=$res['city'];
    $mobiledealer=$res['mobile'];
    $pincode=$res['pincode'];

    if($res['pincode']=='')
    {
    $reply_message .= "Greeting. We will get back to you soon on the nearest Partner details.";
    mysql_query("INSERT INTO history(sender,message,datetime,datenew,code,status,reply_message,url)VALUES('$mobile','$message','$datetime','$datenew','$number','Dealer Does Not Exist','$reply_message','$s')");//insert data in to table
    }
    // part 2
    else
    {
    $reply_message .= "Greetings. Please find the nearest Partner - ".$firm_name.", ".$city.", +".$mobiledealer.".";
    mysql_query("INSERT INTO history(sender,message,datetime,datenew,code,status,reply_message,url)VALUES('$mobile','$message','$datetime','$datenew','$number','Dealer Details Sent','$reply_message','$s')");//insert data in to table
    }  
    }  
if (isset($reply_message) && $reply_message != '')
{
    $reply_message1 = urlencode($reply_message);

    if($res['pincode']=='')
    {
    mysql_query("INSERT INTO history(sender,message,datetime,datenew,code,status,reply_message,url)VALUES('$mobile','$message','$datetime','$datenew','$number','Dealer Does Not Exist','$reply_message1','$s')");//insert data in to table
    }
    // part 2
    else
    {
    mysql_query("INSERT INTO history(sender,message,datetime,datenew,code,status,reply_message,url)VALUES('$mobile','$message','$datetime','$datenew','$number','Dealer Details Sent','$reply_message1','$s')");//insert data in to table
    } 
}

答案 1 :(得分:0)

您只获取单个记录,因为您没有使用任何类型的循环来获取多个记录。

使用while循环:

while($res= mysql_fetch_array($q))
{
     //put your below inside the loop
}