I am trying to make simple page which will return values from MySQL table, but the problem is that if I want to use condotions in query then it doesn't work.
My PHP page:
<?php
$servername = "10.10.10.10";
$username = "username";
$password = "password";
$dbname = "GENERIC_TABLES";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT WO_NUM+1 from GENERIC_TABLES.WO_NUMBERS ORDER BY WO_NUM DESC limit 1";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<br> WO Number ". $row["WO_NUM"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
So, WO_NUM column has numbers like 1, 2, 3 etc.
I want to get the last one + 1
So if I do like:
$sql = "SELECT WO_NUM from GENERIC_TABLES.WO_NUMBERS ORDER BY WO_NUM DESC limit 1";
Then it works fine, but if I want to make it WO_NUM + 1 then it returns nothing.
Why it happens like that and is there any way to get what I want using MySQL?
I don't want to get WO_NUM and then using PHP make it + 1, since I also need INSERT to the table values and I would like to understand why it doesn't work.
As you realized, WO_NUM+1 changes the column name in the resulting array, so use an alias WO_NUM+1 as NEW_NUM. However I would not bother with the sorting and limit. Consider MAX():
SELECT max(WO_NUM)+1 as NEW_NUM from GENERIC_TABLES.WO_NUMBERS
As it has been pointed by AbraCadaver, I missed that if I am using WO_NUM + 1 then column name changing so that is why i didn't get any output, so using alias solved problem.
Related
I'm trying to loop through each row of a table in a database, then once I'm on a particular row get the value of a certain column. Is this possible? I've done a couple Google searches but nothing really concrete. I try using the mysqli_fetch_array() function but when I do I get the results of a column. I want to target each row. The code I have so far gets me the "nid" column. That's not what I want. I want to iterate through each row.
<?php
$serverName = "localhost";
$username = "user1";
$password = "sp#99#1";
$databaseName = "developer_site";
// Connection
$connection = new mysqli($serverName, $username, $password, $databaseName);
// Check 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"
$results = mysqli_query($connection, $queryNodeRevision) or die("Bad Query: $results");
while ($row = mysqli_fetch_array($results)) {
echo "NID: ";
echo $row['nid'];
echo "<br/>";
}
?>
You can select rows by a certain condition with an SQL query alone and also select all columns.
SELECT * FROM node_revision where condition;
condition could be anything. For example another_row = 'something'.
But if, for some reason, you need to process the nid inside your php script to know if that row is the "particular" one you're searching, then you just select all the columns, or the ones you need.
$queryNodeRevision = "SELECT nid, col1, col2 FROM node_revision";
$results = mysqli_query($connection, $queryNodeRevision) or die("Bad Query: $results");
while ($row = mysqli_fetch_array($results)) {
if (condiiton) {
echo "Particular: ".$row['nid']
." ".$row['col1']
." ".$row['col2'];
}
}
condition could be something like $row['nid'] == 123 for example.
Hope it helps.
I have this code that will display all of the info on my database. The problem is I want to show only about 10 of the last results. But be able to request more somehow by pressing button. Thank you for taking interest in my question.
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "myDB";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<div id='message'> <br> ". $row["firstname"]. " " . $row["lastname"] . "<br> </div>";
}
} else {
echo "0 results";
}
$conn->close();
?>
There are two ways you could solve this
Option 1
The problem is I want to show only about 10 of the last results
One way to do it is to use the SQL Limit specifier
$sql="Select * from ORDERS LIMIT 5"
this will fetch the 5 items from your database. You can use ordering mechanisms for sorting,etc
But be able to request more somehow by pressing button.
You can use the offset specifier for this.
$sql="Select * from ORDERS LIMIT 5 OFFSET 15"
This fetches 10 records from the 16th record.You can add to the offset on each button press.
Here is some more information on this link.
Option 2
Pagination.This link should help.
use LIMIT and OFFSET clause for limit selection. link
if you want to select last 10 data, write query like this.
SELECT id, firstname, lastname FROM MyGuests ORDER BY id DESC LIMIT 10
and you should use ajax for "show more" function.
I am new at this and learning the code.
I want to create the php code which select the particular row.
say 5th row or 6th row any row.
I create the code like this
<?php
$servername = "localhost";
$username = "test1";
$password = "pass";
$dbname = "test1";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT FIELD1, FIELD2 FROM mytable ";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["FIELD1"]. " - Name: " . $row["FIELD2"]. " <br>";
}
} else
{
echo "0 results";
}
$conn->close();
?>
THis code works fine but it give the all data of the table.I want to just select particular row number data.how to do this??
You can do it with the LIMIT statement, say LIMIT 3,1 to select the 4th row. First number is the starting row, second number is the count of rows to select.
$sql = "SELECT FIELD1, FIELD2 FROM mytable LIMIT $row_index, 1";
will give you row $row_index + 1
You can do it using WHERE condition in query as SELECT FIELD1, FIELD2 FROM mytable WHERE id = 1.
So I am trying something totally new (for me). I have a PHP file where I try to retrieve the data of 2 tables and replace values if they match. Since I'm not really sure what I'm doing I'm kinda stuck:) Here is what i want:
I have 2 tables in my mysql database.
The table 'advertisement' has the column 'logo_fid'.
The table 'files' has the columns 'fid' and 'filepath'.
Now I would like to echo the values of the 'logo_fid' column in the 'advertisement' table.
Before that, however, I would like to see if in the table 'files' a corresponding value is stored in the 'fid' volumn and if so I would like to echo the value of 'filepath' instead of the value of 'logo_fid'.
So basically what I want is to replace the returned value of the logo_fid column, if this value matches with the value in the 'fid' column, with the corresponding 'filepath' value.
Hope I made myself clear. Anyway I have this code atm. Hope that anybody can help me out with this one. Thanks a lot!
<?php
//connection info xxxx
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//advertisement SELECT
$sql = "SELECT logo_fid FROM advertisement ORDER BY nid ASC";
$result = $conn->query($sql);
//advertisement SELECT
$sql2 = "SELECT fid, filepath FROM files";
$result2 = $conn->query($sql2);
if ($result->num_rows > 0 && $result2->num_rows > 0) {
// output data of each row
while(($row = $result->fetch_assoc()) && ($row2 = $result2->fetch_assoc())){
//So after i received all the info i'd like to check if the value of logo_fid matches with fid and if so replace the value with the value of filepath
if ($row["logo_fid"] == $row2["fid"]) :
echo $row2["filepath"];
else :
echo $row["logo_fid"];
endif;
echo '<br>';
}
} else {
echo "0 results";
}
$conn->close();
?>
Seems like this could be done as one query.
SELECT A.logo_Fid, F.Fid, F.filePath, coalesce(F.FilePath, Logo_Fid) as ValueYouWant
FROM advertisement A
LEFT JOIN files F
on A.logo_Fid = F.FID
Then you can echo ValueYouWant without the inline logic.
The join relates the two tables on the Fid and logo_fid.
if a match is found, Valueyouwant will contain the f.filepath, if no match is found logo_fid will be in valueyouwant.
Since this is using a LEFT join, all values from advertisement will be returned, and only related records from files will be returned.
so to display...
I don't do much PHP, but an online search that was accpted as an answer indicates it would be something like this...
$sql=" SELECT A.logo_Fid, F.Fid, F.filePath, coalesce(F.FilePath, Logo_Fid) as ValueYouWant
FROM advertisement A
LEFT JOIN files F
on A.logo_Fid = F.FID ORDER BY NID asc"
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
echo $row['ValueYouWant'];
echo '<br>';
}
I have a database which simply records words in a table in a single column (with RID beside). All I want to do is display the words in order with a space in between (which I have a working code for below)
<?php
$mysql_host = "mysql1.000webhost.com";
$mysql_database = "db name";
$mysql_user = "user";
$mysql_password = "pass";
// Create connection
$conn = new mysqli( "mysql1.000webhost.com","databaseuser","password","databasename");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = 'SELECT Words FROM Poetry ';
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $row["Words"]. " ";
}
} else {
echo "0 results";
}
?>
HOWEVER. I want to only display a set amount (say 50) of the MOST RECENT db entries (i.e) the 50 with the highest RID. Can somebody quickly tell me what code I need (in php) which will display the 50 most recent on a web page?
Thanks!
Don't do it with PHP, just add it to your query.
$sql = 'SELECT Words FROM Poetry order by RID desc limit 50';
This query will order the result set by RID descending, most recent first I'm assuming, and limit the result set to 50 records.
You can use
SELECT Words FROM Poetry order by RID desc limit 0,50
0 is the offset and 50 is limit.u can dynamically change those value if needed.
For the better security purpose You can use mysqli prepared statements.
You can see dynamically-bind_param-array-mysqli for Dynamically Bind Params
Try saving it in a variable and echoing it after your while statement.
Something like this
$all_words = '';
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$all_words = "$all_words, $row['Words']";
}
echo"$all_words";
} else {
echo "0 results";
}