/    Sign up×
Community /Pin to ProfileBookmark

Notice: Object of class mysqli_result could not be converted to int

Php Folks,

What is wrong with this line ?

[code]
$total_pages = ceil($result_row_count/$result_per_page);
[/code]

I get error:
**Notice: Object of class mysqli_result could not be converted to int in C:xampphtdocspower.pagepagination_test_SIMPLE.php on line …**

You see, I building a pagination page.
Look how I am doing it ….

[code]
<?php
error_reporting(E_ALL);
?>

<!DOCTYPE HTML”>
<html>

<head>
<meta name=”viewport” content=”width-device=width, initial-scale=1″>
</head>
<body>

<?php

if(!session_id())
{
session_start();
$_SESSION[‘form_step’] = ‘start’;
}

if($_SESSION[‘form_step’] != ‘end’);
{
if(!isset($_GET[‘form_type’]) && empty($_GET[‘form_type’]))
{
die(“Invalid Form!”);
}
else
{
$_SESSION[‘form_type’] = $_GET[‘form_type’];

if(!isset($_GET[‘query_type’]) && empty($_GET[‘query_type’]))
{
die(“Invalid Query!”);
}
else
{
$_SESSION[‘query_type’] = $_GET[‘query_type’];

if(!function_exists($_SESSION[‘form_type’]))
{
die(“Invalid Form!”);
}
else
{
$_SESSION[‘form_type’]();
}
}
}


function search()
{
?>
<form action=”<?php echo $_SERVER[‘PHP_SELF’];?>?form_type=<?php echo $_SESSION[‘form_type’];?>&query_type=<?php echo $_SESSION[‘query_type’];?>&form_step=end&page_limit=1&page=1″ method=’post’ enctype=’plain/text’>
<?php

//Added ‘*’ (asterisk) to indicate the ‘Text Field’ is a ‘required’ one.
echo “<label for=”first_name”>First Name *:</label>
<input type=”text” name=”first_name” placeholder=”First Name” value = “”>”;?>
<br>
<?php
echo “<label for=”marital_status”>Marital Status *:</label>”;
echo “<select name=”marital_status”>”;
echo “<option value=”single”>Single</option>”;
echo “<option value=”married”>Married</option>”;
echo “</select>”;
echo “<br>”;
?>
<input type=”submit” name=”search” value=”Search”>
<?php
//$current_function = __FUNCTION__;
//echo $current_function;

if($_SERVER[‘REQUEST_METHOD’] === ‘POST’)
{
if(isset($_POST[‘search’]))// && $_SESSION[‘form_step’] != ‘end’)
{
//Connect to Database. (DB_SERVER, BD_USERNAME, DB_PASSWORD, DB_NAME).
$conn = mysqli_connect(“localhost”,”root”,””,”powerpage”);
$conn->set_charset(‘utf8mb4’); //Always set Charset.

if($conn === false)
{
die(“ERROR: Connection Error!. ” . mysqli_connect_error());
}

$form_step = $_GET[‘form_step’];

$page_number = $_GET[‘page’];
$result_per_page = $_GET[‘page_limit’];
$row_start = (($page_number * $result_per_page) – $result_per_page); //Offset (Row Number that ‘Starts’ on page).
$row_end = ($page_number * $result_per_page); //Max Result (Row Number that ‘Ends’ on page).
$previous_page = $page_number-1;
$next_page = $page_number+1;

echo “Row Start: $row_start”;
echo “Row End: $row_end”;

$sql_query = “SELECT COUNT(*) FROM users WHERE first_name = ? AND marital_status = ?”;
$stmt = mysqli_stmt_init($conn);
mysqli_stmt_prepare($stmt,$sql_query);
mysqli_stmt_bind_param($stmt,”ss”,$_POST[“first_name”],$_POST[“marital_status”]);

//Attempt to Execute the Prepared Statement.
mysqli_stmt_execute($stmt);
if(!mysqli_stmt_execute($stmt))
{
//Close Connection.
mysqli_close($conn);
die(“<pre>Statement Execution Failed!n”.mysqli_stmt_error($stmt).”nmysqli_stmt_bind_param</pre>”);
}
else
{
echo “<pre>1. Statement Execution Success!</pre>”; //DELETE THIS LINE
$result_row_count = mysqli_stmt_get_result($stmt);
if(mysqli_fetch_array($result_row_count, MYSQLI_NUM)[0])
{
$sql_query = “SELECT COUNT(*) FROM users WHERE first_name = ? AND marital_status = ?”;
$stmt = mysqli_stmt_init($conn);
mysqli_stmt_prepare($stmt,$sql_query);
mysqli_stmt_bind_param($stmt,”ss”,$_POST[“$place_holder_1”],$_POST[“$place_holder_2”]);
}
else
{
die(“No results!”);
}
}
//Grab total number of pages to paginate.
$total_pages = ceil($result_row_count/$result_per_page);

echo “TOTAL PAGES: $total_pages<br>”;

while($row = mysqli_fetch_array($result_row_count,MYSQLI_ASSOC)) //Use this if you use ‘$result = mysqli_stmt_get_result($stmt)’ instead of ‘mysqli_stmt_bind_result($stmt,$email)’.
{
//Retrieve Values.
$first_name = $row[“first_name”];//Use this if you use ‘$result = mysqli_stmt_get_result($stmt)’ instead of //’mysqli_stmt_bind_result($stmt,$email_count)’;

echo “First Name: $first_name<br>”;
echo “Middle Name: $middle_name<br>”;
echo “Surname: $surname<br>”;
echo “Gender: $gender<br>”;
echo “Marital Status: $marital_status<br>”;
echo “Working Status: $working_status<br>”;

echo “Total Result Pages: $total_pages<br>”;

$i = 1;
while($i<=$total_pages)
{
if($i<$total_pages)
{
echo “<a href=’http://localhost/power.page/search_3.php?form_type=”;?><?php echo $_SESSION[‘form_type’];?>&query_type=<?php echo $_SESSION[‘query_type’];?>&form_step=end&page_limit=1&page=<?php echo $i;?>’><?php echo ” $i “;?></a><?php
}
elseif($i==$page_number)
{
echo “<a href=’http://localhost/power.page/search_3.php?form_type=”;?><?php echo $_SESSION[‘form_type’];?>&query_type=<?php echo $_SESSION[‘query_type’];?>&form_step=end&page_limit=1&page=<?php echo $i;?>’><?php echo “<b> $i </b>”;?></a><?php
}
elseif($page_number>$total_pages)
{
echo “<a href=’http://localhost/power.page/search_3.php?form_type=”;?><?php echo $_SESSION[‘form_type’];?>&query_type=<?php echo $_SESSION[‘query_type’];?>&form_step=end&page_limit=1&page=<?php echo $previous_page;?>’><?php echo “<b> Previous </b>”;?></a><?php
//session_destroy();
}
$i++;
}
}
}
}
//ABOVE PAGINATION CODE GETS TRIGGERED WHEN CLICKING ‘SEARCH’ BUTTON.
//BELOW PAGINATION CODE GETS TRIGGERED WHEN CLICKING THE PAGE NUMBERS ON THE PAGINATION SECTION.
//BELOW CODE A COPY OF ABOVE PAGINATION CODE. BELOW CODE MUST RUN WHEN PAGE NUMBERS (PAGINATION LINKS) ON PAGINATION SECTION GET CLICKED.

//Connect to Database. (DB_SERVER, BD_USERNAME, DB_PASSWORD, DB_NAME).
$conn = mysqli_connect(“localhost”,”root”,””,”powerpage”);
$conn->set_charset(‘utf8mb4’); //Always set Charset.

if($conn === false)
{
die(“ERROR: Connection Error!. ” . mysqli_connect_error());
}
$form_step = $_GET[‘form_step’];
$page_number = $_GET[‘page’];
$result_per_page = $_GET[‘page_limit’];
$row_start = (($page_number * $result_per_page) – $result_per_page); //Offset (Row Number that ‘Starts’ on page).
$row_end = ($page_number * $result_per_page); //(Row Number that ‘Ends’ on page).
$previous_page = $page_number-1;
$next_page = $page_number+1;

echo “Row Start: $row_start”;
echo “Row End: $row_end”;

$sql_query = “SELECT * FROM users WHERE first_name = ? AND marital_status = ?”;
$stmt = mysqli_stmt_init($conn);
mysqli_stmt_prepare($stmt,$sql_query);
mysqli_stmt_bind_param($stmt,”ss”,$_POST[“first_name”],$_POST[“marital_status”]);

if($stmt == False)
{
//Close Connection.
mysqli_close($conn);
die(“<pre>2. Mysqli Prepare Failed!n”.mysqli_stmt_error($stmt).”n$sql_query</pre>”);
}
else
{
//Attempt to Execute the Prepared Statement.
mysqli_stmt_execute($stmt);
if(!mysqli_stmt_execute($stmt))
{
//Close Connection.
mysqli_close($conn);
die(“<pre>Statement Execution Failed!n”.mysqli_stmt_error($stmt).”nmysqli_stmt_bind_param</pre>”);
}
else
{
echo “<pre>Statement Execution Success!</pre>”;
}

$result_row_count = mysqli_stmt_get_result($stmt);
$total_pages = ceil($result_row_count/$result_per_page);

echo “TOTAL PAGES: $total_pages<br>”;

while($row = mysqli_fetch_array($result_row_count,MYSQLI_ASSOC)) //Use this if you use ‘$result = mysqli_stmt_get_result($stmt)’ instead of ‘mysqli_stmt_bind_result($stmt,$email)’.
{

//Retrieve Values.
$first_name = $row[“first_name”];//Use this if you use ‘$result = mysqli_stmt_get_result($stmt)’ instead of //’mysqli_stmt_bind_result($stmt,$email_count)’;
$middle_name = $row[“middle_name”];//Use this if you use ‘$result = mysqli_stmt_get_result($stmt)’ instead of //’mysqli_stmt_bind_result($stmt,$email_count)’;
$surname = $row[“surname”];//Use this if you use ‘$result = mysqli_stmt_get_result($stmt)’ instead of //’mysqli_stmt_bind_result($stmt,$email_count)’;
$gender = $row[“gender”];//Use this if you use ‘$result = mysqli_stmt_get_result($stmt)’ instead of //’mysqli_stmt_bind_result($stmt,$email_count)’;
$marital_status = $row[“marital_status”];//Use this if you use ‘$result = mysqli_stmt_get_result($stmt)’ instead of //’mysqli_stmt_bind_result($stmt,$email_count)’;
$working_status = $row[“working_status”];//Use this if you use ‘$result = mysqli_stmt_get_result($stmt)’ instead of //’mysqli_stmt_bind_result($stmt,$email_count)’;

echo “First Name: $first_name<br>”;
echo “Middle Name: $middle_name<br>”;
echo “Surname: $surname<br>”;
echo “Gender: $gender<br>”;
echo “Marital Status: $marital_status<br>”;
echo “Working Status: $working_status<br>”;

echo “Total Result Pages: $total_pages<br>”;

$i = 1;

while($i<=$total_pages)
{
if($i<$total_pages)
{
echo “<a href=’http://localhost/power.page/search_3.php?form_type=”;?><?php echo $_SESSION[‘form_type’];?>&query_type=<?php echo $_SESSION[‘query_type’];?>&form_step=end&page_limit=1&page=<?php echo $i;?>’><?php echo ” $i “;?></a><?php
}
elseif($i==$page_number)
{
echo “<a href=’http://localhost/power.page/search_3.php?form_type=”;?><?php echo $_SESSION[‘form_type’];?>&query_type=<?php echo $_SESSION[‘query_type’];?>&form_step=end&page_limit=1&page=<?php echo $i;?>’><?php echo “<b> $i </b>”;?></a><?php
}
elseif($page_number>$total_pages)
{
echo “<a href=’http://localhost/power.page/search_3.php?form_type=”;?><?php echo $_SESSION[‘form_type’];?>&query_type=<?php echo $_SESSION[‘query_type’];?>&form_step=end&page_limit=1&page=<?php echo $previous_page;?>’><?php echo “<b> Previous </b>”;?></a><?php
}
$i++;
}
}
}
}
}
?>
[/code]

My problem is in this line:

[code]
$total_pages = ceil($result_row_count/$result_per_page);
[/code]

to post a comment
PHP

13 Comments(s)

Copy linkTweet thisAlerts:
@NachfolgerJun 10.2020 — $result_row_count is a MySQLi statement result object, it's not a number and you cannot perform numeric operations on it. This is clearly told to you in the PHP warning.

You need to use num_rows attribute after storing the result--Unless there is a quicker way to get the number of rows that I don't know about.
Copy linkTweet thisAlerts:
@NogDogJun 10.2020 — If continuing to use procedural style code, mysqli_num_rows($result_row_count) in place of just $result_row_count. In OOP style: $result_row_count-&gt;row_count is equivalent. In either case, $result_row_count is a bad/misleading name, so probably should be changed to just $result or something similar, then you could refer to $result-&gt;row_count and it would read much better.
Copy linkTweet thisAlerts:
@developer_webauthorJun 11.2020 — @NogDog#1619380

But instead of using mysqli_num_rows(), I used the COUNT. And so, the $result_row_count should contain the row COUNT. Should it not ?

Look:
<i>
</i>$sql_query = "SELECT COUNT(*) FROM users WHERE first_name = ? AND marital_status = ?";
$stmt = mysqli_stmt_init($conn);
mysqli_stmt_prepare($stmt,$sql_query);
mysqli_stmt_bind_param($stmt,"ss",$_POST["first_name"],$_POST["marital_status"]);
if(!mysqli_stmt_execute($stmt))
{
//Close Connection.
mysqli_close($conn);
die("&lt;pre&gt;Statement Execution Failed!n".mysqli_stmt_error($stmt)."nmysqli_stmt_bind_param&lt;/pre&gt;");
}
else
{
echo "&lt;pre&gt;1. Statement Execution Success!&lt;/pre&gt;"; //DELETE THIS LINE
$result_row_count = mysqli_stmt_get_result($stmt);


What exactly is the mysqli_get_result() fetching ? Is it not fetching the matched row COUNT ? In that case, the $result_row_count should contain the row COUNT INT. Should it not ?

In another thread, you suggested I use the COUNt in the sql query over the mysqli_num_rows because mysqli_num_rows would always show value as '1' whenever it manages to collect the row count, even if the row count value is actually "0" (no matches found).
Copy linkTweet thisAlerts:
@developer_webauthorJun 11.2020 — @Nachfolger#1619379

You mean result_row_count() is a function and is reserved by php ? But I did not call any function. I called a variable.

Sorry. I do not understand objects. Still at procedural style and not at oop yet.
Copy linkTweet thisAlerts:
@NachfolgerJun 11.2020 — @developer_web#1619409

  • 1. NEVER count all rows count(*) you should be counting a SINGLE indexed column (Eg. a ID)

  • 2. The value of Count(ID) in a MySQL query will be retrieved just like you fetch a column. "Count" becomes a column, metaphorically speaking.


  • As NogDog said, $result_row_count is misleading. It doesn't matter that you're only selecting the number of rows, it's a statement object.. not a row count. It CONTAINS a row count, but isn't **that**.

    > @developer_web#1619409 mysqli_num_rows because mysqli_num_rows would always show value as '1' whenever it manages to collect the row count, even if the row count value is actually "0" (no matches found).

    If I had to guess, you were incorrectly using the function. You were probably getting the operation result (0 = False, 1 = True), hence the constant 1.
    Copy linkTweet thisAlerts:
    @developer_webauthorJun 11.2020 — @Nachfolger#1619412

    I think it was this particular script that NogDog told me to use the COUNT and not the mysqli_num_rows.

    After tests, I found NogDog 100% right. See for yourself and confirm what you just commented.
    <i>
    </i>&lt;?php

    session_start();

    if($_SERVER['REQUEST_METHOD'] == 'POST')
    {
    if(!isset($_POST['email_account']) || !isset($_POST['email_service']))
    {
    $email_error = "&lt;font color='red'&gt;Input Email Address!&lt;/color&gt;";
    }
    else
    {
    //Connect to Database. (DB_SERVER, BD_USERNAME, DB_PASSWORD, DB_NAME).
    $conn = mysqli_connect("localhost","root","","powerpage");
    $conn-&gt;set_charset('utf8mb4'); //Always set Charset.

    <i> </i> if($conn === false)
    <i> </i> {
    <i> </i> die("ERROR: Connection Error!. " . mysqli_connect_error());
    <i> </i> }
    <i> </i> else
    <i> </i> {
    <i> </i> //Set Parameters.
    <i> </i> $email = trim($_POST["email_account"]) . '@' . trim($_POST["email_service"]);
    <i> </i> $_SESSION['email'] = trim($_POST["email_account"]) . '@' . trim($_POST["email_service"]);//If this fails on test then replace it with above line
    <i> </i> echo "line 25 triggered: $email&lt;br&gt;";
    <i> </i>
    <i> </i> $sql_query = "SELECT COUNT(personal_email) FROM users WHERE personal_email = ?";
    <i> </i> $stmt = mysqli_prepare($conn,$sql_query);
    <i> </i> if($stmt == False)
    <i> </i> {
    <i> </i> //Close Connection.
    <i> </i> mysqli_close($conn);
    <i> </i> echo "Line 33&lt;br&gt;";//DELETE THIS
    <i> </i> die("&lt;pre&gt;Mysqli Prepare Failed!n".mysqli_stmt_error($stmt)."n$sql_query&lt;/pre&gt;");
    <i> </i> }
    <i> </i> else
    <i> </i> {
    <i> </i> mysqli_stmt_bind_param($stmt,'s',$email);
    <i> </i>
    <i> </i> if(!mysqli_stmt_execute($stmt))
    <i> </i> {
    <i> </i> //Close Connection.
    <i> </i> mysqli_close($conn);
    <i> </i> die("Could not mysqli_stmt_execute! Please try again later!");
    <i> </i> }
    <i> </i>
    <i> </i> //$result = mysqli_stmt_bind_result($stmt,$email);
    <i> </i> //$result = mysqli_stmt_store_result($stmt);
    <i> </i> $result = mysqli_stmt_get_result($stmt);
    <i> </i>
    <i> </i> if(mysqli_fetch_array($result, MYSQLI_NUM)[0])
    <i> </i> //($result-&gt;fetch_array()[0]) //DELETE THIS. THIS ALSO NOT RETYPED.
    <i> </i> //if(mysqli_fetch_array($result, MYSQLI_NUM)) //WHY THIS NOT WORK UNLESS NUM ARRAY GIVEN ?
    <i> </i> {
    <i> </i> echo "Line 57 triggered: Function login() will trigger!&lt;br&gt;"; //DELETE THIS
    <i> </i> $_SESSION['session_type'] = 'login';
    <i> </i> login();
    <i> </i>
    <i> </i> }
    <i> </i> else
    <i> </i> {
    <i> </i> echo "Line 61 triggered: Function register() will trigger!&lt;br&gt;"; //DELETE THIS
    <i> </i> $_SESSION['session_type'] = 'register';
    <i> </i> register();
    <i> </i> }
    <i> </i> }
    <i> </i> }
    <i> </i>}
    }


    Basically, user inputs his email. If it is in db then that means he is registered. And so, the login() triggers.

    Else, if no email match, then the register() triggers and registers him.

    As for COUNT(id) I should use and not COUNT(*). I used the former originally and got that error. then i switched to the latter and same result.

    NachFolder,

    Are you 100% sure I should not use the COUNT(id) here (my original post code) and the mysqli_num_rows() ?

    Ok. I go and do that now. If I encounter any problems then you owe me a fix! Lol! ;)
    Copy linkTweet thisAlerts:
    @developer_webauthorJun 11.2020 — @NogDog#1619380

    NogDog,

    I have a question.

    Instead of using this:
    <i>
    </i>$result = mysqli_num_rows($stmt)


    What happens if I use the following to get the matched row count:

    <i>
    </i>$result = mysqli_stmt_get_result($stmt)


    Even if it works, is it ok to use it over the other or not ? If not then why not ? Just curious to know that is all.
    <i>
    </i>$sql_query = "SELECT COUNT(id) FROM users WHERE first_name = ? AND marital_status = ?";


    And, if I use the former, then I should switch the query from following to further following or not ?

    <i>
    </i>$sql_query = "SELECT * FROM users WHERE first_name = ? AND marital_status = ?";


    Remember, I am still a beginner at procedural style. I understand nothing about objects. When I get errors about it I scratch my head. I just can't stomache the object thing even though I read on it many times.
    Copy linkTweet thisAlerts:
    @developer_webauthorJun 11.2020 — @NachFolger,

    On my previous post to you, you see a user login-reg code. Can you tell me, over there, am I using the COUNT function needlessly or not ? Look at my previous post/reply to you.
    Copy linkTweet thisAlerts:
    @NogDogJun 11.2020 — If all you want to do is get a count, then I would do a SELECT COUNT() type of query. Since it should just return one row with one field, you can then just fetch that row and grab that field's contents (again, a bit easier in PDO).
    <i>
    </i>// note the alias added via "AS" to make it easier to fetch that result:
    $sql = "SELECT COUNT(id) AS num_users FROM users WHERE first_name = ? AND marital_status = ?";
    $stmt = mysqli_prepare($conn, $sql);
    // bind any params here...
    // then...
    $result = mysqli_stmt_execute($stmt);
    $row = mysqli_fetch_assoc($result);
    $the_count_you_want = $row['num_users'];

    Untested, for guidance only, I don't use MySQLi so who knows what I did wrong.
    Copy linkTweet thisAlerts:
    @developer_webauthorJun 11.2020 — @NogDog,

    Maybe when I use the COUNT in the query, I should use mysqli_stmt_bind_result() to get the row count instead of mysqli_stmt_get_result() ?

    Maybe, mysqli_stmt_get_result() doesn't get the row count (like i am expecting here) but the whole row like so:

    first_name|surname

    I usually use the mysqli_stmt_get_result() or mysqli_stmt_bind_result to get the row data.

    In my original post, I used COUNT(id) in my mysqli query and then used mysqli_stmt_get_result() to get the row count. I suspect, I should replace mysqli_stmt_get_result() for my_sqli_stmt_bind_result() and everything would be ok even if I do not use the mysqli_stmt_num_rows() function. Correct me if I am wrong.
    Copy linkTweet thisAlerts:
    @developer_webauthorJun 12.2020 — @NogDog#1619418

    I'm afraid your code sample didn't work.

    I copied your technique. Look:
    <i>
    </i>$sql_query = "SELECT COUNT(id) as num_users FROM users WHERE first_name = ? AND marital_status = ?";
    $stmt = mysqli_stmt_init($conn);
    mysqli_stmt_prepare($stmt,$sql_query);
    mysqli_stmt_bind_param($stmt,"ss",$_POST["first_name"],$_POST["marital_status"]);

    if(!mysqli_stmt_execute($stmt))
    {
    //Close Connection.
    mysqli_close($conn);
    die("&lt;pre&gt;Statement Execution Failed!n".mysqli_stmt_error($stmt)."nmysqli_stmt_bind_param&lt;/pre&gt;");
    }
    else
    {
    echo "&lt;pre&gt;1. Statement Execution Success!&lt;/pre&gt;"; //DELETE THIS LINE
    $result = mysqli_stmt_execute($stmt);
    $row = mysqli_fetch_assoc($result);
    $row_match_count = $row['num_users'];
    echo "Row Match Count: $row_match_count&lt;br&gt;";
    die;


    Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in C:xampphtdocspower.pagepagination_test_SIMPLE.php on line 118

    Warning on this line:
    <i>
    </i>$row = mysqli_fetch_assoc($result);
    Copy linkTweet thisAlerts:
    @developer_webauthorJun 12.2020 — @NogDog,

    I just found this oop style code here:

    https://www.thetopsites.net/article/53053914.shtml

    <i>
    </i>if (!$conn = new mysqli("host", "user", "pass", "db")) {
    echo "Database Connection Error: " , $conn-&gt;connect_error; // don't show this to the public
    } else {
    if (!$stmt = $conn-&gt;prepare("SELECT COUNT(*) FROM <span><code>table</code></span> WHERE id= ?")) {
    echo "Prepare Syntax Error: " , $conn-&gt;error; // don't show this to the public
    } else {
    if (!$stmt-&gt;bind_param("s", $id) // if trouble while binding to ? placeholder
    || !$stmt-&gt;execute() // or if trouble while executing
    || !$stmt-&gt;bind_result($num_rows) // or if trouble while binding to $num_rows
    || !$stmt-&gt;fetch()) { // or if trouble while fetching the one row.
    echo "Statement Error: " , $stmt-&gt;error; // don't show this to the public
    }else{
    echo $num_rows;
    }
    $stmt-&gt;close(); // no longer need statement
    }
    $conn-&gt;close(); // no longer need connection

    Can you check if I got it write converting it to procedural style ?

    This is what I did last night and so switching it back to this as the above tutorial sample oop code seems to be in line with what I did last night in my final post of last night. Post: 1619419
    <i>
    </i>$sql_query = "SELECT COUNT(id) as TOTAL FROM users WHERE first_name = ? AND marital_status = ?";
    $stmt = mysqli_stmt_init($conn);
    mysqli_stmt_prepare($stmt,$sql_query);
    mysqli_stmt_bind_param($stmt,"ss",$_POST["first_name"],$_POST["marital_status"]);

    //Attempt to Execute the Prepared Statement.
    if(!mysqli_stmt_execute($stmt))
    {
    //Close Connection.
    mysqli_close($conn);
    die("&lt;pre&gt;Statement Execution Failed!n".mysqli_stmt_error($stmt)."nmysqli_stmt_bind_param&lt;/pre&gt;");
    }
    else
    {
    echo "&lt;pre&gt;1. Statement Execution Success!&lt;/pre&gt;"; //DELETE THIS LINE
    $result = mysqli_stmt_bind_result($stmt,$sql_query);//Working
    //$result = mysqli_stmt_bind_result($stmt,$row_count);//NOT WORKING
    echo "Result: $result&lt;br&gt;";
    //echo "Row Count: $row_count&lt;br&gt;";
    die('dead');


    NogDog,

    Based on this procedural style mysqli_stmt_bind_result() from the pghp manual, this line should have worked:
    <i>
    </i>$result = mysqli_stmt_bind_result($stmt,$row_count);//NOT WORKING


    Instead, this one works:
    <i>
    </i>$result = mysqli_stmt_bind_result($stmt,$sql_query);//Working

    This one never should have worked because the last param of mysqli_stmt_bind_result() should be the variable that will hold the row count. And not the SQL QUERY variable itself.

    Check the procedural style on php manual here for my confirmation:

    https://www.php.net/manual/en/mysqli-stmt.bind-result.php

    This code should have worked to echo the row count but it doesn't:
    <i>
    </i>$sql_query = "SELECT COUNT(id) as TOTAL FROM users WHERE first_name = ? AND marital_status = ?";
    $stmt = mysqli_stmt_init($conn);
    mysqli_stmt_prepare($stmt,$sql_query);
    mysqli_stmt_bind_param($stmt,"ss",$_POST["first_name"],$_POST["marital_status"]);

    //Attempt to Execute the Prepared Statement.
    if(!mysqli_stmt_execute($stmt))
    {
    //Close Connection.
    mysqli_close($conn);
    die("&lt;pre&gt;Statement Execution Failed!n".mysqli_stmt_error($stmt)."nmysqli_stmt_bind_param&lt;/pre&gt;");
    }
    else
    {
    echo "&lt;pre&gt;1. Statement Execution Success!&lt;/pre&gt;"; //DELETE THIS LINE
    //$result = mysqli_stmt_bind_result($stmt,$sql_query);//THIS LINE WORKING TO ECHO ROW COUNT. SHOULD NOT HAVE WORKED
    $result = mysqli_stmt_bind_result($stmt,$row_count);//NOT WORKING TO ECHO ROW COUNT. SHOULD HAVE WORKED
    //echo "Result: $result&lt;br&gt;";
    echo "Row Count: $row_count&lt;br&gt;";
    die('dead');


    Echoes:

    **1. Statement Execution Success!

    Row Count:

    dead**

    And this code NEVER should have worked to echo the row count BUT IT DOES WORK!
    <i>
    </i>$sql_query = "SELECT COUNT(id) as TOTAL FROM users WHERE first_name = ? AND marital_status = ?";
    $stmt = mysqli_stmt_init($conn);
    mysqli_stmt_prepare($stmt,$sql_query);
    mysqli_stmt_bind_param($stmt,"ss",$_POST["first_name"],$_POST["marital_status"]);

    //Attempt to Execute the Prepared Statement.
    if(!mysqli_stmt_execute($stmt))
    {
    //Close Connection.
    mysqli_close($conn);
    die("&lt;pre&gt;Statement Execution Failed!n".mysqli_stmt_error($stmt)."nmysqli_stmt_bind_param&lt;/pre&gt;");
    }
    else
    {
    echo "&lt;pre&gt;1. Statement Execution Success!&lt;/pre&gt;"; //DELETE THIS LINE
    $result = mysqli_stmt_bind_result($stmt,$sql_query);//THIS LINE WORKING TO ECHO ROW COUNT. SHOULD NOT HAVE WORKED
    //$result = mysqli_stmt_bind_result($stmt,$row_count);//NOT WORKING TO ECHO ROW COUNT. SHOULD HAVE WORKED
    echo "Result: $result&lt;br&gt;";
    //echo "Row Count: $row_count&lt;br&gt;";
    die('dead');

    Echoes:

    **1. Statement Execution Success!

    Result: 1

    dead**
    Copy linkTweet thisAlerts:
    @developer_webauthorJun 12.2020 — <i>
    </i>//$result = mysqli_stmt_bind_result($stmt,$sql_query);//Working
    $result = mysqli_stmt_bind_result($stmt,$row_count);//NOT WORKING
    //echo "Result: $result&lt;br&gt;";
    echo "Row Count: $row_count&lt;br&gt;";
    die('dead');
    if(mysqli_stmt_fetch($stmt))


    I think I know why why the 2nd (uncommented) line was not working to echo row count. I had not done the 'fetching' yet.

    But why was the 1st line (commented one) working by echoing "1" when I uncomment the line ?

    I took that "1" as a row count.

    Or maybe the "1" was not row count but an expression showing that the $result variable had a value and was not empty ? Can someone confirm this ?

    Now, once you have confirmed/unconfirmed that, let me know this ...

    <i>
    </i>$sql_query = "SELECT COUNT(id) FROM users WHERE first_name = ? AND marital_status = ?";


    <i>
    </i>$sql_query = "SELECT COUNT(id) AS TOTAL FROM users WHERE first_name = ? AND marital_status = ?";


    Why sometimes they use "AS TOTAL" and sometimes not in the COUNT() function ?

    Thank You!
    ×

    Success!

    Help @developer_web spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 4.19,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...