mysqli_error() expects exactly 1 parameter, 0 given

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
jaad
Forum Commoner
Posts: 95
Joined: Fri Jan 03, 2014 5:30 am
Location: Vancouver Canada

mysqli_error() expects exactly 1 parameter, 0 given

Post by jaad »

Hi,

I found a really great book and following a tutorial and receiving a warning:
Warning: mysqli_error() expects exactly 1 parameter, 0 given in C:\xampp\htdocs\aliens_abduction\report.php on line 29
here is my code:

Code: Select all

<?php
$host 			= "localhost";
$user 			= "root";
$pass 			= "1122334";
$database		= "aliendatabase";

$db	= mysqli_connect($host, $user, $pass, $database) or die("you did not connect bozo");

//Abduction_report engine.
$first_name			= $_POST['first_name'];
$last_name			= $_POST['last_name'];
$when_it_happened	= $_POST['when_it_happened'];
$how_long			= $_POST['how_long'];
$how_many			= $_POST['how_many'];
$alien_description	= $_POST['alien_description'];
$what_they_did		= $_POST['what_they_did'];
$fang_spotted		= $_POST['fang_spotted'];
$other				= $_POST['other'];
$email				= $_POST['email'];


$query = "INSERT INTO aliens_abduction (first_name, last_name, when_it_happened, how_long, " .
    "how_many, alien_description, what_they_did, fang_spotted, other, email) " .
    "VALUES ('$first_name', '$last_name', '$when_it_happened', '$how_long', '$how_many', " .
    "'$alien_description', '$what_they_did', '$fang_spotted', '$other', '$email')";


$result = mysqli_query($db, $query)
or die(mysqli_error());----------------------------------------this is line 29
mysqli_close($db);

echo 'Thanks for submitting the form.<br />';
  echo 'You were abducted ' . $when_it_happened;
  echo ' and were gone for ' . $how_long . '<br />';
  echo 'Number of aliens: ' . $how_many . '<br />';
  echo 'Describe them: ' . $alien_description . '<br />';
  echo 'The aliens did this: ' . $what_they_did . '<br />';
  echo 'Was Fang there? ' . $fang_spotted . '<br />';
  echo 'Other comments: ' . $other . '<br />';
  echo 'Your email address is ' . $email;

?>
I've gone back and forth in it and just can't find why that is...
I also done a var_dump($query) right after query and all looks normal, all the values are there. connection to DB works fine too no error there. I know it's something stupid, i just can't see it lol. I know, I know.... I will get better glasses soon.
User avatar
jaad
Forum Commoner
Posts: 95
Joined: Fri Jan 03, 2014 5:30 am
Location: Vancouver Canada

Re: mysqli_error() expects exactly 1 parameter, 0 given

Post by jaad »

ok I solved my problem. one field was misspelled in my database.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: mysqli_error() expects exactly 1 parameter, 0 given

Post by requinix »

The other problem is that you weren't usng mysqli_error() properly.
User avatar
jaad
Forum Commoner
Posts: 95
Joined: Fri Jan 03, 2014 5:30 am
Location: Vancouver Canada

Re: mysqli_error() expects exactly 1 parameter, 0 given

Post by jaad »

PHP.net is useless for newbies like me. Only seasoned coding veterans can make sense of this manual. Newbies like me would have an equal or better chances of understanding this manual if it was written in Japanese or in another foreign language I know nothing about. I been to this site a million times.... I never found what I was looking for or ever understood any of the example they give. This site is only good for people who have advance knowledge of php.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: mysqli_error() expects exactly 1 parameter, 0 given

Post by requinix »

I could also point you to a dictionary if you don't know what a "parameter" is?
Warning: mysqli_error() expects exactly 1 parameter, 0 given in C:\xampp\htdocs\aliens_abduction\report.php on line 29

Code: Select all

or die(mysqli_error());----------------------------------------this is line 29
Zero parameters. You didn't pass anything to the function. Meanwhile the manual says
Procedural style

string mysqli_error ( mysqli $link )
And then below,
Parameters

link
Procedural style only: A link identifier returned by mysqli_connect() or mysqli_init()
mysqli_connect? That sounds familiar...

Code: Select all

$db     = mysqli_connect($host, $user, $pass, $database) or die("you did not connect bozo");
So you should be passing that value to the mysqli_error function.

Code: Select all

or die(mysqli_error($db));----------------------------------------this is line 29
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: mysqli_error() expects exactly 1 parameter, 0 given

Post by Christopher »

jaad wrote:PHP.net is useless for newbies like me. Only seasoned coding veterans can make sense of this manual. Newbies like me would have an equal or better chances of understanding this manual if it was written in Japanese or in another foreign language I know nothing about. I been to this site a million times.... I never found what I was looking for or ever understood any of the example they give. This site is only good for people who have advance knowledge of php.
I know you have said this in a couple posts. As requinix pointed out, the docs and error messages are giving you very specific information. But you need to learn a few terms and be a little more open to tracking things down. As requinix shows, the error message tells you the exact line and function where the problem is (no parameter), and docs tell you exactly what kind of parameter is needed. Just keep at it.
(#10850)
User avatar
jaad
Forum Commoner
Posts: 95
Joined: Fri Jan 03, 2014 5:30 am
Location: Vancouver Canada

Re: mysqli_error() expects exactly 1 parameter, 0 given

Post by jaad »

for anyone who understands the language it's easy, and straight forward.

in all the books and all the tutorial I ever watched ->I have never seen a set of codes like this:
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}

if (!mysqli_query($link, "SET a=1")) {
printf("Errormessage: %s\n", mysqli_error($link));
}

/* close connection */
mysqli_close($link);
?>
for a newbie, this is what my interpretation of it is:

1- a new connection to the database is being established ($link = mysqli_connect("localhost", "my_user", "my_password", "world");)

2-check the connection -> (and return a number if the connection hasn't been made)

3- This is where it makes no sense to me (if (!mysqli_query($link, "SET a=1")) { printf("Errormessage: %s\n", mysqli_error($link));)

3.1 - if there is no query being made with the new connection that you tried to do on line -1, do a query anyway and SET the field (a) in the database to 1. if you have any problem with that query, return an error message on the line underneath where the code is supposed to be executed.

so from what I can see a connection has been made since no error is showing for the connection itself. so the error has nothing to do with the connection. from what I can see in this example it's the line where it say !mysqli_query.... why don't you write it without the "!" first and try to get your 'a' value set to 1? How do I know there is no 'a' field in the database in there example? How come the example is not showing 'a' as `a` instead? maybe that would work if they identify the field correctly? And I never seen a query being written in this fashion before, I mean the syntax itself. And the error points to a system error???? now I am thinking that the software I am using is having a problem.... it is because my php ini isn't set properly?

So as you can see... I make no sense right? and you would be right about that because I can tell I make no sense myself because I don't understand the example to start with. PHP.net, everything they use for example makes no sense to me. Only really hard core people who have been coding their whole life would have come up with codes like this at one point or another in their life and make half sense of what this is supposed to mean.

What is an array()
php.net definition: "An array in PHP is actually an ordered map." A map? it's not a map! As far as I know, an array is a list of strings or numbers or other items grouped together and defined by a variable name.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: mysqli_error() expects exactly 1 parameter, 0 given

Post by Celauran »

Code: Select all

if (!mysqli_query(whatever))
is shorthand for

Code: Select all

if (mysqli_query(whatever) == false)
It may not be a best practice, but it is a common one. Moreover, while it may not have been intuitively obvious to you, now you know and will remember.
User avatar
jaad
Forum Commoner
Posts: 95
Joined: Fri Jan 03, 2014 5:30 am
Location: Vancouver Canada

Re: mysqli_error() expects exactly 1 parameter, 0 given

Post by jaad »

lol yes I will remember. thank you.
Post Reply