0

I am trying this piece of code for hours. I am not able to figure out what my error is.

$conn = mysqli_connect('localhost', 'user', 'pass', 'mydb')
            or die( "Unable to connect");

$check = mysqli_query($conn, "select * from Users where Fuid='$fbid'",
            MYSQLI_STORE_RESULT);

$rows = mysqli_num_rows($check);


if (empty($rows)) { // if new user . Insert a new record        
    $query = "INSERT INTO Users (Fuid,Funame,Ffname,Femail)
            VALUES ('$fbid','$funame','$ffname','$femail')";

    mysqli_query($conn, $query);    
}

There might be a very small error, but I am just fried up.

user3344070
  • 15
  • 1
  • 7
  • 2
    `mysqli_query($conn,$query);` You reversed the parameters the second time. – bansi Jun 04 '14 at 05:06
  • 1
    remove the or die statement. Add this on a new line after the connection string. If the line fires, fix the connection error. Otherwise, let me know that know error was generated. if (mysqli_connect_errno()) { echo '

    Cannot connect to DB: ' . mysqli_connect_error() . '

    ';
    – Len_D Jun 04 '14 at 05:10
  • Accidentally posted before I finished that thought :-) See me updated response – Len_D Jun 04 '14 at 05:12
  • @bansi, if you don't mind your users seeing an error page that looks like the site is broken, nothing – serakfalcon Jun 04 '14 at 05:14
  • @Len_D That if statement shows no error. – user3344070 Jun 04 '14 at 05:18
  • add this on a line right after the connect statement: mysqli_select_db($conn, "dbname"); where dbname is your actual dbname – Len_D Jun 04 '14 at 05:21
  • @Len_D The database name is already passed as the fourth argument to `mysqli_connect` – Phil Jun 04 '14 at 05:30
  • I know. Humor me and try it anyway. – Len_D Jun 04 '14 at 05:31

3 Answers3

4

This seems to do a lot of things the hard way. Try this instead...

// Set MySQLi to throw exceptions when it encounters an error
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
 
$conn = new mysqli('localhost', 'user', 'pass', 'mydb');
$conn->set_charset('utf8mb4'); // or whatever is appropriate

$check = $conn->prepare('SELECT 1 FROM `Users` WHERE Fuid = ?');
$check->bind_param('i', $fbid); // assuming $fbid is an integer
$check->execute();
if (!$check->fetch()) {
    $sql  = 'INSERT INTO `Users` (Fuid, Funame, Ffname, Femail) VALUES (?, ?, ?, ?)';
    $stmt = $conn->prepare($sql);
    $stmt->bind_param('isss', $fbid, $funame, $ffname, $femail);
    $stmt->execute();
}
Phil
  • 157,677
  • 23
  • 242
  • 245
  • What you mean by `the hard way`? isn't this the right way! – bansi Jun 04 '14 at 05:30
  • i am sure OP of question won't understand this :D as OP doesnt using prepaired statements – NullPoiиteя Jun 04 '14 at 05:30
  • @bansi *"the hard way"* meaning using a `SELECT *` query to simply check existence and executing code without any error checking – Phil Jun 04 '14 at 05:32
  • @NullPoiиteя that's why I keep posting answers like this – Phil Jun 04 '14 at 05:33
  • @YourCommonSense I appreciate the edit for brevity but I stand by my use of exceptions. You simply can't control user errors in the same way – Phil Jun 04 '14 at 05:34
  • Surely you have objections which you can express with words of language – Your Common Sense Jun 04 '14 at 05:35
  • 1
    There is no such thing like "your" use of exceptions. there is just use of exceptions. All you need is to use them in a little smarter way, making your code readable. – Your Common Sense Jun 04 '14 at 05:36
  • `Fatal error: Uncaught exception 'mysqli_sql_exception' with message 'Column 'Fuid' cannot be null' in /home/anaad6c3/public_html/demo/functions.php:14 Stack trace: #0 /home/anaad6c3/public_html/demo/functions.php(14): mysqli_stmt->execute() #1 /home/anaad6c3/public_html/demo/fbconfig.php(3): require('/home/anaad6c3/...') #2 /home/anaad6c3/public_html/demo/index.php(2): require('/home/anaad6c3/...') #3 {main} thrown in /home/anaad6c3/public_html/demo/functions.php on line 14` – user3344070 Jun 04 '14 at 05:37
  • 1
    @user3344070 this error message means $fbid variable is not set. – Your Common Sense Jun 04 '14 at 05:44
  • @YourCommonSense I meant the use of them (exceptions) in my answer. – Phil Jun 04 '14 at 05:45
  • Which assumes I didn't use them in mine. This assumption is obviously wrong. Don't take me for a regular participant of this unlucky tag. I do no harmful edits. – Your Common Sense Jun 04 '14 at 05:46
  • @YourCommonSense `Fatal error: Call to a member function prepare() on a non-object in /home/anaad6c3/public_html/demo/functions.php on line 8` – user3344070 Jun 04 '14 at 05:52
  • @YourCommonSense I'm well aware of your contributions. What I wasn't aware of is how `mysqli_report` works with `MYSQLI_REPORT_STRICT`. I assumed it merely triggered `E_WARNING` level errors. Thank you – Phil Jun 04 '14 at 05:53
  • @user3344070 That doesn't make any sense. If `$conn` was a *non-object*, it would have triggered an error for `$conn->set_charset()`. – Phil Jun 04 '14 at 05:59
  • @user3344070 this error is impossible for this code. You have to try is exactly as is and then, if worked, split it to separate files or whatever you want with it. – Your Common Sense Jun 04 '14 at 06:00
  • 1
    @Phil surely it throws an exception for the connect as well. So, it should have been thrown even earlier – Your Common Sense Jun 04 '14 at 06:01
  • @YourCommonSense I wasn't sure about that as the PHP manual examples for `mysqli_report` still check `connect_errno`. Tested locally and yes, it *should* throw an exception for a bad connection. – Phil Jun 04 '14 at 06:04
  • 1
    @Dharman done but feel free to make the edits yourself whenever you want. The author can always roll it back if they don't agree. – Phil Jan 19 '21 at 23:33
0

Use

mysqli_query($conn, $query); 

instead of

 mysqli_query($query, $conn); 
Deepu Sasidharan
  • 5,193
  • 10
  • 40
  • 97
-2

You reversed the parameters!

mysqli_query($query, $conn);

Should be

mysqli_query($conn, $query);  

Try this: Use mysqli_connect_errno instead of die

$conn = mysqli_connect('localhost', 'user', 'pass', 'mydb');
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}    

$check = mysqli_query($conn, "select * from Users where Fuid='$fbid'",
            MYSQLI_STORE_RESULT);

$rows = mysqli_num_rows($check);


if (empty($rows)) { // if new user . Insert a new record        
    $query = "INSERT INTO Users (Fuid,Funame,Ffname,Femail)
            VALUES ('$fbid','$funame','$ffname','$femail')";

    mysqli_query($conn,$query);    
}
bansi
  • 55,591
  • 6
  • 41
  • 52
  • I used that in previous query `$check = mysqli_query($conn, "select * from Users where Fuid='$fbid'", MYSQLI_STORE_RESULT);` Shows error in this too – user3344070 Jun 04 '14 at 05:11