I want to get num rows of this query. Now output is empty and I don't know what to do now...
EDITED
And I need to put to "?" text from input..
My code:
$stmt = $conn->prepare("SELECT email FROM users WHERE email=?");
$stmt->bind_param("s", $email);
$stmt->execute();
$stmt->store_result();
$num_rows = $stmt->num_rows;
$stmt->bind_result($email);
$stmt->fetch();
echo $email;
if ($num_rows > 0)
{
echo "Exists";
}
else
{
echo "doesnt exists";
}
$stmt->close();
$conn->close();
Related
What i want to do is to check if the email & username are already inside on my database, then if it is already inside the database then i want to echo out the username, how to fix this? thanks
$sql = "SELECT * FROM user WHERE uidUser=? OR emailUser=?";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
header("Location: ../signup.php?error=sqlerror");
exit();
}
else {
mysqli_stmt_bind_param($stmt, "ss", $username, $email);
mysqli_stmt_execute($stmt);
mysqli_stmt_store_result($stmt);
$resultCheck = mysqli_stmt_num_rows($stmt);
if ($resultCheck > 0) {
while ($row=mysqli_stmt_fetch($stmt)) {
echo "Name: ".$row['uidUser'];
}
}
}
I have a interesting problem.
// in pdo with function --> not work
function UserIsExist($name)
{
global $db;
$stmt = $db->prepare("SELECT id FROM tarskereso_users WHERE email = '$name' LIMIT 1");
$stmt->execute();
if ($stmt->fetchColumn() == 1) return 1;
else return 0;
}
// with MySQLi --> not working
function UserIsExist($name)
{
global $db;
$stmt = $db->prepare("SELECT id,email FROM tarskereso_users WHERE email = ? LIMIT 1");
$stmt->bind_param('s', $name);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows == 1)
return 1;
else
return 0;
$stmt->close();
}
// In Register.php
... other ..
if(UserIsExist($user) == 1)
$error_msg = "Is Exist";
else
{
$birthdate = $year.'.'.$month.'.'.$day;
CreateUser($user,$pass,$birthdate,$sex);
$error_msg = 'Success';
}
So, with function not working, I try with:
$stmt = $db->prepare("SELECT id,email FROM tarskereso_users WHERE email = ? LIMIT 1");
$stmt->bind_param('s', $name);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows > 0)
... other ...
else
echo 'isnt exist...';
$stmt->close();
but not working, the num_rows always return 0. And the account in the database successfuly created
in pdo, num_rows won't work.
you have to use $sql->rowCount() method to get number of records in a table.
<?php
$sql = $con->prepare("<YOUR SQL QUERY HERE>");
$sql->execute();
if($sql->rowCount() > 0){
echo $sql->rowCount() ." rows found";
}
?>
$stmt = $mysqli->prepare("SELECT username, email, password, code FROM temp_users WHERE code = ?");
$stmt->bind_param('s', $code);
$stmt->execute();
$stmt->store_result();
//if SELECT statement returns 1, grab data.
if ($stmt->num_rows === 1) {
echo "Got Row";
$result = $stmt->get_result();
var_dump($result);
while ($row = $result->fetch_assoc()) {
$username = $row['username'];
$email = $row['email'];
$password = $row['password'];
}
This is really weird, the query must be going through because the script is echoing "Got Row", and I have no errors up to that point. But when I try to use $result->fetch_assoc() I get an error,and $result is spitting out false, so why is that? Please excuse how dumb this question may seem, I'm still learning how to use mysqli. :)
Your question is neither dumb nor weird. You are simply confused by store_result() and get_result().
Both of these functions fetch the whole record set from the database. Once the data is fetched you can't fetch it again. Therefore, you can't use both of these functions at the same time!
We can fix your code in two ways.
With store_result():
$stmt = $mysqli->prepare("SELECT username, email, password, code FROM temp_users WHERE code = ?");
$stmt->bind_param('s', $code);
$stmt->execute();
$stmt->store_result();
//if SELECT statement returns 1, grab data.
if ($stmt->num_rows === 1) {
echo "Got Row";
$stmt->bind_result($username, $email, $password);
while ($stmt->fetch()) {
// use the data here
var_dump($username);
}
}
with get_result():
$stmt = $mysqli->prepare("SELECT username, email, password, code FROM temp_users WHERE code = ?");
$stmt->bind_param('s', $code);
$stmt->execute();
$result = $stmt->get_result();
//if SELECT statement returns 1, grab data.
if ($result->num_rows === 1) { // <--- !!! We are using the result object here
echo "Got Row";
foreach ($result as $row) {
$username = $row['username'];
$email = $row['email'];
$password = $row['password'];
}
}
I actually have this PHP code, which i use to obtain my database info..
function searchUserEmail($username, $raw_email){
//Prepare Statements
$query = "SELECT * FROM users WHERE username = ?";
$query2 = "SELECT * FROM users WHERE email = ?";
//Sanitize Input
$user = $this->conn->real_escape_string($username);
$email = $this->conn->real_escape_string($raw_email);
if($stmt = $this->conn->prepare($query)){
$stmt->bind_param('s', $user);
$stmt->execute();
if($stmt->num_rows > 0){
$stmt->close();
return true;
}
}
if($stmt = $this->conn->prepare($query2)){
$stmt->bind_param('s', $email);
$stmt->execute();
if($stmt->num_rows > 0){
$stmt->close();
return true;
}
}
return false;
}
I've tried this alot of times, ran it through a statement checker, still it doesn't work. Is there anything i'm missing???
Somehow it always returns a false (Even though its suppose to return true)
mysql_select_db($dataname,$conn);
I can't figure out what is wrong with these few lines:
if ( $stmt = $db->prepare('SELECT * FROM users WHERE username = ? LIMIT 1');
$stmt->bind_param('s', $username);
$stmt->execute();
$stmt->store_result();
$stmt->fetch();
$stmt->num_rows == 1;
)
{
$errors[] = "Username is taken.";
}
if($stmt = $db->prepare('SELECT * FROM users WHERE username = ? LIMIT 1'))
{
$stmt->bind_param('s', $username);
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows > 0)
{
$errors[] = "Username $username is taken.";
}
$stmt->free_result();
$stmt->close();
}
Not sure how you got your other code, but I think you want something like this:
$stmt = $db->prepare('SELECT * FROM users WHERE username = ? LIMIT 1');
$stmt->bind_param('s', $username);
$stmt->execute();
$stmt->bind_result($res);
if($stmt->num_rows == 1)
{
$errors[] = "Username is taken.";
}
There's an unnecessary fetch in your original code, also