Is it possible to somehow use list and mysqli_fetch_row together? The code below doesn't work.
$query = "SELECT id, email FROM emails WHERE id='24'";
$result = mysqli_query($link, $query);
list($get_id, $get_email) = mysqli_fetch_row($result);
This code however works, but I want a code that is less lines:
$query = "SELECT id, email FROM emails WHERE id='24'";
$result = mysqli_query($link, $query);
$row = mysqli_fetch_row($result);
$get_id = $row[0];
$get_email = $row[1];
You can use the list() like that.
So there must be an error in your query or somewhere else in your code, so add this code to be sure if there is an error and if so what it is.
error_reporting(E_ALL);
ini_set('display_errors', 1);
$query = "SELECT id, email FROM emails WHERE id='24'";
$result = mysqli_query($link, $query);
if ( ! $result ) {
echo mysqli_error($link);
exit;
}
list($get_id, $get_email) = mysqli_fetch_row($result);
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();
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'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'];
}
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 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();