Generate simple JSON file based on database - php

Hey I am iOS developer I am trying to create simple JSON output from my website. I found good start link and here is some explanation how to do it.
So I've created accounts.php file and put it to my public_html folder
<?php
include_once("JSON.php");
$json = new Services_JSON();
$link = mysql_pconnect("localhost", "user", "pass") or die("Could not connect");
mysql_select_db("iglobe") or die("Could not select database");
$arr = array();
$rs = mysql_query("SELECT * FROM users");
while($obj = mysql_fetch_object($rs)) {
$arr[] = $obj;
}
Echo $json->encode($arr);
?>
Of course I use my user and password and I pointed my just created database ob my end.
so when I try to request my file so http//mywebsite.com/accounts.php there is no data.
I tried to use google chrome and Postman so it says No response received when I switch to JSON. For HTML there is no info in Postman.
My question how can I test it? even if I use Echo(123) before include_once("JSON.php"); line there is no 123 on html page.
I tried to test PHP with only this code:
<?php
phpinfo();
?>
and it works. I have PHP Version 5.4.32

First of all, simply use PHP's function json_encode($arr). It does exactly what you are asking for and is pretty much included in every version of PHP that I can think of.
Documentation
Also, I am not sure if this is the issue, but you may want to change Echo ==> echo. This is generally convention at the very least.
SUPER IMPORTANT
Finally, DO NOT USE mysql extension. Its is dangerous, may not work correctly, and has security vulnerabilities. Use mysqli or PDO.

Matrosov -
You are very close. Use the json_encode function to output your code via the PHP manual. Also consider using mysqli instead of mysql for your database connection as it has been better support for modern MySQL servers.
http://php.net/manual/en/function.json-encode.php
http://php.net/manual/en/book.mysqli.php
<?php
include_once("JSON.php");
$link = mysqli_connect("localhost", "user", "pass") or die("Could not connect");
$link->mysql_select_db("iglobe") or die("Could not select database");
$arr = array();
$rs = mysql_query("SELECT * FROM users");
while($obj = mysql_fetch_object($rs)) {
$arr[] = $obj;
}
echo json_encode($arr);
?>

Related

php decode JSON -> mysql query

I'm trying to make query.
I don't know why this php doesn't work.
there is no change in my DB.
are there errors?
<?
$json = file_get_contents('php://input');
$obj = json_decode($json);
$con = mysql_connect("localhost:(port)","ID","PW")
or die('Cannot connect to the DB');
mysql_select_db("DBID",$con);
$row = $obj->{"menusel_row"};
$col = $obj->{"menusel_col"};
$val = $obj->{"menuvalue"};
mysql_query("UPDATE recommend SET $row = '$val' where id = '$col'", $con);
mysql_close($con);
?>
A brief note: you are using short open tags for PHP <?, this feature most probably is disabled on your PHP server, because it is obsolete.
Absence of any output most probably is related to that. Use <?php instead.

Outputting contents of database mysqli

Hi I know this is a little general but its something I cant seem to work out by reading online.
Im trying to connnect to a database using php / mysqli using a wamp server and a database which is local host on php admin.
No matter what I try i keep getting the error Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given when i try to output the contents of the database.
the code im using is:
if (isset($_POST["submit"]))
{
$con = mysqli_connect("localhost");
if ($con == true)
{
echo "Database connection established";
}
else
{
die("Unable to connect to database");
}
$result = mysqli_query($con,"SELECT *");
while($row = mysqli_fetch_array($result))
{
echo $row['login'];
}
}
I will be good if you have a look at the standard mysqli_connect here
I will dont seem to see where you have selected any data base before attempting to dump it contents.
<?php
//set up basic connection :
$con = mysqli_connect("host","user","passw","db") or die("Error " . mysqli_error($con));
?>
Following this basic standard will also help you know where prob is.
you have to select from table . or mysqli dont know what table are you selecting from.
change this
$result = mysqli_query($con,"SELECT *");
to
$result = mysqli_query($con,"SELECT * FROM table_name ");
table_name is the name of your table
and your connection is tottally wrong.
use this
$con = mysqli_connect("hostname","username","password","database_name");
you have to learn here how to connect and use mysqli

Switching to mysqli from Mysql. Can't get simple query workign

Decided to move to procedural style coding. I've tried the below code and receive no errors or returned data. What am I doing wrong? Blanked out the mysqli_connect parameters because I am using a shared server - assume the parameters are correct. Thanks in advance.
function login(){
$connect = mysqli_connect("**********", "******", "****", "*****");
$result = mysqli_query($connect, "SELECT * FROM `new_base`");
$bang = mysqli_fetch_all($result);
echo $bang; }
NOTE: The call to the function does work echoing a string does get returned to the page.

How to connect to a SQLite3 db with PHP

I'm new to SQLite3 and PHP and was wondering whether and how I could connect to a SQLite3 database with PHP.
How would I get the data from the db and would it be possible to output them on a browser screen?
I've been searching the web for a while now, but couldn't find anything.
Thank you.
<?php
$db = new SQLite3('mysqlitedb.db');
$results = $db->query('SELECT bar FROM foo');
while ($row = $results->fetchArray()) {
var_dump($row);
}
?>
Taken from here: PHP: SQLite3::query - Manual
SQLite is enabled by default with PHP. You have to use the built-in class SQLite3 (you will find some examples on this page).
This is the best way I have found and I got it directly from the sqlite website:
<?php
$db = new SQLite3('sqlite3db.db');
$results = $db->query('select * from db');
while ($row = $results->fetchArray()) {
var_dump($row);
}
?>
Also if you're looking to include the php results into html like a table cell or something, go as such:
$results = $db->query('select * from db');
?>
<?php while ($row = $results2->fetchArray()) {?>
<td><h4><?php echo $row['id'];?></h4></td>
<?php } ?>

Connecting to MSSQL database with ADO and PHP

I have a MSSQL database. I wrote a php page to connect and retreive data from it. The code works on my localhost.
$strCon = "Provider=SQLOLEDB;Data Source=local;database=test;uid=sa;pwd=go;";
$strAlias = "GEORGE";
$strSql = "SELECT LINK FROM LINKS WHERE ALIAS = '" . $strAlias . "'";
$Con = new COM ("ADODB.Connection") or die("Cannot start ADO");
$Rs = new COM ("ADODB.Recordset") or die("Cannot start ADO");
$Con->open($strCon);
$Rs->open($strSql,$Con,1,3);
if (!$Rs->EOF && !$Rs->BOF) {
$strTargetLink = $Rs->Fields['LINK'];
echo $strTargetLink;
}
$Rs->Close();
$Con->Close();
$Rs = null;
$Con = null;
When I run this code on the server, I receive some error? when echo $strTargetLink; row is executed, word
Object
is received on the sent html page and also in the source code of that page.
I am running PHP on IIS as a FastCGI application. Both PHP 4.4.7 and 5.2.6 are supported.
Any ideas? What does this Object text mean?
Thanks.
Instead of using echo, I now tried
var_dump($strTargetLink);
and received the information
object(COM)(1) { [0]=> resource(3) of type (COM) }
try using var_dump instead of echo. it will at least give you more information about the object contained in $strTargetLink.
I have found a solution and now it works. So just in case you would like to connect with ADO, I am writing what I have done.
Instead of
$strTargetLink = $Rs->Fields['LINK'];
I wrote
$strTargetLink = $Rs->Fields(0);

Categories