I attempt to get the result of a very simple query with the function query but nothing appears. If I execute the query in PHPMyAdmin, with the same data, I have a result.
There is my code :
$sql = "SELECT * FROM users WHERE email='$email'";
$response = $conn->query($conn, $sql);
The $conn variable is correct, I did an Insert with that.
$response is null. I can do an echo and there is nothing.
What can I do to solve this problem ? What can I check ?
Thank you very much.
You don't need to pass connection in query.
Solution:
$sql = "SELECT * FROM users WHERE email='$email'";
$response = $conn->query($sql);
while($res = $response->fetch_array()){
$name=$res['nameofuser']; //just an example
}
echo $name;
Real solution (prepare stmt):
$sql = "SELECT * FROM users WHERE email=?";
$response = $conn->prepare($sql);
$response->bind_param('s',$email);
if(!$response->execute()){
echo "Error query: " . $response->error . ".";
}
$result=$response->get_result();
while($res = $result->fetch_array()){
$name=$res['nameofuser']; //just an example
}
echo $name;
'Tips' add to real solution check if query is done.
After execute query . fetch the results
$stmt = $conn->prepare( "SELECT * FROM users WHERE email= ? ");
$stmt->bind_param("s", $email);
$stmt->execute();
$result = $stmt->get_result();
if($result->num_rows === 0) exit('No rows');
while($row = $result->fetch_assoc()) {
// your code
}
Related
I would like to echo the number of people registered on my website
only the code that I have does not work, it gives me back that it can't be
converted to string. Also when I make it a function to call in my HTML I get error that $connection is undefined
require_once("connect.php");
$sql = "SELECT * FROM persons";
if ($result=mysqli_query($connection, $sql)){
$rowcount = mysqli_num_rows($result);
mysqli_free_result($result);
return $result;}
How do I get this in a function that I can call on my page that prints the number of people registered?
First of all you should use count because of speed issues:
$sql = "SELECT COUNT(id) FROM persons";
To write a function that returns the number, you can do something like
function registredMemberCount ($connection)
{
$sql = "SELECT COUNT(id) FROM persons";
$result = mysqli_query($connection,$sql);
$rows = mysqli_fetch_row($result);
return $rows[0];
}
and call it with
registredMemberCount($connection);
require_once("connect.php");
function blah()
{
global $connection;
$sql = "SELECT COUNT(*) FROM persons";
if ($result=mysqli_query($connection, $sql)){
$row= mysqli_fetch_array($result);
$rowcount = $row[0];
mysqli_free_result($result);
}
return $rowcount;
}
echo blah();
Lets see this,
require('connect.php');
function total_num_users(){
$sql = "SELECT * FROM persons";
$result = mysqli_query($connection,$sql);
$count = mysqli_num_rows($result);
return $count;
}
And you can call and echo it like this.
echo total_num_users();
When trying to run a MYSQLI command in PHP, Its coming back failing.
function DB_query($query, $params = []) {
$conn = DB_connect();
if ($params)
{
$stmt = $conn->prepare($query);
$types = str_repeat('s', count($params));
$stmt->bind_param($types, ...$params);
$stmt->execute();
$result = $stmt->get_result();
} else {
$result = mysqli_query($conn, $query);
}
if ($result)
{
$result = mysqli_fetch_all($result);
return $result;
} else {
return mysqli_affected_rows($conn);
}
}
Here is my query:
DB_query("SELECT count(*) FROM members WHERE email = ? LIMIT 1",[$email])
It runs on:
$result = mysqli_fetch_all($result);
The results from $result: array(1)([0]=>array(1)([0]=>(int)1))
Here is my query:
DB_query("SELECT id, username, password FROM members WHERE email = ? LIMIT 1",[$email]);
It runs on:
$result = mysqli_fetch_all($result);
The results from $result: > array(0)
I have tried changing out the "mysqli_fetch_all" to fetch_all, but I cant figure it out. I need both query to run through the same function.
I cant figure out why the last query is returning nothing in the array.
<?php
$sql = "SELECT * FROM members WHERE email = '".$email."'";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_row($result)) {
echo $row[0];
echo $row[1];
echo $row[2];
}
?>
Alternatively, you can use mysqli_fetch_array and use $row['fieldname'] inside the loop.
I've tried to find any reference to display data where no data in table for an hours, but I'm not yet found a code to fix the issue
I've this code:
//function
function readAll(){
$query = "SELECT * FROM ".$this->table_name." ORDER BY id_nilai ASC";
$stmt = $this->conn->prepare( $query );
$stmt->execute();
return $stmt;
}
//execute
$pro3 = new Nilai($db);
$stmt3 = $pro3->readAll();
while ($row3 = $stmt3->fetch(PDO::FETCH_ASSOC)){
// not yet fix how to display no data
//if($row3==false){ tried to change $row3==0 still won't work
//echo "No Data";
//}
//die(var_dump($row3)); showing `bool(false)`
echo $row3['ket_nilai'] (echo $row3['jum_nilai'])
}
any idea how to do?
I want to display some text when no data found in database table
You need to fetch the results before entering the loop, check if you do get the results then if you do enter the loop else display the message
see bellow code :
<?php
//function
function readAll(){
$query = "SELECT * FROM ".$this->table_name." ORDER BY id_nilai ASC";
$stmt = $this->conn->prepare( $query );
$stmt->execute();
return $stmt;
}
//execute
$pro3 = new Nilai($db);
$stmt3 = $pro3->readAll();
$results = $stmt3->fetchall(PDO::FETCH_ASSOC);
if(count($results) > 0){
foreach($results as $row3){
//display them
echo $row3['ket_nilai'] ;
echo $row3['jum_nilai'];
}
}else{
echo "No data available";
}
?>
I would like to echo the number of people registered on my website
only the code that I have does not work, it gives me back that it can't be
converted to string. Also when I make it a function to call in my HTML I get error that $connection is undefined
require_once("connect.php");
$sql = "SELECT * FROM persons";
if ($result=mysqli_query($connection, $sql)){
$rowcount = mysqli_num_rows($result);
mysqli_free_result($result);
return $result;}
How do I get this in a function that I can call on my page that prints the number of people registered?
First of all you should use count because of speed issues:
$sql = "SELECT COUNT(id) FROM persons";
To write a function that returns the number, you can do something like
function registredMemberCount ($connection)
{
$sql = "SELECT COUNT(id) FROM persons";
$result = mysqli_query($connection,$sql);
$rows = mysqli_fetch_row($result);
return $rows[0];
}
and call it with
registredMemberCount($connection);
require_once("connect.php");
function blah()
{
global $connection;
$sql = "SELECT COUNT(*) FROM persons";
if ($result=mysqli_query($connection, $sql)){
$row= mysqli_fetch_array($result);
$rowcount = $row[0];
mysqli_free_result($result);
}
return $rowcount;
}
echo blah();
Lets see this,
require('connect.php');
function total_num_users(){
$sql = "SELECT * FROM persons";
$result = mysqli_query($connection,$sql);
$count = mysqli_num_rows($result);
return $count;
}
And you can call and echo it like this.
echo total_num_users();
So I'm making a usergroup function that allows me to block off pages to lower user levels. This is my function for grabbing info:
function grab_info($id, $requested_info){
$id = $_SESSION['user_id'];
$requested_info = $requested_info;
$con = new mysqli('localhost', 'root', '', 'login');
if ($con->connect_errno >0){
die("Handle your connection error here");
}
$sql = "SELECT * FROM `users` WHERE `id` = $id";
if (!$result = $con->query($sql)) {
die("There as a query error for some reason handle your query error");
}
while($row = $result-fetch_assoc()){
$info = $row[$requested_info];
return $info;
}
}
Right here:
$sql = "SELECT * FROM `users` WHERE `id` = $id";
if (!$result = $con->query($sql)) {
die("There as a query error for some reason handle your query error");
}
is where something is going wrong. This is my method for grabbing the info:
$id = $_SESSION['user_id'];
$rank = grab_info($id, 'rank');//Gets rank from our id
$meets = can_access($rank, 4, true);//We're saying our user has a rank of 1 to access this page you need a rank of 3 and only 3 hence strict
if ($meets == false){//user cant access page
header("Location: index.php");
die();
}
Basically, it just keeps giving me the "There as a query error for some reason handle your query error" and I'm stuck. New to php so sorry if it's messy.
Using prepared statements and cast the variable as an integer.
$stmt = $con->prepare("SELECT * FROM `users` WHERE `id` = ?");
$stmt->bind_param("i",$id);
$id = (int) $_SESSION['user_id'];
$stmt->execute();
$result = $stmt->get_result();
Check to make sure that $id is actually set. If it's null that will cause your query to explode.
$sql = "SELECT * FROM `users` WHERE `id`='{$id}'";
Try this :)
$query=mysql_query("SELECT * FROM user WHERE user_email='$user_email');
Please try this:
function grab_info($id, $requested_info){
$id = $_SESSION['user_id'];
$requested_info = $requested_info;
$con = new mysqli('localhost', 'root', '', 'login');
if ($con->connect_errno >0){
die("Handle your connection error here");
}
$sql = "SELECT * FROM users WHERE id =". $id;
if (!$result = $con->query($sql)) {
die("There as a query error for some reason handle your query error");
}
while($row = $result->fetch_assoc()){
$info = $row;
return $info;
}
}