Trying to write a function that displays data in an SQL database - php

Trying to write a function that displays the id, and filepath of all public content in an SQL database. Here's what I have so far, but it failed to run.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "pricosha";
// Create connection
$connection = mysql_connect($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//Added two extra variables, username and content_name
$query = "SELECT id, username, file_path, content_name FROM Content";
$result = mysql_query($query);
//Loop through the results of the query
while($row = mysql_fetch_array($result)){
echo "ID: " . $row["id"]. " Username: " . $row["username"]. " File Path: " . $row["file_path"]. " Content Name: " . $row["content_name"]. "<br>";
}
$conn->close();
?>

Your variable for create the connection to mysql is $connection, while you use $conn for this :
$conn->connect_error
and this :
$conn->close();
Now try to change $conn to $connection or just change this :
$connection = mysql_connect($servername, $username, $password, $dbname);
to this :
$conn = mysql_connect($servername, $username, $password, $dbname);
So far now, the function mysql_connect, mysql_query, and others have been deprecated. You need to change it to mysqli I think. Try this.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "pricosha";
$connection = new mysqli($servername, $username, $password, $dbname) or die(mysqli_errno());
$query = "SELECT id, username, file_path, content_name FROM Content";
$result = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc($result)){
echo "ID: " . $row["id"]. " Username: " . $row["username"]. " File Path: " . $row["file_path"]. " Content Name: " . $row["content_name"]. "<br>";
}
?>

Try this
$conn = mysql_connect($servername, $username, $password, $dbname);

Related

PHP mysql insert not working without error check

My table has 2 fields a primary with auto increment and then entry_id
why does the below not insert in the table:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "ecolog";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$entry_id = 5;
$sql = "INSERT INTO orders (entry_id) VALUE ('$entry_id')";
$conn->close();
?>
but when adding a error check it does?
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "ecolog";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$entry_id = 5;
$sql = "INSERT INTO orders (entry_id) VALUE ('$entry_id')";
if ($conn->query($sql) === TRUE) {
echo "";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
cant get my head around why this would be happening.

how to execute query with constant in php mysql

while executing query got this error "Error updating record: You have an error in your SQL syntax"
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SET #a:=0;UPDATE registrations SET EXIBIT_NO=#a:=#a+1 ORDER BY GR_ID";
You can wrtie:
$result=mysqli_query($conn,$sql);
Code Example:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>

Need help to display data from mysql database

I would need some help with showing data that I have on my database but I can't seen to be able to.`
$servername = "servername";
$username = "username";
$password = "password";
$dbname = "dbname";
$connect = mysqli_connect($servername, $username, $password, $dbname) or die ("connection failed");
//Query
$query = "SELECT * FROM 'Students'";
mysqli_master_query($dbname, $query) or die ("Error while Query");
$result = mysqli_master_query($dbname, $query);
$row = mysql_fetch_array($result);
while ($row = mysql_fetch_array($result)) {
echo "<p>".$row['Name']."</p>";
};
mysql_close($connect);
?>`
I am pretty new to this so I could have missed something simple. Any help appreciated.
Below is a sample code of the normal procedure to connect to a database and to select data from it. Please follow this type of coding since MySQL is now deprecated and MySQLi is used.
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
For further reference check out http://php.net/manual/en/book.mysqli.php and also https://www.w3schools.com/php/php_mysql_insert.asp

Cannot execute database statement from another php file using include

What I'm doing wrong after adding class define to StoreUser.php file and calling it from store.php file because it doesn't work any moore? It worked fine inside file StoreUser.php without class define and function see below.
$dbhost = "localhost";
$dbname = "riskinarviointilomake";
$dbuser = "root";
$dbpass = "";
//private $email;
if(isset($_GET["email"])){
$conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if (!( $stmt = $conn->prepare("INSERT INTO users(unique_id,email,
encrypted_password) VALUES(?,?,?)"))){
echo "Table creation failed: (" . $conn->errno . ") " . $conn->error;
}
$stmt->bind_param('sss', $unique_id,$email,$password);
$unique_id=123457764;
$password="df12467hh";
$result = $stmt->execute();
$stmt->close();
$conn->close();
}
?>
This doesn't work
class StoreUser{
$dbhost = "localhost";
$dbname = "riskinarviointilomake";
$dbuser = "root";
$dbpass = "";
//private $email;
public function store($email){
$conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if (!( $stmt = $conn->prepare("INSERT INTO users(unique_id,email, encrypted_password) VALUES(?,?,?)"))){
echo "Table creation failed: (" . $conn->errno . ") " . $conn->error;
}
$stmt->bind_param('sss', $unique_id,$email,$password);
$unique_id=123457764;
$password="df12467hh";
$result = $stmt->execute();
$stmt->close();
$conn->close();
}
}
<?php
include 'StoreTest.php';
if(isset($_GET['email'])){
$email = $_GET['email'];
$store = new StoreTest();
$store->store($email);
}
?>

mysql will not insert past fourth row

I'm not exactly sure what happened but this database and the php effecting it were working just fine until it hit the fourth row and now it won't insert new records at all.
if($_POST)
{
$servername = ******;
$username = ******;
$password = ******;
$db = ******;
$conn = mysqli_connect($servername, $username, $password, $db);
mysqli_select_db($conn,$db);
$uuid = $_POST['uuid'];
$sql = "INSERT INTO uuid VALUES ('$uuid');";
mysqli_query($conn,$sql);
mysqli_close($conn);
}
I'm not sure what happened but this is the relevant code for the mysqli query.
try this
<?php
if(isset($_POST['uuid']))
{
$servername = yourServerName;
$username = username;
$password = password;
$dbname = databaseName;
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$uuid = $_POST['uuid'];
$sql = "INSERT INTO tableName (columnName) VALUES ('$uuid')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
}
?>
Also, I recommend using prepared statements.

Categories