This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 6 years ago.
I am facing trouble printing the details of a username from MYSQL. The quotes in WHERE name = "xxx" is the cause.
This is the code:
$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 name, email FROM MyTable WHERE name=$name";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["name"]. " - email: " . $row["email"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
How do I replace the WHERE name = $name?
you are missing an ''
should be:
WHERE name = '$name'
notice the quotes
I think you shoudl use this notation (previously be sure of a proper sanitize of $name for preventing potential SQL injection)
"SELECT name, email FROM MyTable WHERE name='$name'";
Related
This question already has an answer here:
PHP: 500 Error to error page
(1 answer)
Closed 5 years ago.
I want to select data from a MySQL database with PHP. Problem is, that when I try to echo out the $result, I get a 500 Error. When I leave out the echo $result;, I get a 200 OK return.
You guys gut any ideas?
Here's the PHP:
$q = $_GET['q'];
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "test";
//establish connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check Conncetion
if(!$conn) {
die("Connection failes: " . mysqli_connect_error());
}
$sql = "SELECT * FROM phptesting";
$result = mysqli_query($conn, $sql);
echo $result;
/*
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"]. "- Name: " . $row["first_name"]. " " . $row["last_name"]";
}
*/
mysqli_close($conn);
For your info, $q is just an integer with an id for testing it out.
echo is used to output primitive data type such as String, Integers.
$result holds the metadata of your SQL query result.
In ur code ur trying to echo the metadata. And This caused PHP fatal error.
This question already exists:
PHP's white screen of death [duplicate]
Closed 5 years ago.
I am always getting blank page when using Php and mysql connections
this is my php file
this is my html file
Try this code instead:
<?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();
?>
This question already has answers here:
How to increment value in MySQL with PHP mysqli
(4 answers)
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 5 years ago.
Ok so I am wondering if there is a simple way to make Addonetothis in my code to add it's current INT + 1 whenever this code is ran?
<?php
$server = "localhost";
$user = "**********";
$pass = "**********";
$dbname = "**********";
//Creating connection for mysqli
$conn = new mysqli($server, $user, $pass, $dbname);
//Checking connection
if ($conn->connect_error) {
die("Connection failed:" . $conn->connect_error);
}
$article_id = $_GET['id'];
if ( ! is_numeric($article_id))
die("Looks like you are lost! <a href='#'>Back to Home</a> ");
$sql = "UPDATE Example SET addonetothis='+ 1' WHERE `ID` =$article_id";
if ($conn->query($sql) === TRUE) {
header("Refresh:1; url=example.php?id=$article_id");
echo "Thank you!";
} else {
echo "Error" . $sql . "<br/>" . $conn->error;
}
$conn->close();
So you would have seen that...
$sql = "UPDATE Example SET Addonetothis='+ 1' WHERE `ID` =$article_id";
will blow up if Addonetothis is defined as an integer in your Database table as you are setting it to a string.
What you are looking for is something like...
$sql = "UPDATE Example SET AddOneToThis = AddOneToThis + 1 WHERE `ID` =$article_id";
This question already has answers here:
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed 5 years ago.
This code showing the output as :
string(59) "{"status":{"message":"error parsing
parameter","value":14}}"
but when instead of "file_get_contents" only "echo" is used, it shows correct url as output : http://ws.geonames.org/countryCodeJSON?lat=20.992&lng=73.314&username=****
what is going wrong here ?
<?php
$servername = "localhost";
$username = "*******";
$password = "*************";
$dbname = "id1116502_kk";
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//remove limit 1 is you want multiple data.
$sql = "SELECT degree_n, minute_n,degree_e, minute_e FROM coordinates ORDER BY id DESC limit 1 ";
$result = $conn->query($sql);
$deg_e = "";
$min_e = "";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$deg_n = $row["degree_n"];
$min_n = $row["minute_n"];
$deg_e = $row["degree_e"];
$min_e = $row["minute_e"];
$url = file_get_contents('http://ws.geonames.org/countryCodeJSON?lat=$deg_n.$min_n&lng=$deg_e.$min_e&username=****');
var_dump($url);
}
} else {
echo "0 results";
}
$conn->close();
?>
you are using single quotes in your file_get_contents
// change this to double quotes
$url = file_get_contents('http://ws.geonames.org/countryCodeJSON?lat=$deg_n.$min_n&lng=$deg_e.$min_e&username=krunal123');
for more info check this post
This question already has answers here:
JSON encode MySQL results
(16 answers)
Closed 6 years ago.
i have written code in php file that to connect to database and get the requested data .
<?php
// 1. Create a database connection
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "root";
$dbname = "widget_corp";
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
// Test if connection succeeded
if(mysqli_connect_errno()) {
die("Database connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
}
$query = "SELECT * ";
$query .= "FROM subjects ";
$query .= "WHERE visible = 1 ";
$query .= "ORDER BY position ASC";
$result = mysqli_query($connection, $query);
$subject = mysqli_fetch_assoc($result);
print_r($subject);
?>
i am getting the output in array format ,i want to get the out put in json format.how to change the code ,please help me .
You need to use php json_encode() method. Please check php doc for json_encode here
Use json_encode:
I suppose you're making API to return JSON string, make sure you return it as a JSON response instead of HTML. It's a good practice :)
To return JSON string as a JSON response, You can do the following.
<?php
header('Content-Type: application/json');
echo json_encode($subject);
?>
create a array or results and use json_encode.
$subject = array();
while($row = $result->fetch_assoc() ){
$subject[] = $row;
}
echo json_encode($subject);
You have to use json_encode($subject).
<?php
// 1. Create a database connection
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "root";
$dbname = "widget_corp";
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
// Test if connection succeeded
if(mysqli_connect_errno()) {
die("Database connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
}
$query = "SELECT * ";
$query .= "FROM subjects ";
$query .= "WHERE visible = 1 ";
$query .= "ORDER BY position ASC";
$result = mysqli_query($connection, $query);
$subject = mysqli_fetch_assoc($result);
if($subject)
{
$json_subject = json_encode($subject);
print_r($json_subject);
}
?>