how to execute query with constant in php mysql - php

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

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.

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

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

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

PHP script returns no result

I have a script where I try to get the date from my database.
The script needs to show: The date in the database is (date). Click here to continue. When I run the SQL query in phpMyAdmin, the SQL query returns the date. When I run it in my script I get no result.
Here is my script:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "db";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "set lc_time_names = 'nl_NL';";
$sql = "SELECT date_format(date, '%e %M %Y') AS date FROM table WHERE id='1'";
$result = $conn->multi_query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "The date in the database is:";
echo " " . $row['date'] . ". ";
echo "Click here to continue.";
}
} else {
echo "0";
}
?>
When I run this script I get 0. When I change echo "0"; with echo " " . $row['date'] . ". "; I get a empty page.
What am I doing wrong? How can I fix this?
I had another edit that isn't approved...does this work?
Just splitting your queries/variables into 2 separate ones--only only worry that first value may not persist for you.
Like so:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "db";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "set lc_time_names = 'nl_NL'";
$sql2 = "SELECT date_format(date, '%e %M %Y') AS date FROM table WHERE id='1'";
$resultZ = $conn->query($sql);
$result = $conn->query($sql2);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "The date in the database is:";
echo " " . $row['date'] . ". ";
echo "Click here to continue.";
}
} else {
echo "0";
}
?>
You are using mysqli_query (which executes exactly ONE query) and thus your script is only ever getting to the set variable statement and ending at the semicolon.
Follow the advice below--the 2nd example shows using multi_query and setting the variable value just like you do in phpMyAdmin.
Try with:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "db";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT date_format(date, '%e %M %Y') AS date FROM table WHERE id='1'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "The date in the database is:";
echo " " . $row['date'] . ". ";
echo "Click here to continue.";
}
} else {
echo "0";
}
?>
Or use multi_query:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "db";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "set lc_time_names = 'nl_NL'; SELECT date_format(date, '%e %M %Y') AS date FROM table WHERE id='1'";
$result = $conn->multi_query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "The date in the database is:";
echo " " . $row['date'] . ". ";
echo "Click here to continue.";
}
} else {
echo "0";
}
?>
You have formatted the sql script.. So instead of writing $row['date'] just put like this $row[0]
Hope this works for you..

How do I import info from a database to my website? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Using the code below I am trying to import users table into my php page and only want to show the last ten entries in a table, what else do I need to add to my code to achieve this
<?php
$servername = "localhost";
$username = "";
$password = "";
$dbname = "";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM dispenses";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "Amount: " . $row["amount"];
}
} else {
echo "0 results";
}
$conn->close();
?>
$query = "SELECT * FROM 'users'";
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM users";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"];
}
} else {
echo "0 results";
}
$conn->close();
?>
This should help:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
http://www.w3schools.com/php/php_mysql_select.asp

Categories