使用Msqli API连接到数据库

时间:2018-05-07 09:20:50

标签: php database mysqli pdo

帮助我调整此脚本。 连接到数据库是用MySQL编写的,我想,我尝试使用与MySQLi的差异连接,但我似乎在连接后遗漏了代码。任何帮助将受到高度赞赏。 PHP代码是:

//my config
   $servername = "localhost";
   $username = "root";
   $password = "";
   $databasename = "subscribe";


   $conn = new mysqli($servername, $username, $password, $databasename);
   if ($conn->connect_error) {
   die("Connection failed: " . $conn->connect_error);
   }
   echo "done!!";


$subscribe = (isset($_POST['action']) && $_POST['action'] ==    
'unsubscribe')?false:true;

if ($subscribe){
$fields = array(
    array('name' => 'email', 'valid' => array('require', 'email')),
    array('name' => 'name', 'valid' => array('require')),
);
}else{
$fields = array(
    array('name' => 'unsubscribe_email', 'valid' => array('require',  
'email')),
    array('name' => 'confirm', 'valid' => array('require'), 'err_message'
=> 'confirm'),
);
}

上面是我在mysqli中所做的连接。 现在下面的行是脚本的另一部分,我相信我仍然缺少一些可以使它工作的东西。这是我从网上获得的旧订阅脚本。

$error_fields = array();
$get = array();
foreach ($fields AS $field){
$value = isset($_POST[$field['name']])?$_POST[$field['name']]:'';
if (is_array($value)){
    $value = implode('/ ', $value);
}
if (get_magic_quotes_gpc()){
    $value = stripslashes($value);
}
$get[$field['name']] = mysqli_real_escape_string($value);
$is_valid = true;
$err_message = '';
if (!empty($field['valid'])){
    foreach ($field['valid'] AS $valid) {
        switch ($valid) {
            case 'require':
                $is_valid = $is_valid && strlen($value) > 0;
                $err_message = 'Field required';
                break;
            case 'email':
                $is_valid = $is_valid && preg_match("/^[_a-z0-9-]+(\.[_a-
   z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $value);
                $err_message = 'Email required';
                break;
            default:                
                break;
        }
    }
   }
   if (!$is_valid){
    if (!empty($field['err_message'])){
        $err_message = $field['err_message'];
    }
    $error_fields[] = array('name' => $field['name'], 'message' =>    
 $err_message);
 }
}

if (empty($error_fields)){
if ($subscribe){
    $data = array(
        'email' => "'".$get['email']."'",
        'name' => "'".$get['name']."'",
        'date_subscribe' => 'NOW()',
        'status' => "'T'",
    );
    $sql = "REPLACE INTO subscription_form (`".implode("`, `",    
 array_keys($data))."`) VALUES(".implode(", ", array_values($data)).")";
 }else{
    $sql = "UPDATE subscription_form SET date_unsubscribe = NOW(), status
 = 'F' WHERE email = '".$get['unsubscribe_email']."'";
}
if (!empty($sql)){
    $sql_result = mysqli_query ($sql, $connection ) or die ('request  
"Could not execute SQL query" '.$sql);
}
echo (json_encode(array('code' => 'success')));
}else{
echo json_encode(array('code' => 'failed', 'fields' => $error_fields));
}

很抱歉说我有点冲向前,因为我相信我会逐渐到达那里,但我只需要这样就完成了我正在开发的一些pho网站。请帮助我们。我需要的只是一点方向,而我一直在阅读更多内容以提高我在phhp和数据库方面的技能。我只有大约1/4进入php和后端。

2 个答案:

答案 0 :(得分:0)

请检查以下行,您已将连接字符串用作$ connection,它应为$ conn

$sql_result = mysqli_query ($sql, $connection )

所以将上面的句子更改为$ sql_result = mysqli_query($ sql,$ conn) 它应该解决你的问题

答案 1 :(得分:0)

对于代码的第一部分,我认为你必须编写if..else块 如下所示

if ($subscribe === true){
  echo 'In IF BLOCK<br />';
  $fields = array(
  array('name' => 'email', 'valid' => array('require', 'email')),
   array('name' => 'name', 'valid' => array('require')),
 );

 }else if($subscribe === false){
   echo 'In the ELSE BLOCK <br />';
    $fields = array(
         array('name' => 'unsubscribe_email', 'valid' => array('require',  
         'email')),
         array('name' => 'confirm', 'valid' => array('require'), 
            'err_message' => 'confirm'),
           );

 } else{
    echo 'This else block i have written for clearification only';
}

=============================================== =============

IN SHORT ifelse if必须包含已评估的条件

if(condition1_for_evalute){

 } elseif(condition2_for_evalute){

 } else{

 }

第二段代码我没有得到你想要的东西。抱歉,