Json returns Null instead some datas - php

I'm trying to display in a TextView the text in this page: http://www.oscarilfilm.com/oscar/
using json, php and mysql. I created a json.php to fetch data from db and encode the data in json using this:
<?php
$host="XXXXX"; //replace with database hostname
$username="XXXXX"; //replace with database username
$password="XXXXX"; //replace with database password
$db_name="XXXXXX"; //replace with database name
$con=mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql = "select * from emp_info";
$result = mysql_query($sql);
$json = array();
if(mysql_num_rows($result)){
while($row=mysql_fetch_assoc($result)){
$json['emp_info'][]=$row;
}
}
mysql_close($con);
echo json_encode($json);
?>
And it goes. But when i try change the query to display that post i get this: http://www.oscarilfilm.com/newjson.php and exactly this for OSCAR page: {"id":"39","post_title":"Oscar","post_content":null}
but it's impossible! it can't be null! there is some content! Why? I'm using wordpress as cms.

Related

PHP wont delete mysql rows

Everything seems OK, but it won't delete rows. MySQL version is 5.1. Script is:
<?php
$host="localhost";
$username="***";
$password="***";
$db_name="***";
$db = mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql = "DELETE FROM oc_ixml_cat_map WHERE name='***'";
$result=mysql_query($sql,$db) or die(mysql_error());
if ($result) {
echo "OK";
}
else {
echo "Not OK";
}
?>
Try to change the string to:
DELETE FROM oc_ixml_cat_map WHERE id=1
Where 1 is an available ID of your table just to see if the command is running or it is connection problem...

PHP issue with random mysql rows

I'm having a little problem with php, basically I want to get a random row from my mysql database, I am really new to php and mysql so please be kind and explain me what's going on. I've already granted all permissions on mysql, now I just have to figure out what's going on, i tried to put some echoes to debug but it seems like anything happens, there's just a blank page with nothing on it, this drives me crazy so I'd like to resolve it. Here's the code
<?php
echo "test";
$host="127.0.0.1"; // Host name
$username="username"; // Mysql username
$password="password"; // Mysql password
$db_name="mine"; // Database name
$tbl_name="accounts"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Select a random account
$min=1;
$row=mysql_fetch_assoc(mysql_query("SHOW TABLE STATUS LIKE 'mine.accounts';"));
$max=$row["Auto_increment"];
$random_id=rand($min,$max);
$row=mysql_fetch_assoc(mysql_query("SELECT * FROM `mine`.`accounts` WHERE id='$random_id'");
echo $row["username"]. ":" . $row["password"]
?>
// --- UPDATE ---
<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
$host="127.0.0.1"; // Host name
$username="username"; // Mysql username
$password="password"; // Mysql password
$db_name="mine"; // Database name
$tbl_name="accounts"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Select a random account
$row = mysql_query("SELECT username AND password FROM accounts order by RAND() LIMIT 1");
WHILE ($data = mysql_fetch_array($row))
ENDWHILE;
echo $row['username'] . " " . $row['password'];
?>
On this line, you forgot the closing parentheses.
$row=mysql_fetch_assoc(mysql_query("SELECT * FROM `mine`.`accounts` WHERE id='$random_id'");
Hence the single closing parentheses while you open two.
$row=mysql_fetch_assoc(mysql_query("SELECT * FROM `mine`.`accounts` WHERE id='$random_id'"));
And you'll have to use a while loop to make $row output anything, since fetch_assoc returns an associative array:
while($row = mysql_fetch_assoc(<...>){
$max = $row['Auto_increment'];
}
Also you might wanna look into Prepared Statements or PDO as mysql_* Functions are officially deprecated.

Updating MySQL db

<?php
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="hsp_property"; // Database name
$tbl_name="project_directory"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
//Get values from form
$id = $_POST['id'];
$hospital = $_POST['hospital'];
$project = $_POST['project'];
$state = $_POST['state'];
$status = $_POST['status'];
$da_status = $_POST['da_status'];
$pm = $_POST['pm'];
$budgett = $_POST['budgett'];
$budgetat = $_POST['budgetat'];
$pdapproval = $_POST['pdapproval'];
$pdcs = $_POST['pdcs'];
$pdcd = $_POST['pdcd'];
$pdcf = $_POST['pdcf'];
$pnm = $_POST['pnm'];
$prm = $_POST['prm'];
$comments = $_POST['comments'];
// update data in mysql database
$sql="UPDATE $tbl_name SET Hospital='$hospital', Project='$project', State='$state',Project_Status='$status',DA_Status='$da_status',Project_Manager='$pm',Budget_Total='$budgett',Budget_Approved='$budgetat',Project_Approval_Dates='$pdapproval',Project_Contstruction_Dates='$pdcs',Project_Contract_Dates='$pdcd',Project_Current_Dates='$pdcf',Program_Next_Milestone='$pnm',Program_Milestone='$prm',Comments='$comments' WHERE id='$id'";
$result=mysql_query($sql);
// if successfully updated.
if ($result) {
header ('Location: ../project_directory.php');
}
else {
echo 'Error';
}
?>
The above is some code to update a MySQL db, i'm running WAMP to test the website before I'll upload.
I've been using the phpeasysteps tutorial as php and mysql is new to me. It's been working all ok until now.
Would love to know what i'm doing wrong, the PhpEasySteps tutorial might be a tad old as i've had to update a few elements of the initial code to get it to work..
Replace echo 'Error'; with echo mysql_error(); to see why you didn't get a result and then slap yourself for misspelling a column name or something most likely easily overlooked. If you still can't figure it out, post the error. And if you go that far, post the result of SHOW CREATE TABLE project_directory
You need to add $link_identifier to your mysql_select_db database selection,
Syntax: bool mysql_select_db ( string $database_name [, resource $link_identifier = NULL ] )
$link = mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name", $link)or die("cannot select DB");
You can use mysql_error(); function to find the mysql related errors.

number format in SQL INSERT

Hi guys i need some support on number format in SQL INSERT INTO
i need to convert {$data['rank']['points']} as number format, so that instead of it showing as 5467389 it converts it to 54,673,89
the data is being imported from API Data
this is my code
$host="hostname"; // Host name
$username="database"; // Mysql username
$password="password"; // Mysql password
$db_name="dbname"; // Database name
$tbl_name="table"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// API Data
$url = "http://api.erpk.org/citizen/profile/3121752.json?key=Yn3AsG80";
$json = $json = file_get_contents($url);
$data = json_decode($json, true);
date_default_timezone_set('Europe/London');
// Insert data into mysql
$sql="INSERT INTO $tbl_name(citid, citname, rankpoints)VALUES('{$data['id']}', '{$data['name']}', '{$data['rank']['points']}')";
$result=mysql_query($sql);
// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='supplies_insert.php'>Back to main page</a>";
}
else {
echo "ERROR";
}
Try this:
$query = sprintf("INSERT INTO `%s` (citid, citname, rankpoints) VALUES ('%s', '%s', '%s')",
mysql_real_escape_string($tbl_name),
$data['id'],
mysql_real_escape_string($data['name']}),
mysql_real_escape_string(number_format($data['rank']['points'])));
$result = mysql_query($query);
You can use php's number_format() function if I understand your requirement correctly. Let me know if that's not what you want.

Update SQL tables

I want to update a table on a specific row need some advice on my php statement
I use this statement to call the client's info
<?php
$host="localhost"; // Host name
$username="****"; // Mysql username
$password="****"; // Mysql password
$db_name="****"; // Database name
$tbl_name="members"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// get value of id that sent from address bar
$id=$_GET['id'];
// Retrieve data from database
$sql="SELECT * FROM $tbl_name WHERE member_msisdn='$query'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
?>
This works fine echoĆ­ng the information I need and able to alter it.
<form name="form1" method="post" action="control_clientupdated.php">
This referes to my action php script
Problem I need assistance with is how do i notify my action script to use the same id I ran the query on to update that row.
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// update data in mysql database
$sql="UPDATE $tbl_name SET member_name='$member_name',
member_surname='$member_surname', member_msisdn='$member_msisdn', cid='$cid',
cofficenr='$cofficenr', cfax='$cfax', e2mobile='$e2mobile' WHERE member_msisdn='$query'";
$result=mysql_query($sql);
I have placed the WHERE statement on the end of the update
Let me just state it shows done, but it did not update the table at all
Firstly you need to store your ID into a hidden form element in your form.
<form method="post" action="control_clientupdated.php">
<input type="hidden" name="member_msisdn" value="<?=$query?>" />
...
</form>
This will allow you to passthrough the value from your first php script.
Then in your control_clientupdated.php you need to use $_POST to recover your value.
// Store the $_POST value for my query ID
$query = $_POST['member_msisdn'] ;
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// update data in mysql database
$sql="UPDATE $tbl_name SET member_name='$member_name',
member_surname='$member_surname', member_msisdn='$member_msisdn', cid='$cid',
cofficenr='$cofficenr', cfax='$cfax', e2mobile='$e2mobile' WHERE member_msisdn='$query'";
$result=mysql_query($sql);
This should be what you need - note that you cannot use $_GET to retrieve the variable passed by the form, as you are sending it with the method="post" attribute, you must use $_POST instead of $_GET

Categories