I got the json data false. [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
Here Is my PHP code to encode the JSON data from MySQL database. And this is my URL http://fwtest.ga/appoint.php; I tested on the online website JSONLint to validate my JSON data. It is valid, but I got the result [false] instead of the data in JSON format. Can anybody tell what am I doing wrong?
<?php
$host = "my_host";
$user = "user";
$password = "pass";
$db = "db_name";
$con = mysqli_connect($host, $user, $password, $db);
$sql = "select time, date from table_name;";
$result = mysqli_query($con, $sql)
or die("Error: ".mysqli_error($con));
$response = array();
while ($row = mysqli_fetch_array($result))
{
array_push($response, array("time" >= $row[1], "date" >= $row[2]));
}
echo json_encode(array("server_response">= $response));
echo (json_last_error()=== JSON_ERROR_UTF8);
mysqli_close($con)
?>

Probably because you're returning a single boolean here:
json_encode(array("server_response">= $response));
↑
That's not the array operator.

Related

Mysqli_Affected_rows always return 0 [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 months ago.
Improve this question
Hi i am trying to use mysqli_affected_rows function but it always return 0 can someone help. Trying to get it done theu MSQLI OOP.
<?php
$servername ="localhost";
$username ="root";
$password ="testdb";
$database ="mydb";
$conn = new mysqli($servername, $username, $password, $database);
if($conn->connect_error)
{
die ("The Database connection is not established : " .connect_error);
}
echo "connection established";
//editing record
$sql_update = "update mytbl SET fname='Nitin Sharma' where sr=2";
echo "The affected Rows :" .mysqli_affected_rows($conn);
$conn->close();
?>
Table Values:
You missed to execute your SQL query.
$conn->query($sql_update);
You need to first execute your query by mysqli_query
$sql_update = "update mytbl SET fname='Nitin Sharma' where sr=2";
$conn->mysqli_query($sql_update);
echo "The affected Rows :" .$conn->affected_rows;

print a column from Mysql database [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
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.
Improve this question
I am trying to print out one column called Language1 from my Table that is called Mull, in a database called v6e.
At the moment i am getting a blank white screen.
<?php
session_start();
$servername = "localhost";
$user = "xxxx";
$password = "xxxx";
$dbname = "v6e";
// Create connection
$conn = mysqli_connect($servername, $user, $password, $dbname);
// Check connection
if (!$conn)
{
die("Connection failed: " . mysqli_connect_error());
}
else
{
$query = "SELECT Language1 FROM Mull WHERE username = 'Mull'";
$result = mysqli_query($query);
$row = mysqli_fetch_arrary($result);
echo $row['Language1'];
}
mysqli_close($conn);
?>
You have a typo issue. Change the line:
$row = mysqli_fetch_arrary($result);
With:
$row = mysqli_fetch_array($result);
Plus, you're also not connecting to DB with your query
$result = mysqli_query($conn, $query);
Reference:
http://php.net/manual/en/mysqli.query.php
You should also check for errors:
http://php.net/manual/en/mysqli.error.php

mysqli query returns structure instead of data [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
When I do a query to my database this is the result:
mysqli_result Object
(
[current_field] => 0
[field_count] => 3
[lengths] =>
[num_rows] => 3
[type] => 0
)
This is the code I'm using:
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
$conn = new mysqli($servername, $username, $password, 'melona');
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM votacion";
$result = $conn->query($sql);
echo "<pre>";
print_r($result);
echo "</pre>";
die();
Of course I don't want to obtain that, I want to get the rows contained in the table, what's wrong with my code?
Well, you need to fetch the results out first, and to do that, you need to use either ->fetch_assoc() or ->fetch_array():
// you need to loop it if you're expecting multiple rows
while($row = $result->fetch_assoc()) {
echo $row['column_name'];
}
Ref:
http://php.net/manual/en/mysqli-result.fetch-array.php
http://php.net/manual/en/mysqli-result.fetch-assoc.php
You have to do
$row = $result->fetch_assoc();

How to get stuff from database using PDO? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm new at PHP and just learning.
<?php
// database connection
$dbhost = "localhost";
$dbname = "pdo";
$dbuser = "root";
$dbpass = "7777777";
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass)
// echo from database
$select = $conn->query('SELECT username FROM users');
while($row = $select->fetch(PDO::FETCH_ASSOC))
{
$username = $_GET['username'];
echo $username;
}
?>
This gives error:
Parse error: syntax error, unexpected T_PUBLIC in line : $select = $conn->query('SELECT username FROM users');
The connection needs to be changed
From:
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass)
To:
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
The connection is missing a ;

Convert Json Array to Strings [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have my result in json array format, I want to change my array results to json string format. How do I do this. Please help.. i dont have idea how i go about doing this :(. new to json
My result as json array is like this
[["Date.UTC(2011,09,03)","1"],["Date.UTC(2011,09,06)","53"],["Date.UTC(2011,09,07)","178"],["Date.UTC(2011,09,08)","305"],["Date.UTC(2011,09,09)","152"],["Date.UTC(2011,09,11)","20"],["Date.UTC(2011,09,12)","239"],["Date.UTC(2011,09,13)","25"],["Date.UTC(2011,09,14)","316"],["Date.UTC(2011,09,15)","169"],["Date.UTC(2011,09,16)","20"],["Date.UTC(2011,09,19)","126"]
My code
$mysql_connect = mysql_connect($db_host, $db_user, $db_pword, $db_name);
$query = "Select DATE_FORMAT(`timestamp`,'%Y,%m,%d') as date, Count(*) as frequency from mytable group by date_format(`timestamp`,'%Y,%m,%d')";
if (!$mysql_connect) die("unable to connect to database: " . mysql_error());
#mysql_select_db($db_name) or die( "Unable to select database");
$result = mysql_query($query);
$response = array();
if($result === FALSE)
{
die(mysql_error()); // TODO: better error handling
}
while($row=mysql_fetch_array($result))
{
$date = "Date.UTC(".$row ['date'].")";
$frequency =$row['frequency'];
$response[] = array ($date,$frequency);
}
JSONArray jArray;
String s = jArray.toString();
mysql_close($mysql_connect);
json_encode:
<?php
$json_string = json_encode($your_array);
Did you even bother to Google this?

Categories