I am trying to determine whether an email is aready in my database or not. For this purpose I write this piece of code:
$query_checkmail = "SELECT COUNT(*) FROM user WHERE email = ?;";
if($stmt = mysqli_prepare($connection, $query_checkmail))
{
mysqli_stmt_bind_param($stmt, "s", $_POST["email"]);
$result = mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
}
echo "<pre>";
var_dump($result, mysqli_fetch_all($result));
echo "</pre>";
Since I expect the query to return 0 because the email should not be in the database, the output is this (from var_dump):
bool(true)
NULL
I do not understand why I do not get a mysqli result object with the value 0 but instead the boolean value true which always triggers a PHP Warning in the logs if I want to check it.
I had a normal SELECT id FROM user WHERE email = ?;"; before and got the same result. I thought with COUNT I could prevent this but my attempt has obviously failed.
I also found this Stackoverflow Link but unfortunatly it did not help my to solve my problem of getting to know whether the value already exists or not.
Any help is highly appreciated. If this information is not enough I will provide the missing bits immediatly.
The count itself doesn't return a boolean true or false, you're checking against a variable assigned from mysqli_stmt_execute(), which returns a boolean. This has nothing to do with the results of the query. If you'll read the documentation on this function, specifically the return values of mysqli_stmt_execute(), you'll see that they are either true or false, so there is no surprise that a var_dump() of that would return a boolean.
If you want the actual count, you have to use mysqli_stmt_bind_result() and mysqli_stmt_fetch() to get the results of the count. This would produce the correct results. The manuals of this would show examples of that if you are unsure how to use these functions.
http://php.net/manual/en/mysqli-stmt.execute.php
http://php.net/manual/en/mysqli-stmt.bind-result.php
As for the NULL, it's because you're passing a boolean into the mysqli_fetch_all() function, which expects a mysqli_result, while you're giving it a boolean from the above-mentioned reasons.
mysqli_stmt_execute returns true or false, you should use mysqli_stmt_get_result after the excute to retrieve the data
i copied sample code from PHP.NET
$result = mysqli_stmt_get_result($stmt);
while ($row = mysqli_fetch_array($result, MYSQLI_NUM))
{
foreach ($row as $r)
{
print "$r ";
}
print "\n";
}
I suggest You to use PDO instead of mysqli. First make a connection, and then:
$stm = $pdo->prepare("select count(*) from user where email = :email");
$stm->bindValue(':email', $_POST['email'], PDO::PARAM_STR);
if ($stm->execute()) {
$c = $stm->fetchColumn();
echo $c;
}
You can use MeekroDB, is the perfect php-mysql library. For example, with this code line you get similar result:
$user = DB::query("SELECT UserId FROM user WHERE email = %s",$email);
$count = DB::count();
As you can see, the code is cleaner and easy to write. Meekro is free and opensource: http://meekro.com/quickstart.php
Related
I am having a strange issue with mysqli_num_rows. Searching for this issue, I've only found people having issues where NULL is returned no matter what. I also checked the official documentation for the function, and it says it returns an integer of the number of rows returned by the query. Whenever my query returns 1 row (it never should return more), it behaves as I expect. When the query returns 0 rows, I expect the function to return 0, but it returns NULL. Why doesn't it return 0?
I know that my database connection is good and my query works correctly, because when I look for a record that's in the database, I get an integer back. I just can't figure out why this is returning NULL rather than 0.
function getArtistID($name) {
global $conn;
$query = "SELECT artist_id FROM artist WHERE artist_name LIKE '${name}'";
$result = mysqli_query($conn, $query);
if ($result->num_rows) {
$row = mysqli_fetch_assoc($result);
return $row['artist_id'];
} else {
return 0;
}
}
Here's some code that I used to reproduce a case where num_rows seems to be NULL:
<?php
error_reporting(E_ALL);
$conn = new mysqli('127.0.0.1', 'root', null, 'test');
$sql = "SELECT * FROM dual";
$result = $conn->query($sql);
if ($result === false) {
print "Error: {$conn->error}\n";
}
$n = $result->num_rows;
echo "Dump the num_rows property: ";
var_dump($n);
Output:
Error: No tables used
Notice: Trying to get property of non-object in /Users/bkarwin/Documents/SO/my.php on line 14
Dump the num_rows property: NULL
The notice is because it's invalid to access an object-oriented property of a variable that is not an object. This is a frequent source of confusion for PHP developers, and it's a byproduct of the fact that PHP is a loosely typed language, and functions like query() can return either a result object, or a boolean scalar.
The query() function actually returned a false as $result because of some error. In my code, I checked for this error, and you didn't.
When you run mysqli::query() or mysqli::prepare() or mysqli_stmt::execute(), you must check for error conditions every time.
Something about your query caused an error. It's up to you to check for the error and report it.
Update: I edited some text above to make the explanation better, but it might make some comments below seem out of place.
I just can't figure out why this is returning NULL rather than 0.
We can only guess without seeing the log output; but, it is likely the return value is null because it raised an error instead.
You need to ensure that errors are handled when calling a function, before attempting to use the return value.
I have maybe a really simple problem for you but it's making me crazy.
In my index.php I call my userHandler to retrieve data from MySQL in this first line of code but whatever I do the $result stays null.
When debugging, I can clearly see that the MySQL returns one row and its not null.
$result = $userHandler->getUserByPhone($phoneNumber);
if ($result != NULL) {
}
Here is the method that returns result:
public function getUserByPhone($Phone) {
$stmt = $this->conn->prepare("SELECT user.* FROM user");
$stmt->execute();
$result = $stmt->get_result();
$num_of_rows = $result->num_rows;
$stmt->store_result();
$stmt->close();
return $result;
}
And here is the screenshot of function
and it returns null
Edit:
Ok i tought that after closing the statement the data would be lost. and thats true if i still want to return the data of the statement. but before closing i stored the data in a variable. and then close it. I checked in debug after closing the variable still has the data.
Then i thought maybe the variable will be set null too (after all im a noob in php i can think none sense :D )
so i only returned True. But its still null in receiving end.
You'd need to pull the data you need out of $result prior to closing the statement. Closing the statement will clear the statement and the related result set, rendering the value null.
Since getUserByPhone should seemingly return a user, I'd suggest that you get the user data out of the result and return that rather than closing the statement then trying to return the mysqli result object.
Something like:
$result = $stmt->get_result();
$user = $result->fetch_assoc();
$stmt->close();
return $user;
Would someone please me with the code below, I am inexperienced in this area and my class in SQL was "A long time ago in a galaxy far, far away..." I know the connection string works because I have used it in other functions with this app. I have even used the code below for retrieving *rows from another table in another function, for the most part, except that I didn't use the WHERE clause.
First, I am able to store IP addresses in the table using a function and it is working well. Now I want to check to see if a given one exist in this table. Partial code is given below.
What seems to always return is 0 rows. I have put in test data into the table and hard-coded the $ipA, but I still get 0 rows return. Please help if possible and thanks for the effort spent.
function checkDB($ipA) {
require_once('connection.inc.php');
$resultAns = "";
//create db connection
$conn = dbConnect();
//init prepared stmt
$stmt = $conn->stmt_init();
//Set sql query for ipAddress search
//prepare the SQL query
$sql = 'SELECT * FROM ipAddress WHERE ipA = ?';
//submit the query and capture the result
if ($stmt->prepare($sql)) {
$stmt->bind_param('s', $ipA);
$stmt = $stmt->execute();
//if qry triggers error affeted_rows value becomes -1 &
//php treats -1 as true; so test for greater than 0
$numRows = $stmt->num_rows; //not to sure about the syntax here
}
// I want to know if the query brought back something or not, I don't what
// to know exactly what, only that it found a match or did not find a match.
// echos are for testing purposes to show me where I am landing.
if ($numRows == 0) {
echo '<script type="text/javascript">window.alert("numRows = 0")</script>';
$resultAns = 0;
} elseif ($numRows == 1) {
echo '<script type="text/javascript">window.alert("numRows = 1")</script>';
$resultAns = 1;
}
return $resultAns;
}
Try storing the result after you execute
$stmt->store_result();
Use $stmt->store_result(); before you call num_rows.
While the others caught one reason that $numRows would never receive a value other than 0, the other piece of code that was flawed and caused problems was...
$stmt = $stmt->execute(); which should have been just $stmt->execute();
I must have mixed it up with other code I wrote from somewhere else.
Thanks for the answers, they did help.
Below is some poorly written and heavily misunderstood PHP code with no error checking. To be honest, I'm struggling a little getting my head around the maze of PHP->MySQLi functions! Could someone please provide an example of how one would use prepared statements to collect results in an associative array whilst also getting a row count from $stmt? The code below is what I'm playing around with. I think the bit that's throwing me off is using $stmt values after store_result and then trying to collect an assoc array, and I'm not too sure why...
$mysqli = mysqli_connect($config['host'], $config['user'], $config['pass'], $config['db']);
$stmt = $mysqli->prepare("SELECT * FROM licences WHERE generated = ?");
$stmt->bind_param('i', $core['id']);
$result = $stmt->execute();
$stmt->store_result();
if ($stmt->num_rows >= "1") {
while($data = $result->fetch_assoc()){
//Loop through results here $data[]
}
}else{
echo "0 records found";
}
I feel a little cheeky just asking for code, but its a working demonstration of my circumstances that I feel I need to finally understand what's actually going on. Thanks a million!
I searched for a long time but never found documentation needed to respond correctly, but I did my research.
$stmt->get_result() replace $stmt->store_result() for this purpose.
So, If we see
$stmt_result = $stmt->get_result();
var_dump($stmt_result);
we get
object(mysqli_result)[3]
public 'current_field' => int 0
public 'field_count' => int 10
public 'lengths' => null
public 'num_rows' => int 8 #That we need!
public 'type' => int 0
Therefore I propose the following generic solution. (I include the bug report I use)
#Prepare stmt or reports errors
($stmt = $mysqli->prepare($query)) or trigger_error($mysqli->error, E_USER_ERROR);
#Execute stmt or reports errors
$stmt->execute() or trigger_error($stmt->error, E_USER_ERROR);
#Save data or reports errors
($stmt_result = $stmt->get_result()) or trigger_error($stmt->error, E_USER_ERROR);
#Check if are rows in query
if ($stmt_result->num_rows>0) {
# Save in $row_data[] all columns of query
while($row_data = $stmt_result->fetch_assoc()) {
# Action to do
echo $row_data['my_db_column_name_or_ALIAS'];
}
} else {
# No data actions
echo 'No data here :(';
}
$stmt->close();
$result = $stmt->execute(); /* function returns a bool value */
reference : http://php.net/manual/en/mysqli-stmt.execute.php
so its just sufficient to write $stmt->execute(); for the query execution.
The basic idea is to follow the following sequence :
1. make a connection. (now while using sqli or PDO method you make connection and connect with database in a single step)
2. prepare the query template
3. bind the the parameters with the variable
4. (set the values for the variable if not set or if you wish to change the values) and then Execute your query.
5. Now fetch your data and do your work.
6. Close the connection.
/*STEP 1*/
$mysqli = mysqli_connect($servername,$usrname,$pswd,$dbname);
/*STEP 2*/
$stmt = $mysqli->prepare("SELECT * FROM licences WHERE generated = ?");
/*Prepares the SQL query, and returns a statement handle to be used for further operations on the statement.*/
//mysqli_prepare() returns a statement object(of class mysqli_stmt) or FALSE if an error occurred.
/* STEP 3*/
$stmt->bind_param('i', $core['id']);//Binds variables to a prepared statement as parameters
/* STEP 4*/
$result = $stmt->execute();//Executes a prepared Query
/* IF you wish to count the no. of rows only then you will require the following 2 lines */
$stmt->store_result();//Transfers a result set from a prepared statement
$count=$stmt->num_rows;
/*STEP 5*/
//The best way is to bind result, its easy and sleek
while($data = $stmt->fetch()) //use fetch() fetch_assoc() is not a member of mysqli_stmt class
{ //DO what you wish
//$data is an array, one can access the contents like $data['attributeName']
}
One must call mysqli_stmt_store_result() for (SELECT, SHOW, DESCRIBE, EXPLAIN), if one wants to buffer the complete result set by the client, so that the subsequent mysqli_stmt_fetch() call returns buffered data.
It is unnecessary to call mysqli_stmt_store_result() for other queries, but if you do, it will not harm or cause any notable performance in all cases.
--reference: php.net/manual/en/mysqli-stmt.store-result.php
and http://www.w3schools.com/php/php_mysql_prepared_statements.asp
One must look up the above reference who are facing issue regarding this,
My answer may not be perfect, people are welcome to improve my answer...
If you would like to collect mysqli results into an associative array in PHP you can use fetch_all() method. Of course before you try to fetch the rows, you need to get the result with get_result(). execute() does not return any useful values.
For example:
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli($config['host'], $config['user'], $config['pass'], $config['db']);
$mysqli->set_charset('utf8mb4'); // Don't forget to set the charset!
$stmt = $mysqli->prepare("SELECT * FROM licences WHERE generated = ?");
$stmt->bind_param('i', $core['id']);
$stmt->execute(); // This doesn't return any useful value
$result = $stmt->get_result();
$data = $result->fetch_all(MYSQLI_ASSOC);
if ($data) {
foreach ($data as $row) {
//Loop through results here
}
} else {
echo "0 records found";
}
I am not sure why would you need num_rows, you can always use the array itself to check if there are any rows. An empty array is false-ish in PHP.
Your problem here is that to do a fetch->assoc(), you need to get first a result set from a prepared statement using:
http://php.net/manual/en/mysqli-stmt.get-result.php
And guess what: this function only works if you are using MySQL native driver, or "mysqlnd". If you are not using it, you'll get the "Fatal error" message.
You can try this using the mysqli_stmt function get_result() which you can use to fetch an associated array. Note get_result returns an object of type mysqli_result.
$stmt->execute();
$result = $stmt->get_result(); //$result is of type mysqli_result
$num_rows = $result->num_rows; //count number of rows in the result
// the '=' in the if statement is intentional, it will return true on success or false if it fails.
if ($result_array = $result->fetch_assoc(MYSQLI_ASSOC)) {
//loop through the result_array fetching rows.
// $ rows is an array populated with all the rows with an associative array with column names as the key
for($j=0;$j<$num_rows;$j++)
$rows[$j]=$result->fetch_row();
var_dump($rows);
}
else{
echo 'Failed to retrieve rows';
}
Here is an example code:
$db = connect_db();
$result = $db->prepare("SELECT * FROM users");
$result->execute();
$result = $result->fetchAll();
If my users table was empty there would be no rows returned, so what does $result hold?
If I reference it like this:
if(!(isset($result))) print "no users";
will it work?
Thanks for any answers!
From the docs for fetchAll():
An empty array is returned if there are zero results to fetch, or FALSE on failure.
So, no, isset() will not work as the variable is set with an empty array. You should use empty() or count() instead. Or, better yet, one of the PDO methods like rowCount() (if your db supports this).
Note: The docs are your friend.