I wrote this code as a web service that it returned json array could be by an android app .
How the app could parsed all data if there an addition in MySQL database during running the app.
<?php
ini_set('default_charset','UTF-8');
$servername = "localhost";
$username = "root";
$password = "passw";
$dbname = "db";
$conn = mysql_connect($servername, $username, $password,$dbname);
//fetch table rows from mysql db
$sql = "select * from t_name where id=1";
$result = mysql_query("SELECT ID from db.t_name where id =1" )
or die("Error in Selecting " . mysqli_error($connection));
//create an array
$emparray[] = array();
while($row =mysql_fetch_assoc($result))
{
$emparray[] = $row;
}
echo json_encode($emparray);
//close the db connection
mysql_close($conn);
?>
Android app will not get affected with additional columns added in yourable, but will defiantly get affected if you change the data type from int to var char or remove the column
The reason behind this is becasue the application will not be using your additional fields so it does not matters him if a few more columns are being added to it
it will only get affected if the columns it is using are not accessible either if you changed its datatype from int to some string or if they stop getting that particular field application will start getting error or crashes.
Related
I have a PHP script I'm writing where I have to get data from a database and convert the results into a static HTML page. I'm trying to loop through all the items 1 by 1 in a particular database and grab the value that's in their "nid" column as I go along. I started the script. I have the database connection. Just stuck on how I should do the loop. Should it be a while loop or a for loop? If so how do I go about looping through the items in the table 1 by 1 and grabbing a particular column value? Also attached is the screenshot of the database so you can see the column I'm trying to target.
<?php
// Establish all database credential variables
$serverName = "localhost";
$username = "admin";
$password = "peaches";
$databaseName = "static_site";
// Create Database Connection
$connection = new mysqli($serverName, $username, $password, $databaseName);
// Check Database Connection
if ($connection->connect_error) {
die("Connection failed:" . $connection->connect_error);
} // line ends if statement
$queryNodeRevision = "SELECT nid FROM node_revision";
// line above creates variable $queryNodeRevision > selects column "nid" from table "node_revision"
$dataBaseQueryResults = mysqli_query($connection, $queryNodeRevision);
// line above creates variable $dataBaseQueryResults > actually queries the database and passes in 2 variables
// passes in database connection variable and variable that selects "nid" column from "node_revision" table
?>
Use while loop and mysqli_fetch_array to fetch the value of nid for each row.
$queryNodeRevision = "SELECT nid FROM node_revision";
$results=mysqli_query($queryNodeRevision);
while ($row = mysqli_fetch_array($results)) {
echo $row['nid'];
}
We have a phone system database on one server that we cloned/dumped to our local server, but now we need to keep our version updated. Obviously, tables and schema are the same, I just want to run this scheduled script to update with new records that don't exist on the local table (i.e. records that were created since last update).
Below is a test select/insert block. The select query worked on it's own originally, but now I've modified it to use a loop with hopes of using numrows and a foreach to capture everything in the select.
The session table has about 35 columns so I'm looking for the best way to go about this without having to declare every column. I originally tried to do this using update on duplicate key or insert/ignore using a not exists but I don't really know what I'm doing.
Basically, once I select everything, if my table on server 2 doesn't contain a record with the SESSIONID primary key, I want to insert it. I just need some assistance creating this loop script.
Example:
if the table on server 1 has 2 rows with sessionID 12345, and 12346, but my table on server 2 only has up to sessionID 12344, I want to insert the whole records for those two IDs.
//Defining credentials
$servername = "";
$username = "";
$password = "";
$servername2 = "";
$username2 = "";
$password2 = "";
// Create connection
$conn = new mysqli($servername, $username, $password);
$conn2 = new mysqli($servername2, $username2, $password2);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
// Check connection2
if ($conn2->connect_error) {
die("Connection failed: " . $conn2->connect_error);
}
echo "Connected successfully";
//Query to select * from Session table on server 1
$query1 = "select * from cdrdb.session";
$results = mysqli_query($conn1, $query1);
foreach ($results as $r => $result) {
$stmt1 = mysqli_prepare($conn2, "insert into ambition.session a where not
exists(a.SESSIONID)");
mysqli_stmt_execute($stmt1) or die(mysqli_error($conn2));
}
I just started learning php a few days ago and I'm struggling to export table info from the mysql databases to csv format. I've looked around a lot and I've written various php scripts to try this, but none of them function correctly. The tables all have different rows and columns (i.e., one table may have headers of name, age, address, while another table may have headers of hobbies, gender, and race). For now, I'd just be satisfied with any help I can get to just outputting the information correctly so I can understand how it's done.
This is just one of the examples of my failed attempts:
<?php
// mysql database connection details
$host = "";
$user = "";
$password = "";
$db = "";
// open connection to mysql database
$connection = mysqli_connect($host, $user, $password, $db) or die("Connection Error " . mysqli_error($connection));
// fetch mysql table rows
$sql = "select * from db";
$result = mysqli_query($connection, $sql) or die("Selection Error " . mysqli_error($connection));
$fp = fopen('test.csv', 'w');
while($row = mysqli_fetch_assoc($result))
{
fputcsv($fp, $row);
}
fclose($fp);
mysqli_close($connection);
?>
I have a SQL table, a php page. Both set up on a website using cPanel hosting.
The SQL table is like this. Table name is "dbase".
It has two columns, id and name and each column has two rows, say.
Id -> 01, 02
Name -> Name1, Name2.
What I want to do is.
If my page URL is examp.le/page.php?id=01 , or something like that which has a parameter 'id' valued as 01.
How do I assign value for php variable in that page say, $name as the corresponding value for 'name' column in the '01' row?
You have to get the parameter form the url using $_GET[] and then you have to query from the table to get the Name.
Just try this. Hope it helps you.
$id = $_GET['id'];
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dbname";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT name FROM dbase where Id=".$id;
$result = $conn->query($sql);
$name;
if ($result->num_rows > 0) {
// output data of each row
$row = $result->fetch_assoc();
$name = $row['Name'];
} else {
echo "0 results";
}
Now just access $name wherever you want
So I am trying to download JSON into an iOS application and I don't really know a lot about PHP.
Currently I'm using a generic php scrip as a delegate that connects the the MySQL database and returns JSON results.
<?php
$host = "localhost"; //database host server
$db = "***"; //database name
$user = "root"; //database user
$pass = "root"; //password
$connection = mysql_connect($host, $user, $pass);
//Check to see if we can connect to the server
if(!$connection)
{
die("Database server connection failed.");
}
else
{
//Attempt to select the database
$dbconnect = mysql_select_db("***", $connection);
//Check to see if we could select the database
if(!$dbconnect)
{
die("Unable to connect to the specified database!");
}
else
{
$query = "SELECT * FROM table";
$resultset = mysql_query($query, $connection);
$records = array();
//Loop through all our records and add them to our array
while($r = mysql_fetch_assoc($resultset))
{
$records[] = $r;
}
//Output the data as JSON
echo json_encode($records);
}
}
?>
The issue with this is that I don't want to encode the entire table in JSON I just want to return a few select data fields from the particular table so I don't download unnecessary data.
I know that you do this in the SELECT query but something is wrong with my syntax for this.
I had been trying with:
$query = "SELECT *[datafield],[otherdatafield],... FROM table";
but that doesnt seem to be working.
Also, In that table there are two separate data fields that are used to build a URL for an image, and Im not sure how to combine the two here so that they are returned in the JSON as one field.
For example: I Have a base string
"http://wwww.mysite.com/"
and i would like the add the two data Fields to that string so that when it returns the JSON objects they have those fields already concatenated so that the app can just use that URL:
"http://wwww.mysite.com/[data field1]/[datafield2]"
The query you should be trying looks like
$query = "SELECT col1,col2 FROM table";// no need of asterisk if you mention col names
Now if you need to combine two columns there itself in the query, try something like
$query = "SELECT CONCAT('mysite.com/',col1,'/',col2) FROM table";
"/" is the separator between two columns.
The query to fetch individual cols is
select col1,col2,col3 from table_name
No need to provide * like you did
$query = "SELECT *[datafield],[otherdatafield],... FROM table";
^........here * is causing the problem.