Deleting the row in php - php

I have create a database (student) and table(details) in SQL, Fields are Name,Class,Section,Address,Mobile. I need delete the Selva row in that table,I can't delete it please explain!!
PHP code-
<?php
$connection = mysql_connect('localhost', 'root','');
if (!$connection)
{
die("Database Connection Failed" . mysql_error());
}
$select_db = mysql_select_db( "student",$connection);
if (!$select_db)
{
die("Database Selection Failed" . mysql_error());
}
?>
<?php
if(isset($_GET['did'])) {
$delete_id = mysql_real_escape_string($_GET['did']);
$sql = mysql_query("DELETE FROM details WHERE Name = 'Selva' ");
if($sql) {
header("Location: table1.php");
} else {
echo "ERROR";
}
}
?>
Please explain ! how to delete the row in a table from database.

Your mysql query is proper but you need to check below condition is satisfied or not.
if(isset($_GET['did'])) {
echo 'debug test';exit;
}

Related

Why does mysqli() not recognize my database name?

I'm new to PHP and MySQL. While learning, I got this error that says my database is unkown.
I have already made this database.
and I have already made the table 'todos'
Here is my PHP
<?php
require 'functions.php';
$conn = new mysqli('localhost','root','', 'mytodo');
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$statement = $conn->prepare('select * from todos');
$statement->execute();
var_dump($statement->fetchAll());
require 'index.view.php';
the PHP file is named 'Index.test.php'
but when I try to access localhost/Index.test.php on my browser
it returns this
Could you tell me why I am getting the error? Appreciate the help!
try to use this select:
<?php
require 'functions.php';
$conn = new mysqli('localhost','root','', 'mytodo');
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//select query
$sql = "SELECT * FROM todos";
if ($result = mysqli_query($conn, $sql)) {
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_array($result)) {
echo $row['your column name'];
}
//free result set
mysqli_free_result($result);
} else {
echo "no records found";
}
}
else {
echo "error: could not connect" . mysqli_error($conn);
}
require 'index.view.php';
?>

Connect MYSQL using php and creating Database if not exist

Im trying to learn php with best methods and practice but im not able to get proper output as per the code as there is no error!
is it a good method to create the database if not exist because im not getting output echo "Database $dbname created successfully\n";
<?php
$user = "root";
$pwd = "";
$server = "localhost";
$dbname = "xyz";
//Connecting to MYSQL
$db_conn = mysqli_connect($server,$user,$pwd);
if (!$db_conn) {
die("Connection Error".mysqli_error());
}
echo "Connected Successfully";
$db_select = mysqli_select_db($db_conn,$dbname);
if (!$db_select) {
// If we couldn't, then it either doesn't exist, or we can't see it.
//Create Database Query
$db_create = "CREATE DATABASE $dbname";
$db_selected = mysqli_query($db_conn,$db_create);
if ($db_selected) {
echo "Database $dbname created successfully\n";
mysqli_select_db($db_conn,$dbname);
} else {
echo 'Error creating database: ' . mysql_error() . "\n";
}
}
?>
i got a solution out of it !
please check weather its a good method and best practice to add database !
$db_conn = mysqli_connect($server,$user,$pwd);
if (!$db_conn) {
die("Connection Error".mysqli_error());
}
echo "Connected Successfully";
$db_select = mysqli_select_db($db_conn,$dbname);
if (!$db_select) {
// If we couldn't, then it either doesn't exist, or we can't see it.
//Create Database Query
$db_create = "CREATE DATABASE IF NOT EXISTS $dbname";
$db_selected = mysqli_query($db_conn,$db_create);
if ($db_selected) {
echo "Database $dbname created successfully\n";
} else {
echo 'Error creating database: ' . mysqli_error() . "\n";
}
}
else{
echo "Database $dbname is connected";
}

Select SQL query is not working in PHP

I am having trouble with an SQL query that I have inserted into a piece of PHP code to retrieve some data. The query itself works perfectly within SQL. I am using the following PHP script.
I have the following objectives:
Connect to the existing database. This part works well.
Get data from the column 'Brand' of the table 'Transport' in $sql. This part is not working at this stage. echo ($sql) returns SELECT Brand FROM Transport WHERE Type = 'car'
Could you please let me know if you see the solution to this issue and if the remaining part of the code is correct. This is my f_sqlConnect()
function f_sqlConnect() {
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!link) {
die('Could not connect: '.mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected) {
die('Can not use'.DB_NAME.
': '.mysql_error());
}
}
/*This function cleans up the array to protect against injection attacks */
function f_clean($array) {
return array_map('mysql_real_escape_string', $array);
}
<?php
// Create connection
$link = f_sqlConnect();
// Getting data from the column Brand of the table Transport
$sql = "SELECT Brand FROM Transport WHERE Type = 'car'";
$result = $link->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "Brand: " . $row["Brand"]. "<br>";
}
} else {
echo "0 results";
}
$link->close();
?>
Here is the code, without seeing your f_sqlConnect(); mothod. This method should return connection string for DB in your case. But you can use following code this must work.
<?php
$servername = "Your_db_host";
$username = "your_db_username";
$password = "your_db_password";
$dbname = "your_DB_name";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Brand FROM Transport WHERE Type = 'car'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "Brand: " . $row["Brand"];
}
} else {
echo "0 results";
}
$conn->close();
?>
NOTE: Object oriented way of mysqli, You can use procedural way too to connect and get data.

php my admin: no database selected

I am trying to learn php and mysql. So i tried making a database using phpmyadmin and connect it with my php.
Here is a simple example where I try to see if the database is working
<?php
$connection = mysql_connect("localhost","root");
if(!$connection) {
die("Database connection failed: " . mysql_error());
$db_select = mysql_select_db("nameofdatabase",$connection);
if (!$db_select) {
die("Database selection failed:: " . mysql_error());
}
}
?>
<html>
<head>
<title>Databases</title>
</head>
<body>
<?php
$result = mysql_query("SELECT * FROM nameofdatabasetable", $connection);
if (!$result) {
die("Database query failed::: " . mysql_error());
}
while ($row = mysql_fetch_array($result)) {
echo $row[1];
}
?>
</body>
</html>
<?php
mysql_close($connection);
?>
and i get
Database query failed::: No database selected
which means than this part of code
<?php
$result = mysql_query("SELECT * FROM users", $connection);
if (!$result) {
die("Database query failed::: " . mysql_error());
}
while ($row = mysql_fetch_array($result)) {
echo $row[1];
}
?>
is not working (i put a different number of these ":" in each if.
Any help would be appreciated! Thank you!
The logic for your code doesn't make sense because if the connection doesn't happen then you would not be able to select a database and your database select statement is within the logic for if you cannot connect to the database. Try this instead:
$connection = mysql_connect("localhost","root");
if(!$connection) {
die("Database connection failed: " . mysql_error());
}else{
$db_select = mysql_select_db("nameofdatabase",$connection);
if (!$db_select) {
die("Database selection failed:: " . mysql_error());
}
}
I have to pass the username and password in the mysql_connect call. Here is my database open() function.
$this->con_error = "";
$db_con= mysql_connect($this->server, $this->username, $this->password);
if (!$db_con)
{
$this->con_error = mysql_error();
return false;
}
if(!mysql_select_db($this->database))
{
$this->con_error = mysql_error();
return false;
}
return $db_con;
die("Database connection failed: " . mysql_error());
$db_select = mysql_select_db("nameofdatabase",$connection);
mysql_select_db cannot possibly run here. It's only called after die.
$connection = mysql_connect("localhost","root");
if(!$connection) {
die("Database connection failed: " . mysql_error());
}
$db_select = mysql_select_db("nameofdatabase",$connection);
if (!$db_select) {
die("Database selection failed:: " . mysql_error());
}
Also it would be worth checking if you entered a password to your server.
$connection = mysql_connect("localhost","root"); (Missing Password)
The following code will trap any other error that might be happening and like the answer below this will provide mysql conectivity errors in case there is any.
try
{
$connection = mysql_connect("localhost","root", "password");
if(!$connection) {
die("Database connection failed: " . mysql_error());
}
$db_select = mysql_select_db("nameofdatabase",$connection);
if (!$db_select) {
die("Database selection failed:: " . mysql_error());
}
}catch (Exception $e){
error_log(" DB Error: ".$e->getMessage());
}

Php mysql create database if not exists

I want to create a database. Why is not the db created with this code?
$dbname = 'regulations_db';
$con = mysql_connect("localhost","root","pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
if (mysql_num_rows(mysql_query("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '". $dbname ."'"))) {
echo "Database $dbname already exists.";
}
else {
mysql_query("CREATE DATABASE '". $dbname ."'",$con);
echo "Database $dbname created.";
}
This is working, but I think the first one is the best practice:
if (mysql_query("CREATE DATABASE IF NOT EXISTS regulations_db",$con))
{
echo "Database created";
}
else
{
echo "Error creating database: " . mysql_error();
}
Just do a simple mysql_select_db() and if the result is false then proceed with the creation.
As an example, check out the first answer here by another very smart StackOverflower.
<?php
// Connect to MySQL
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
// Make my_db the current database
$db_selected = mysql_select_db('my_db', $link);
if (!$db_selected) {
// If we couldn't, then it either doesn't exist, or we can't see it.
$sql = 'CREATE DATABASE my_db';
if (mysql_query($sql, $link)) {
echo "Database my_db created successfully\n";
} else {
echo 'Error creating database: ' . mysql_error() . "\n";
}
}
mysql_close($link);
?>
Three steps to fix this:
Don’t specify the database name when connecting.
Your SQL statement should be CREATE DATABASE IF NOT EXISTS php1.
Call mysqli_select_db($link, 'php1') to make that the default database for your connection.
If you're using MySQLi Object-oriented method, you can use following code, this code is similar to previous answer and only the method is different, I just put this because if anyone using MySQLi Object-oriented method, you can use this code directly.
$servername = "localhost";
$username = "mysql_user";
$password = "user_password";
$dbName = "databaseName";
// Connect to MySQL
$conn = new mysqli($servername, $username, $password);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// If database is not exist create one
if (!mysqli_select_db($conn,$dbName)){
$sql = "CREATE DATABASE ".$dbName;
if ($conn->query($sql) === TRUE) {
echo "Database created successfully";
}else {
echo "Error creating database: " . $conn->error;
}
}
Furthermore you can refer W3school site here.
Good Luck! :D

Categories