how to make function to delete data from db - php

can any body tell me that how can i make function of this query in PHP that's why whenever i want it i just call a function to delete record. i am new in coding. please suggest simplest code to write function for this.
<?php
if (isset($_GET['delete']))
{
$query= "DELETE FROM student WHERE id='".$_GET['delete']."'";
global $db;
$row = $db->exec($query);
}
$query= "SELECT * FROM student";
global $db;
$statement = $db->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$result = $result;
?>

Try this:
if (isset($_GET['delete']))
{
deleteStudent($_GET['delete']);
}
$query= "SELECT * FROM student";
global $db;
$statement = $db->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$result = $result;
function deleteStudent($id) {
global $db;
$query= "DELETE FROM student WHERE id='".$id."'";
$row = $db->exec($query);
return $row; // expects it return the succes/failure flags properly.
} ?>

Related

PHP MYSQLI Query is failing

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.

Count registered users in database

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();

PHP undefined variable mysqli connection

I have a mysql connection which is included in a separate file:
require 'settings.php';
and I have a file with all functions, also included:
require 'functions.php';
In the settings there it looks like this:
$db = mysqli_connect("host", "username", "passwort", "database");
if(!$db) {
exit("Error: ".mysqli_connect_error());
}
and a function uses this connection like this:
function includehomepage() {
$data = array();
$query = "SELECT pagecontent FROM `pages` WHERE `id` = `0`";
$query = mysqli_query($db, $query);
$data = mysqli_fetch_assoc($query);
return $data['pagecontent'];
}
But I get an error message like this:
Undefined variable: db in /var/... on line 18
Do you have an answer? The variable have to be defined in the included file.. I am confused. Thanks for your answers!
Variable scope problem. Look at global
function includehomepage() {
global $db;
$data = array();
$query = "SELECT pagecontent FROM `pages` WHERE `id` = '0'";
$query = mysqli_query($db, $query);
$data = mysqli_fetch_assoc($query);
return $data['pagecontent'];
}
$db is a global variable to includehomepage function. If you want to access it, then you have to pass it to the function or declare it as global inside the function.
like
function includehomepage() {
global $db;
$data = array();
$query = "SELECT pagecontent FROM `pages` WHERE `id` = '0'";
$query = mysqli_query($db, $query);
$data = mysqli_fetch_assoc($query);
return $data['pagecontent'];
}
or have it as a parameter in your function and pass it via call.
function includehomepage($db) {
$data = array();
$query = "SELECT pagecontent FROM `pages` WHERE `id` = '0'";
$query = mysqli_query($db, $query);
$data = mysqli_fetch_assoc($query);
return $data['pagecontent'];
}
includehomepage($db);

how to properly use while loop in PDO fetchAll

please be easy on me, i just started learning PDO and still finding my way how to convert my mysqli to PDO.
so i have a function to get the contents from my database
function getContent() {
$db = PDOconn();
$query = "SELECT * FROM posts ORDER BY id DESC LIMIT 0,3";
$sql = $db->prepare($sql);
$row = $sql->fetchAll(PDO::FETCH_ASSOC);
return $row;
}
normally when i return $row in mysqli, i would define fetch_assoc() in my while loop.
while ($row = $result->fetch_assoc()) {
$id = $row['id'];
$content = $row['content'];
}
Now, since (PDO::FETCH_ASSOC) is already declared in my function.
how would i properly create my while loop to print the values in PDO?
[edit]
updated code
i will be declaring my while loop outside of the function. so i need something to return from my function but i dont know what that is..
function getContent() {
$db = PDOconn();
$query = "SELECT * FROM posts ORDER BY id DESC LIMIT 0,3";
$sql = $db->prepare($query);
$row = $sql->execute();
return $row;
}
this is my while loop outside the function.
$sql = getContent();
while ($row = $sql->fetchAll(PDO::FETCH_ASSOC)) {
$id = $row['id'];
$content = $row['content'];
}
With fetchAll() you don't have to use while at all. As this function returns an array, you have to use foreach() instead:
function getContent() {
$db = PDOconn();
$query = "SELECT * FROM posts ORDER BY id DESC LIMIT 0,3";
$sql = $db->prepare($query);
$sql->execute();
return $sql->fetchAll();
}
$data = getContent();
foreach($data as $row) {
$id = $row['id'];
$content = $row['content'];
}

Error when I passed on values on function

Sorry about the last post I had. Here's my revision, please help me.
<?php
//connect database
$sql = "SELECT * FROM user where user_id = 8320 AND password = 'admin' ";
$query = pg_query($sql);
var_dump($row = pg_fetch_array($query)); //dumps correctly.
?>
BUT THE PROBLEM IS THIS..when I try to make it as a function LIKE:
function check($user_id, $password)
{
$sql = "SELECT * FROM user where user_id = $user_id AND password = '$password' ";
$query = pg_query($sql);
$row = pg_fetch_array($query);
return $row;
}
AND CALL IT HERE:
var_dump($data = check(8320, 'admin')); DUMPS NULL;
How come it ended up like this?
Its returning NULL because there is an error with your SQL query, and no results are being returned. You should do some error checking in your function, try this version:
function check($user_id, $password)
{
$dbconn = pg_connect("host=localhost dbname=test");
$sql = "SELECT * FROM user where user_id = $1 AND password = $2 ";
$result = pg_query_params($dbconn, $sql, array($user_id,$password));
$row = pg_fetch_array($result);
if (!$row) {
echo pg_last_error($dbconn);
} else {
return $row;
}
}
Try the code below. It should work fine for you.
$data = check(8320, 'admin');
var_dump($data);
Seems like your PostgreSQL resource is missing inside the function. You have two options.
Declare the connection resource inside the function using global.
Establish the connection inside the function.
This is the first option:
$conn = pg_connect('host','user','pass','db');
function check($user_id, $password)
{
global $conn;
$sql = "SELECT * FROM user where user_id = $user_id AND password = '$password' ";
$query = pg_query($conn, $sql);
$row = pg_fetch_array($query);
return $row;
}
And this is the second option:
function check($user_id, $password)
{
$conn = pg_connect('host','user','pass','db');
$sql = "SELECT * FROM user where user_id = $user_id AND password = '$password' ";
$query = pg_query($conn, $sql);
$row = pg_fetch_array($query);
return $row;
}
According to the PHP manual, You may omit connection resource, but it is not recommended, since it can be the cause of hard to find bugs in scripts.

Categories