1

everything functions on the side of data validation and error gathering and such, I get a problem specifically where $r is being executed. I always get the echo error. I've used this same bit of code before, but for some reason isn't working. Just need a second look of eyes at it perhaps.

if (empty($errors)){
    $q = "INSERT INTO testTable (test1, test2) VALUES ('Test', 'Test')";
    $r = @mysqli_query($dbc, $q);

    if($r){
        echo 'Complete!';
    }
    else{ 
        echo 'error';
        }

    mysqli_close($dbc);

    include('footer.html');
    exit();

I can enter the statement manually in MySQL and it will add it to the table, so I don't think its a syntax error there, I am just a little tired at this point.

15
  • try with mysqli_error($dbc) in else part. Commented Apr 13, 2015 at 21:55
  • 1
    Change echo 'error' to echo mysqli_error($dbc); so you see the reason for the error. Commented Apr 13, 2015 at 21:56
  • 3
    Get the real error or die(mysqli_error($dbc)) to mysqli_query() and remove the @ Commented Apr 13, 2015 at 21:56
  • Yeah, it seems strange to ask a question about an error you're getting when you've told it to not report any errors in your function call. Commented Apr 13, 2015 at 21:59
  • I have changed it and now I just get a blank page. No indication of anything. Commented Apr 13, 2015 at 22:03

1 Answer 1

2

"I'm getting a lot of warnings saying that parameter 1 of mysqli_xxx must be mysqli. So does that mean that my $dbc variable isn't working properly?"

The reason for that may very well be because your DB connection method is probably based on mysql_connect() or PDO to connect with.

  • Something you haven't posted in your question.

Those different MySQL APIs do not intermix with each other.

mysql_ + mysqli_ or PDO = no love. mysqli_ + PDO, same thing.

Refer to both manuals:

and use only "one" MySQL API, from beginning to end and not a "mix of".


You should also remove the @ symbol(s) from your code. Those are error suppressors and won't help you when debugging code.


So your connection for MySQLi_ would look something like this:

$dbc = mysqli_connect("myhost","myuser","mypassw","mybd") 
        or die("Error " . mysqli_error($dbc));

Unlike mysql_:

$dbc = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$dbc) {
die('Could not connect: ' . mysql_error());
}
Sign up to request clarification or add additional context in comments.

5 Comments

My $dbc looks exactly like your mysqli_connect example. But I looked back up higher and saw that my mysqli_select_db() had the parameters in the wrong order. I had no idea that mysqli and mysql were different. Thank you so much.
@Burninrock24 Ah, glad to know this one was solved, cheers and you're welcome.
@Burninrock24 You can use all 4 parameters in mysqli_connect() rather than using mysqli_select_db(). Saves you a bit of code too.
Thanks again, what happened is that I was mixing and matching elements from different scripts that I had completed independent of each other, not knowing that there were compatibility issues between them. Thank you again!
@Burninrock24 It happens and you're welcome. The old mysql_ method of connecting, required the connection variable being last, as opposed to mysqli_ being the first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.