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
Related
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:
Reference - What does this error mean in PHP?
(38 answers)
Closed 6 years ago.
<?php
$servername = "localhost";
$username = "user";
$password = "pass";
$dbname = "test";
$tablename = "mapcoords";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error)
{
echo "Failure";
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$sql = "SELECT (lat, lng) FROM mapcoords";
$result = $conn->query($sql);
while($row = $result->fetch_assoc())
{
echo "ok";
}
$conn->close();
?>
Here is the code. So like I said, it can connect successfully, but the code won't successfully query. What's weird is that if I copy and paste the same code, which seems to be EXACTLY the same, it works. It makes no sense. I can't find a single difference between their code and my code besides the way they space things and the way I space things. Here is their code:
<?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 (lat, lng) FROM mapcoords";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $row["lat"]. " " . $row["lng"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
The problem is that this query:
SELECT (lat, lng) FROM mapcoords
returns the folloowing error:
[21000][1241] Operand should contain 1 column(s)
You have to change the query to
SELECT lat, lng FROM mapcoords
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);
}
?>
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'";
This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 6 years ago.
i want to fetch a mysql table named "my_table" column named "Email" contents as an array by php , so this is my code :
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "my_table";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$result = mysql_query("SELECT * FROM my_table");
if ($result === FALSE) {
die(mysql_error()); // TODO: better error handling
}
$data = array();
while ($row = mysql_fetch_array($result)) {
$data[] = $row['Email'];
}
echo join($data, ',');
?>
but this code returns me this error :
No database selected
but i've selected my table and database ...
and i know this code have some problems as mixing mysql and mysqli content but i dont know how to fix it i just want that array echo , if this code need to be fixed just guid me ,
how to solve this problem ? thanks in advance
Thanks to #Martin
my problem has solved i just changed the code by this way :
<?php
$servername = "localhost"; $username = "root"; $password = ""; $dbname = "my_db";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname); // Check connection
if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error);}
$result = mysqli_query($conn, "SELECT * FROM my_table");
if($result === FALSE) {
die(mysql_error()); // TODO: better error handling
}
$data = array();
while ($row = mysqli_fetch_array($result))
{
$data[] = $row['Email'];
}
echo join($data, ',')
?>
You are conencting to a database here called "my_table":
$dbname = "my_table";
And then, in your SQL statement, you try connecting to a table called the same:
$result = mysql_query("SELECT * FROM my_table");
Are you sure this is the correct name for your database?
On PHPMyAdmin you can click "Databases" to view the Database names and then, when clicking on the db, it will give you a list of tables:
Image file of getting database views from tables in PHPMyAdmin