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'];
}
Related
i have PHP file that post data to database sequentially ( row by row ) using url , i want to know how can post data at specific row and if there is data in that row replace it by new value
<?php
$dbusername = "root";
$dbpassword = "root";
$server = "192.168.137.150";
$dbconnect = mysql_pconnect($server, $dbusername, $dbpassword);
$dbselect = mysql_select_db("test",$dbconnect);
$sql = "INSERT INTO test.a0 (a0) VALUES ('".$_GET["value"]."')";
mysql_query($sql)
?>
Depending on logic you need in the table, usually you have to look for the entry with SELECT first and then UPDATE if exists or INSERT if missing (2 queries required: SELECT + UPDATE/INSERT).
You can also make that field key and directly use INSERT ... ON DUPLICATE KEY UPDATE in a single query.
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 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
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.
I am new in PHP. I use session first time. I have two tables in db. First table with name pacra_teams with column id and title. Second table is og_users with multiple column but i use team_title as foreign key as store id against team title.
Now i want to create a session and want to display team name from table pacra_teams and user name from table og_users.
I try following code but i failed.
<?php
// starts session
session_start();
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "pacra1";
$conn = new mysqli($servername, $username, $password, $dbname);
$sql="SELECT *
FROM og_users
LEFT JOIN pacra_teams
ON og_users.id = pacra_teams.id
LIMIT 1
";
// setting variable values during session
$_SESSION['og_users.username']=$username;
$_SESSION['pacra_teams.title']=$title;
?>
call these variables
<?php
session_start();
?>
<?php
print_r($_SESSION);
?>
Please help me how i can do this?
One Thing More. if i run seesion.php page it display undefine variable "title"
and if i run print code. It display username "root" but i dont have any user name root in my db
You already defined a query but didn't execute it.
// starts session
session_start();
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "pacra1";
$conn = new mysqli($servername, $username, $password, $dbname);
$sql="SELECT *
FROM og_users
LEFT JOIN pacra_teams
ON og_users.id = pacra_teams.id
LIMIT 1
";
$result = $conn->query($sql);
$row = $result->fetch_object();
// setting variable values during session
$_SESSION['og_users.username'] = $row->USER_NAME; // Change to correct column name in table og_users
$_SESSION['pacra_teams.title'] = $row->TITLE_COLUMN_NAME; // Change to correct column name in table pacra_teams
The result will be the same every time without a WHERE clause in your sql statement. It's only going to return the first row it finds. It looks like you're trying to set user information in a session variable so you can call the data throughout your application so here's a possible solution assuming you grab an ID for the user somewhere (IE web form).
This is a simple answer to explain a concept, not a tutorial.
<?php
//Setup your connection stuff here
session_start();
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "pacra1";
//Get a user's name from a form
$userName = $_POST['username'];
// Perform your query
$db= new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT *
FROM og_users
LEFT JOIN pacra_teams
ON og_users.id = pacra_teams.id
WHERE og_users.username = {$userName} LIMIT 1";
if(!$result = $db->query($sql)){
die('Error [' . $db->error . ']');
}
// Setting variable values during session
while($row = $result->fetch_assoc()) {
$_SESSION['ogUsername'] = $row['USERNAME']; // USERNAME is a placeholder for the example
$_SESSION['pacraTeamsTitle'] = $row['TITLE']; // Same here
}
It's not perfect, but hopefully it helps explain the concept and helps you complete your task.