添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
$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 );
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 mysql i _query function. 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);
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.
   $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%'' "table" is a mysql reserved word and therefore a poor choice of name. If you must used a reserved word as an identifier for a table or column then you have to enclose it in backticks...
SELECT COUNT(row1) AS aantal FROM `table` WHERE ...
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.