Select Sorgu sonucu bulundu Testi ise

0 Cevap php

Hi I'm new to PHP and would really appreciate if someone could tell me where I'm going wrong in this code. To put the code in context, I'm using the facebook to authenticate users and adding their signup details to my app database. In the following code intend to check if the user already exists in the database and if not add them. For some reason I can't test the $result

Ben sorgu değişkenleri kontrol ettik ve onlar bir sorun olmadan echo

@ $con = new mysqli('localhost', 'username', 'password', 'database');
if(mysqli_connect_errno()){
    echo 'Error: Could not connect to the database. Please try again later';
    exit;
}

$query = "SELECT * FROM users WHERE oauth_provider = 'facebook' AND oauth_uid ='".$uid."'";

$result = $con->query($query);


if($result === FALSE){
    $insertquery = "INSERT INTO ('oauth_provider', 'oauth_uid', 'username') VALUES ('facebook', '".$uid."', '".$username."')";
    $result = $con->query($query);
}

Ben muhtemelen kod eski mysql yaklaşım kullanarak çalışma var eklemek gerekir. Ama nesne yönelimli mysqli yaklaşımı kullanmak daha iyidir okudum.

İşte eski işçi kod

if($session){
$con = mysql_connect('localhost', 'user', 'password');
$select_db = mysql_select_db('database');

if(!$con || !$select_db){
    die('Could not connect: ' . mysql_error());
}
else{
    echo "connected to database and table selected";
}

$query = mysql_query("SELECT * FROM users WHERE oauth_provider = 'facebook' AND oauth_uid = ". $user['id']);
$result = mysql_fetch_array($query);

if(empty($result)){
    $query = mysql_query("INSERT INTO users (oauth_provider, oauth_uid, username) VALUES ('facebook', {$user['id']}, '{$user['name']}')");
    $query = mysql_query("SELECT * FROM users WHERE id = " . mysql_insert_id());
    $result = mysql_fetch_array($query);
}

}

Bana verebileceğiniz herhangi bir yardım takdir edilmektedir.

0 Cevap