TheStudent2023 Posted April 28, 2023 Share Posted April 28, 2023 (edited) Php Gurus, Why I get this error: Fatal error: Uncaught ArgumentCountError: The number of elements in the type definition string must match the number of bind variables in C:\wamp64\www\Work\buzz\Templates\short.php on line 96 ( ! ) ArgumentCountError: The number of elements in the type definition string must match the number of bind variables in C:\wamp64\www\Work\buzz\Templates\short.php on line 96 Call Stack # Time Memory Function Location 1 0.0022 361808 {main}( ) ...\short.php:0 2 0.0095 440672 mysqli_stmt_bind_param( $statement = class mysqli_stmt { public $affected_rows = NULL; public $insert_id = NULL; public $num_rows = NULL; public $param_count = NULL; public $field_count = NULL; public $errno = NULL; public $error = NULL; public $error_list = NULL; public $sqlstate = NULL; public $id = NULL }, $types = 'ssssssssssssssssssss', $vars = [0 => 'mobile', 1 => 'mobile', 2 => 'mobile', 3 => 'mobile', 4 => 'mobile', 5 => 'mobile', 6 => 'mobile', 7 => 'mobile', 8 => 'mobile', 9 => 'mobile', 10 => 'mobile', 11 => 'mobile', 12 => 'mobile', 13 => 'mobile', 14 => 'mobile', 15 => 'mobile', 16 => 'mobile', 17 => 'mobile', 18 => 'mobile', 19 => 'mobile'] ) ...\short.php:96 Here is my code: <?php //Report Error. ini_set('display_errors',1); ini_set('display_startup_errors',1); error_reporting(E_ALL); //Valid tbls & Columns. $tbls = [ 'links_crawls_drummin' => ['id' => "ID" ,'date_and_time' => "Date and Time" ,'domain' => "Domain" ,'domain_point' => "Domain Point" ,'url' => "URL" ,'url_point' => "Url Point" ,'title' => "Title" ,'title_point' => "Title Point" ,'header' => "Header" ,'header_point' => "Header Point" ,'kw_1' => "Keyword 1" ,'kw_1_point' => "Keyword 1 Point" ,'kw_2' => "Keyword 2" ,'kw_2_point' => "Keyword 2 Point" ,'kw_3' => "Keyword 3" ,'kw_3_point' => "Keyword 3 Point" ,'kw_4' => "Keyword 4" ,'kw_4_point' => "Keyword 4 Point" ,'description' => "Description" ,'description_point' => "Description Point" ], 'links_submits_drummin' => ['id' => "ID" ,'date_and_time' => "Date and Time" ,'domain' => "Domain" ,'domain_point' => "Domain Point" ,'url' => "URL" ,'url_point' => "Url Point" ,'title' => "Title" ,'title_point' => "Title Point" ,'header' => "Header" ,'header_point' => "Header Point" ,'kw_1' => "Keyword 1" ,'kw_1_point' => "Keyword 1 Point" ,'kw_2' => "Keyword 2" ,'kw_2_point' => "Keyword 2 Point" ,'kw_3' => "Keyword 3" ,'kw_3_point' => "Keyword 3 Point" ,'kw_4' => "Keyword 4" ,'kw_4_point' => "Keyword 4 Point" ,'description' => "Description" ,'description_point' => "Description Point" ] ]; //Extract $_GETs. $tbl = !EMPTY($_POST['tbl'])?$_POST['tbl']:(!EMPTY($_GET['tbl'])?$_GET['tbl']:'links_crawls_drummin'); $lmt = !EMPTY($_POST['lmt'])?$_POST['lmt']:(!EMPTY($_GET['lmt'])?$_GET['lmt']:1); $match = !EMPTY($_POST['mtch'])?$_POST['mtch']:(!EMPTY($_GET['mtch'])?$_GET['mtch']:'fuzzy'); $srch = !EMPTY($_POST['srch'])?$_POST['srch']:(!EMPTY($_GET['srch'])?$_GET['srch']:'mobile'); $page = !EMPTY($_GET['pg'])?intval($_GET['pg']):1; $limit = !EMPTY($_GET['lmt'])?intval($_GET['lmt']):1; $offset = ($page*$limit)-$limit; $display_headings = array_values($tbls[$tbl]); $cols = array_keys($tbls[$tbl]); $tbl_cols_no = count($cols); echo '<br>'; $comparators = array(); $comparator = ($match == "exact" ? "=" : "LIKE"); $sql = "SELECT * from $tbl WHERE $cols[0]"." $comparator "."?"; $CharTypes = 's'; $SearchValues[] = $srch; for($i=1;$i!==$tbl_cols_no;$i++) { $sql .= " OR $cols[$i]"." $comparator "."?"; $CharTypes .= 's'; $SearchValues[] = $srch; } $sql .= " ORDER BY id DESC LIMIT $lmt OFFSET $offset"; mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT); $conn = mysqli_connect("localhost","root","","buzz"); //mysqli_connect("server","user","password","db"); mysqli_set_charset($conn,'utf8mb4'); $stmt = mysqli_stmt_init($conn); mysqli_stmt_prepare($stmt,$sql); mysqli_stmt_bind_param($stmt,$CharTypes,$SearchValues); mysqli_stmt_execute($stmt); $result = mysqli_stmt_get_result($stmt); while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) { echo "<pre>"; print_r($row); echo "</pre>";; } Issue: For some reason, php is not accepting $SearchValues Note 1: $CharTypes = ssssssssssssssssssss. (20 's'). Note 2: $SearchValues = $srch,$srch,$srch,$srch,$srch,$srch,$srch,$srch,$srch,$srch,$srch,$srch,$srch,$srch,$srch,$srch,$srch,$srch,$srch,$srch. (20 $srch). Hence, The number of elements in the type definition string does match number of bind variables. Therefore, I should get no errors! Now, if you say that, I cannot have it like this: mysqli_stmt_bind_param($stmt,$CharTypes,$SearchValues); And must have it like this: $srch,$srch,$srch,$srch,$srch,$srch,$srch,$srch,$srch,$srch,$srch,$srch,$srch,$srch,$srch,$srch,$srch,$srch,$srch,$srch Then know this that, the following works, if I switch it to OOP: $query = $conn->prepare($sql); $query->bind_param($CharTypes, ...$SearchValues); $query->execute(); $result = $query->get_result(); while($row = $result->fetch_assoc()){ echo "<pre>"; print_r($row); echo "</pre>"; } NOTE: It is: I get no errors and get echoed the search result:Array ( [id] => 3 [date_and_time] => 2023-04-24 00:14:35 [domain] => [kw_1_point] => 5 [kw_2] => tutorial [kw_2_point] => 5 [kw_3] => apps [kw_3_point] => 5 [kw_4] => usa [kw_4_point] => 5 [header] => 0 [kw_1] => mobile [header_point] => 0 [title] => 0 [title_point] => 0 [domain_point] => 0 => mobilephones.com [url_point] => 0 [description] => [description_point] => 0 ) Edited April 28, 2023 by TheStudent2023 Link to comment https://forums.phpfreaks.com/topic/316217-why-i-get-this-unnecssary-fatal-error/ Share on other sites More sharing options...
ginerjm Posted April 28, 2023 Share Posted April 28, 2023 All you have to do is show us LINE 96. Don't know about all that other stuff but since the error tells you the line you should show US that line. Link to comment https://forums.phpfreaks.com/topic/316217-why-i-get-this-unnecssary-fatal-error/#findComment-1607799 Share on other sites More sharing options...
TheStudent2023 Posted April 28, 2023 Author Share Posted April 28, 2023 (edited) 6 minutes ago, ginerjm said: All you have to do is show us LINE 96. Don't know about all that other stuff but since the error tells you the line you should show US that line. LINE 96: mysqli_stmt_bind_param($stmt,$CharTypes,$SearchValues); Edited April 28, 2023 by TheStudent2023 Link to comment https://forums.phpfreaks.com/topic/316217-why-i-get-this-unnecssary-fatal-error/#findComment-1607801 Share on other sites More sharing options...
ginerjm Posted April 28, 2023 Share Posted April 28, 2023 Before that line add an echo of the contents of your query that you are binding these values to and see if they match up Link to comment https://forums.phpfreaks.com/topic/316217-why-i-get-this-unnecssary-fatal-error/#findComment-1607802 Share on other sites More sharing options...
Solution Barand Posted April 28, 2023 Solution Share Posted April 28, 2023 Since you found that this... $query->bind_param($CharTypes, ...$SearchValues); works, why are you using 6 minutes ago, TheStudent2023 said: mysqli_stmt_bind_param($stmt,$CharTypes,$SearchValues); without the "..." to expand the array into a comma separated list? Link to comment https://forums.phpfreaks.com/topic/316217-why-i-get-this-unnecssary-fatal-error/#findComment-1607803 Share on other sites More sharing options...
TheStudent2023 Posted April 28, 2023 Author Share Posted April 28, 2023 (edited) 6 minutes ago, Barand said: Since you found that this... $query->bind_param($CharTypes, ...$SearchValues); works, why are you using without the "..." to expand the array into a comma separated list? You say, this would work then ? ```` mysqli_stmt_bind_param($stmt,$CharTypes, ...$SearchValues); ```` Edited April 28, 2023 by TheStudent2023 Link to comment https://forums.phpfreaks.com/topic/316217-why-i-get-this-unnecssary-fatal-error/#findComment-1607805 Share on other sites More sharing options...
TheStudent2023 Posted April 28, 2023 Author Share Posted April 28, 2023 @barand Thanks, I getting no errors now! Cheers! Link to comment https://forums.phpfreaks.com/topic/316217-why-i-get-this-unnecssary-fatal-error/#findComment-1607806 Share on other sites More sharing options...
TheStudent2023 Posted April 28, 2023 Author Share Posted April 28, 2023 @ginerjm Barand saved the night! Link to comment https://forums.phpfreaks.com/topic/316217-why-i-get-this-unnecssary-fatal-error/#findComment-1607807 Share on other sites More sharing options...
Barand Posted April 28, 2023 Share Posted April 28, 2023 Easier with PDO $stmt = $pdo->prepare($sql); $stmt->execute($SearchValues); 2 Link to comment https://forums.phpfreaks.com/topic/316217-why-i-get-this-unnecssary-fatal-error/#findComment-1607808 Share on other sites More sharing options...
TheStudent2023 Posted April 30, 2023 Author Share Posted April 30, 2023 On 4/29/2023 at 3:20 AM, Barand said: Easier with PDO $stmt = $pdo->prepare($sql); $stmt->execute($SearchValues); I do not know pdo but still thanks. Link to comment https://forums.phpfreaks.com/topic/316217-why-i-get-this-unnecssary-fatal-error/#findComment-1607826 Share on other sites More sharing options...
TheStudent2023 Posted April 30, 2023 Author Share Posted April 30, 2023 @requinix Issue solved. Do close this thread. Thanks! Link to comment https://forums.phpfreaks.com/topic/316217-why-i-get-this-unnecssary-fatal-error/#findComment-1607827 Share on other sites More sharing options...
Recommended Posts