Jump to content

Fatal error: Uncaught ArgumentCountError: mysqli_query() expects at least 2 arguments, 1 given in on line 19


Table
Go to solution Solved by requinix,

Recommended Posts

Fatal error: Uncaught ArgumentCountError: mysqli_query() expects at least 2 arguments, 1 given in  on line 19

Stuck on this error help please!

<?php
$servername = "localhost";
$username = "root";
$password = "";

// Create connection
$conn = mysqli_connect($servername, $username, $password);


   $alfabet = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',);
         

   for($teller = 0; $teller < count($alfabet); $teller++){
         //Nu laten we mysql tellen hoeveel namen er voorkomen in de database met de mysql-functie COUNT(
   $query = "SELECT COUNT(row1) AS aantal FROM table WHERE row1 LIKE '" . $alfabet[$teller] . "%'";
   


   $query_result = mysqli_query($query);
      //haal de waarden op!
   $row = mysqli_fetch_assoc()($query_result);
          //nu printen we een link als de waarde groter is als 0
       
          if($row["aantal"] > 0 ){
       echo "<a href='" . $_SERVER["PHP_SELF"] . "?letter=" . $alfabet[$teller] . "'>" . $alfabet[$teller] . "</a>";
       } else {
       //alleen de letter
       echo $alfabet[$teller];           }
   }



mysqli_free_result($result);


//Close the MySQL Link
mysqli_close($link);

?>  

 

Link to comment
Share on other sites

11 minutes ago, Table said:

Fatal error: Uncaught ArgumentCountError: mysqli_query() expects at least 2 arguments, 1 given in  on line 19

The error message is telling you that the function requires at least two arguments but you only passed one.

Perhaps the documentation may be of help?

There are other... issues in here, though.

Link to comment
Share on other sites

  • Solution

Here's the code you have:

$query_result = mysqli_query($query);

The "procedural style" mentioned in the docs shows this:

mysqli_query(mysqli $mysql, string $query, int $result_mode = MYSQLI_STORE_RESULT): mysqli_result|bool

You read it like this:

1. This is a function named "mysqli_query"
2. The first parameter is called $mysql - at least in the documentation itself, you can name your variables whatever you want - and it is a mysqli (that's a class) value
3. The second parameter is called $query and is a string value
4. The third parameter is called $result_mode and is an int(eger) value; it has a default value of MYSQLI_STORE_RESULT, which will apply when you do not pass a third argument
5. The function returns a value that is mysqli_result|bool, meaning it will be a mysqli_result (another class) value or a bool(ean) value

The Parameters section gives more details about those three parameters. Same for the Return Values section and the return value.
There's also an Errors/Exceptions that is useful to understand when and why the function may or may not behave correctly.

But back on topic:

The first parameter is not optional - there is no default value, so you must provide one yourself. You have done so with your $query variable; remember, your variable can be named whatever you want.
But there's a problem: the parameter is supposed to be a mysqli value, and you provided a query string.

The second parameter is not optional either, however you are not providing one. That's what PHP is complaining about.

If you combine those two facts together, the problem should be pretty clear: you need to call the function with a mysqli and a query string, but you only called it with a query string.
I'm guessing this was originally based on mysql (no 'i') code? There is a mysql_query function but it isn't the same as the mysqli_query function.

  • Like 1
Link to comment
Share on other sites

Were you able to fix the error?

As requinix described, you are currently using the procedural style for calling mysqli_query(), which requires 2 arguments. You already included the query. Now you need to provide the database connection object, as the first argument. Based on the following line, your connection object is stored in $conn:

$conn = mysqli_connect($servername, $username, $password);

So, your call to mysqli_query() would look like the following:

$query_result = mysqli_query($conn, $query);

 

Link to comment
Share on other sites

1 hour ago, Table said:

Why do you reply in the first case?

The other problems are:

1. Connecting to the database as root and without a password
2. Running 26 separate queries to get row counts for each letter
3. Running the same query multiple times without using a prepared statement
4. The line that calls mysqli_fetch_assoc
5. Using PHP_SELF
6. Trying to navigate from one letter page to another won't work
7. Freeing a result in a variable that doesn't exist
8. Closing a connection in a variable that doesn't exist

I assumed that you would fix your current problem, find some of the others, and probably have more questions to ask. Then I would try to help answer them. But if you'd prefer I don't reply to you then I won't do that anymore.

  • Like 1
Link to comment
Share on other sites

22 hours ago, cyberRobot said:

 

$conn = mysqli_connect($servername, $username, $password);

So, your call to mysqli_query() would look like the following:

$query_result = mysqli_query($conn, $query);

 

Thanks for your reply

I tryed this it gives back this error?

   $query = "SELECT COUNT(row1) AS aantal FROM table WHERE row1 LIKE '" . $alfabet[$teller] . "%'";

Fatal error: Uncaught mysqli_sql_exception: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'table WHERE row1 LIKE 'a%'' at line 1 on line 18

( ! ) mysqli_sql_exception: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'table WHERE row1 LIKE 'a%''

Edited by Table
Link to comment
Share on other sites

 ) Fatal error: Uncaught ArgumentCountError: mysqli_fetch_assoc() expects exactly 1 argument, 0 given in on line 22

( ! ) ArgumentCountError: mysqli_fetch_assoc() expects exactly 1 argument, 0 given in  on line 22

I get this error. ?

   $row = mysqli_fetch_assoc()($query_result);

 

Edited by Table
Link to comment
Share on other sites

On 11/2/2023 at 7:45 PM, requinix said:

The other problems are:

1. Connecting to the database as root and without a password
2. Running 26 separate queries to get row counts for each letter
3. Running the same query multiple times without using a prepared statement
4. The line that calls mysqli_fetch_assoc
5. Using PHP_SELF
6. Trying to navigate from one letter page to another won't work
7. Freeing a result in a variable that doesn't exist
8. Closing a connection in a variable that doesn't exist

I assumed that you would fix your current problem, find some of the others, and probably have more questions to ask. Then I would try to help answer them. But if you'd prefer I don't reply to you then I won't do that anymore.

Anny suggestions would be appreciated.

Link to comment
Share on other sites

On 11/2/2023 at 10:33 AM, Table said:
   $alfabet = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',);

Someone can correct me if I'm wrong, but can't you just create a range of array values like (a,z) instead of writing every alphabetic character

 

could look something like:

$alfabet = range("a","z");

 

Edited by webdeveloper123
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.