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.
Related
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
}
i have created delete statement in php pdo. i need to know bind_param query for my code. I used one bind_param which is not wrking.
PHP7
public function deleteRecord($table,$pk,$id){
if($table == "categories"){
$pre_stmt = $this->con->prepare("SELECT ".$id." FROM categories WHERE parent_cat = ?");
$pre_stmt->bind_param("i",$id);
$pre_stmt->execute();
$result = $pre_stmt->get_result() or die($this->con->error);
if ($result->num_rows > 0) {
return "DEPENDENT_CATEGORY";
}else{
$pre_stmt = $this->con->prepare("DELETE FROM ".$table." WHERE ".$pk." = ?");
$pre_stmt->bind_param("i",$id);
$result = $pre_stmt->execute() or die($this->con->error);
if ($result) {
return "CATEGORY_DELETED";
}
}
I rectified that above and got error on below
public function getAllRecord($table){
$result = $this->con->prepare("SELECT * FROM ".$table);
$result->execute();
$results = $result->fetchAll();
$rows = array();
if ($results->rowCount > 0) {
while($row = $results->fetch(PDO::FETCH_ASSOC)){
$rows[] = $row;
}
return $rows;
}
return "NO_DATA";
}
Error:Notice: Trying to get property of non-object in C:\xampp\htdocs\inv_project\public_html\includes\DBOperation.php on line 68
I'm trying to echo the results of a query. When I run the query in the phpmyadmin SQL box, it returns the right information. When I add it to php, it doesn't echo anything if I add a second condition such as AND
Doesn't return anything
$query = "SELECT * FROM CwAp_postmeta WHERE meta_key = 'adurl' AND post_id = '31'";
mysqli_query($db, $query) or die('Error querying database.');
$result = mysqli_query($db, $query);
$row = mysqli_fetch_array($result);
while ($row = mysqli_fetch_array($result)) {
echo $row['meta_value'];
}
Does return something
$query = "SELECT * FROM CwAp_postmeta WHERE meta_key = 'adurl'";
mysqli_query($db, $query) or die('Error querying database.');
$result = mysqli_query($db, $query);
$row = mysqli_fetch_array($result);
while ($row = mysqli_fetch_array($result)) {
echo $row['meta_value'];
}
I am having some trouble getting some values from my sql query in php
This is my php code:
<?php
include "init.php";
$stmt = "SELECT email FROM Customer ";
$result = $dbcon -> prepare($stmt);
$result->execute();
$result->store_result();
while($result -> fetch()){
// This line below works
echo "Works";
// This doesn't work
echo $result['email'];
}
?>
You need to use bind_result() to retrieve the values returned by the query.
include "init.php";
$stmt = "SELECT email FROM Customer ";
$result = $dbcon -> prepare($stmt);
$result->execute();
$result->store_result();
$result->bind_result($email);
while($result -> fetch()){
// This line below works
echo "Works";
echo $email;
}
To start, mysqli::prepare doesn't return a mysqli_result, it returns a mysqli_stmt. You then have to execute that mysqli_stmt and either fetch directly from it into a bound variable, or extract a mysqli_result and fetch from that. There are several ways to do what you want:
$qry = "SELECT email FROM Customer ";
$stmt = $dbcon->prepare($qry);
$stmt->execute();
$stmt->bind_result($email); // Bind variable to statement
while($stmt->fetch()) // Iterate through statement
{
// This line below works
echo "Works";
echo $email;
}
Or:
$qry = "SELECT email FROM Customer ";
$stmt = $dbcon->prepare($qry);
$stmt->execute();
$rslt = $stmt->get_result(); // Retrieve result set
while($row = $rslt->fetch_assoc()) // Iterate through result
{
// This line below works
echo "Works";
echo $row['email'];
}
Or:
$qry = "SELECT email FROM Customer ";
$stmt = $dbcon->prepare($qry);
$stmt->execute();
$rslt = $stmt->get_result(); // Retrieve result
$resArray = $rslt->fetch_all(MYSQLI_ASSOC)); // Convert to array
foreach ($resArray as $row) // Iterate through array
{
// This line below works
echo "Works";
echo $row['email'];
}
As is my custom, I leave error handling as an exercise for the reader.
I have 2 rows of data in my database and I am trying to fetch all rows
$query = mysqli_query($connection, "select username, email, is_admin from adminUsers");
$results = mysqli_fetch_assoc($query);
I have tried mysqli_fetch_assoc, mysqli_fetch_row, mysqli_fetch_array all of them just return 1 row of data.
mysqli_fetch_*() functions except mysqli_fetch_all(), if supported, fetch one row. Loop and fetch:
while($row = mysqli_fetch_assoc($query)) {
print_r($row);
//or you can echo $row['username'] etc...
//or store in an array to loop through later
$rows[] = $row;
}
If you use mysqlnd there is a mysqli_fetch_all() or use:
if(!function_exists('mysqli_fetch_all')) {
function mysqli_fetch_all($result, $resulttype=MYSQLI_BOTH) {
while($row = mysqli_fetch_array($result, $resulttype)) {
$rows[] =$row;
}
return $rows;
}
}
$results = mysqli_fetch_all($query);
But then you have to loop through all the returned rows anyway:
foreach($results as $row) {
print_r($row);
//or you can echo $row['username'] etc...
}
Switch to PDO to do it in a single go... Use PDOStatement::fetchAll
<?php
//.. Do the neccessary PDO connections...
$sth = $dbh->prepare("select username, email, is_admin from adminUsers");
$sth->execute();
$result = $sth->fetchAll();
print_r($result);
(or)
You need to loop through them... [MySQLi]
$query = mysqli_query($connection, "select username, email, is_admin from adminUsers");
$res = array();
while($results = mysqli_fetch_assoc($query))
{
$res[] = $results;
}
print_r($res);
Use while loop for fetching all lines:
while ($result = mysqli_fetch_assoc($query))
{
// Rest of your code. See an example below
echo $result['username'];
}