Select statement is not working properly - php

after i select from the db, i keep getting 0 when it's actually 1
Code:
$username = $_SESSION['username'];
echo $username;
$sql = "SELECT activated FROM members WHERE username = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param('s', $username);
$stmt->execute();
$stmt->bind_result($emailactivated);
$stmt -> close();
echo $emailactivated;
Echo says 0.. it should be 1, and note : $username isn't empty

You're missing $stmt->fetch()
http://php.net/manual/en/mysqli-stmt.fetch.php
$stmt = $conn->prepare($sql);
$stmt->bind_param('s', $username);
$stmt->execute();
$stmt->bind_result($emailactivated);
$stmt->fetch(); //right here
$stmt -> close();

Related

Mysql get count with php prepared

I want get count rows in my table. How can I do it?
<?php
require_once "config.php";
$conn = new mysqli($servername, $username, $password, $dbname);
$stmt = $conn->prepare("SELECT COUNT(*) FROM `books`");
$stmt->execute();
$result = $stmt->get_result();
?>
Try this :
$query = "SELECT COUNT(*) FROM books";
$stmt = $conn->prepare($query);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($Count);
$stmt->fetch();
echo "Count: $Count";

PHP MySQL: How to get result from prepared statement? [duplicate]

This question already has an answer here:
Store_result and get_result for statement
(1 answer)
Closed last year.
$conn = new mysqli(.....);
$param = $_GET['manf'];
$stmt = $conn->prepare('select manf from manf where manf = ?');
$stmt->bind_param('s', $param);
$stmt->execute();
$stmt->store_result();
echo $stmt->num_rows;
$result = $stmt->get_result();
if(!$result){
die(mysql_error());
}
while($row = $result->fetch_assoc()){
echo $row['manf'];
}
echo $stmt->num_rows prints right vaule however I can't get results from while statement. I also tried mysqli::bind_result but didn't work.
How can I fix this?
Try this:
$conn = new mysqli(.....);
$param = $_GET['manf'];
$stmt = $conn->prepare('select manf from manf where manf = ?');
$stmt->bind_param('s', $param);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($result);
echo $stmt->num_rows;
while($stmt->fetch()){
echo $result;
}
$stmt->free_result();
$stmt->close();
for fetching you need to use $stmt->fetch().

Bind_Result always return 0

This is my code. I'm trying to get the value accountId returned and assigned to $accountId but I always get 0 returned. The query is fine it return the value I want in PHP my admin but it doesn't return it here. Do you know why?
$mysqli = new mysqli($SQLhost, $SQLusername, $SQLpassword, $SQLdatabase);
$stmt = $mysqli->prepare("SELECT accountId FROM Account WHERE accountUsername=? AND accountPassword=?");
$stmt->bind_param('ss', $username,$password);
$stmt->execute();
$stmt->bind_result($accountId);
$_SESSION['accountId'] = $accountId;
The working code:
$mysqli = new mysqli($SQLhost, $SQLusername, $SQLpassword, $SQLdatabase);
$stmt = $mysqli->prepare("SELECT accountId FROM Account WHERE accountUsername=? AND accountPassword=?");
$stmt->bind_param('ss', $username,$password);
$stmt->execute();
$stmt->bind_result($accountID);
$stmt -> fetch();
$_SESSION['accountId'] = $accountID;

mysqli prepare - only works when not a function?

That one always returns a false bool:
<?php
function check($username, $db_conx) {
$sql = 'SELECT User_ID FROM tbl_user WHERE Username=?';
$stmt = $db_conx->prepare($sql);
$stmt->bind_param('s', $username);
$stmt->execute();
$ret= $stmt->get_result();
$stmt->close();
$db_conx->close();
$ret = $ret->fetch_row();
return $ret;
}
$usr = "root";
$res = check($u,$db_conx);
echo var_dump($res);
echo $a[0];
?>
I don't get it, they are pretty equal - so what's the error?
That one returns what I expected:
<?php
$usr = 'root';
$sql = "SELECT User_ID FROM tbl_user WHERE Username=?";
$stmt = $db_conx->prepare($sql);
$stmt->bind_param('s', $usr);
$stmt->execute();
$ret = $stmt->get_result();
$stmt->close();
$db_conx->close();
$ret = $ret->fetch_row();
echo var_dump($ret);
echo $ret[0];
?>
I want to recycle it over and over again with the function, but it doesn't seem to work. Is it even possible to set & execute the parameters in a function or have I just made a stupid mistake?

MySQLi Prepared Statement Query Issue

I'm relatively new to MySQLi prepared statements, and running into an error. Take this code:
$user = 'admin';
$pass = 'admin';
if ($stmt = $mysqli->query("SELECT * FROM members WHERE username='$user' AND password='$pass'"))
{
echo $stmt->num_rows;
}
This will display "1", as it should.
This next piece of code though, returns "0":
$user = 'admin';
$pass = 'admin';
if ($stmt = $mysqli->prepare("SELECT * FROM members WHERE username=? AND password=?"))
{
$stmt->bind_param("ss", $user, $pass);
$stmt->execute();
echo $stmt->num_rows;
}
Any ideas why?
you need to call store_result() before you get the number of rows
$user = 'admin';
$pass = 'admin';
if ($stmt = $mysqli->prepare("SELECT * FROM members WHERE username=? AND password=?"))
{
$stmt->bind_param("ss", $user, $pass);
$stmt->execute();
$stmt->store_result(); // add this line
echo $stmt->num_rows;
}

Categories