Note that there are some explanatory texts on larger screens.

plurals
  1. POTesting if a Select Query found results
    primarykey
    data
    text
    <p>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 <code>$result</code> </p> <p>I've checked the query variables and they echo out without a problem</p> <pre><code>@ $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-&gt;query($query); if($result === FALSE){ $insertquery = "INSERT INTO ('oauth_provider', 'oauth_uid', 'username') VALUES ('facebook', '".$uid."', '".$username."')"; $result = $con-&gt;query($query); } </code></pre> <p>I should probably add I have the code working using the older mysql approach. But I have read it is better to use the object-oriented mysqli approach.</p> <p>Here's the old working code</p> <pre><code>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); } } </code></pre> <p>Any help you can give me is appreciated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload