Right now I am selecting some data from Mysql which I want to echo out, but I don't know how..
Code
<?php
// Database connection
require_once($_SERVER["DOCUMENT_ROOT"] . "/includes/config.php");
require_once($_SERVER["DOCUMENT_ROOT"] . "/includes/opendb.php");
// News 1
$searchroutenews1 = "SELECT newsid FROM NewsHomepage WHERE id = '1'";
$handlenews1 = mysqli_query($GLOBALS["___mysqli_ston"], $searchroutenews1);
$news1 = mysqli_fetch_row($handlenews1);
$searchroutenewsimg1 = "SELECT newsimg FROM NewsHomepage WHERE id = '1'";
$handlenewsimg1 = mysqli_query($GLOBALS["___mysqli_ston"], $searchroutenewsimg1);
$NewsImg1 = mysqli_fetch_row($handlenewsimg1);
// News 2
$searchroutenews2 = "SELECT newsid FROM NewsHomepage WHERE id = '2'";
$handlenews2 = mysqli_query($GLOBALS["___mysqli_ston"], $searchroutenews2);
$news2 = mysqli_fetch_row($handlenews2);
$searchroutenewsimg2 = "SELECT newsimg FROM NewsHomepage WHERE id = '2'";
$handlenewsimg2 = mysqli_query($GLOBALS["___mysqli_ston"], $searchroutenewsimg2);
$NewsImg2 = mysqli_fetch_row($handlenewsimg2);
// News 3
$searchroutenews3 = "SELECT newsid FROM NewsHomepage WHERE id = '3'";
$handlenews3 = mysqli_query($GLOBALS["___mysqli_ston"], $searchroutenews3);
$news3 = mysqli_fetch_row($handlenews3);
$searchroutenewsimg3 = "SELECT newsimg FROM NewsHomepage WHERE id = '3'";
$handlenewsimg3 = mysqli_query($GLOBALS["___mysqli_ston"], $searchroutenewsimg3);
$NewsImg3 = mysqli_fetch_row($handlenewsimg3);
?>
After this I require_once this in an other file, and then I echo the variables $news1, $news2, $news3, $NewsImg1, $NewsImg2 and $NewsImg3. But if I echo this variables out now it says: array.
You can fetch all this information via a single query, instead of the 6 you currently are running. Then it's a matter of putting mysqli_fetch_*() as the argument of a while, as you'll then fetch all the rows, until that function returns null - at which point you've fetched all the rows returned by the query.
$result = mysqli_query($GLOBALS["___mysqli_ston"], "SELECT newsid, newsimg FROM NewsHomepage WHERE id IN (1, 2, 3)");
while ($row = mysqli_fetch_assoc($result)) {
echo $row['newsid']." ".$row['newsimg'];
}
Change however you need it to be displayed inside the while loop, and use the two variables as they are inside.
Alternatively, you can use WHERE id BETWEEN 1 AND 3 instead, but using IN (1, 2, 3) can more easily be changed to the exact ids you need.
http://php.net/mysqli-result.fetch-assoc
First you should read http://php.net/manual/en/mysqli-result.fetch-row.php
you can find there mysqli_result::fetch_row -- mysqli_fetch_row — Get a result row as an enumerated array mysqli_fetch_row always return array so now you can't echo array thats why it gives you array.
You can try foreach loop or for loop or while loop to display your data. there are also various methods to get array value.
Below is an example you can use.
while ($news1 = mysqli_fetch_row($handlenews1)) {
echo $news1[0];
}
Related
I cant figure out how to get multiple entries of data from my database. You can see below that I have newsID = 1, which allows me to get that to show on my database, but how should I change the code to be able to access other entries like newsID = 2 as well?
<?php
$queryAffinity = "SELECT * FROM affnews WHERE newsID = 1";
$stmt = $pdo->query($queryAffinity);
$row = $stmt->fetchObject();
?>
<?php if($row->newsID == 1) echo "<p>{$row->newsDescription}</p>"; ?>
this is the code im trying to use to echo the id 1
Edit your WHERE-Clause and put it in a loop. For example:
$queryAffinity = "SELECT * FROM affnews WHERE newsID = 1 OR newsID = 2";
$stmt = $pdo->query($queryAffinity);
while($row = $stmt->fetchObject())
{
//do something with $row
}
You Can use the following code:
$queryAffinity = "SELECT * FROM affnews WHERE newsID IN (1, 2)";
$stmt = $pdo->query($queryAffinity);
while($row = $stmt->fetchObject())
{
echo $row->id;
}
Hi i have a php form and part of this is to get the last "shiftID" row entry in my table and put this into a variable so i can later add 1 to said variable. However the result of the following code returns the information linked below. How do i get the last "shiftID" number by itself into a variable.
<?php
session_start();
include 'dbh.php';
$start = $_GET['starttime'];
$finish = $_GET['finishtime'];
$dat = $_GET['date'];
$id = $_GET['userid'];
$shiftidd = $conn->query("SELECT shiftID FROM shift_user ORDER BY shiftID DESC LIMIT 1");
$row = mysqli_fetch_row($shiftidd);
echo print_r($row[0]);
//$result = $conn->query("INSERT INTO shift (shiftStart, shiftFinish, shiftDate)
//VALUES ('$start', '$finish', '$dat')");
//$sql = $conn->query("INSERT INTO shift_user (shiftID, userID) VALUES ('$shiftidd', '$id')");
//header("Location: shifts.php");
?>
Web page result:
"connected 41"
I'm looking for the number "4" but I'm guessing the "1" is the affected row along with the result? but how do i get rid of the "1"?
Thanks in advance.
1 is a result of print_r() which is superfluous here. Use either echo or print_r, but not both
You can do it like. After you insert query executed use the following function
mysql_query("INSERT INTO Persons (FirstName,LastName,Age)
VALUES ('Glenn','Quagmire',33)");
$id = mysql_insert_id();//will return you the last id inserted into table//
But if you want to find the last id of some table then you have to use the follwoing code:
$shiftidd = $conn->query("SELECT MAX(shiftID) FROM shift_user LIMIT 1");
$row = mysqli_fetch_row($shiftidd);
echo print_r($row[0])///////it will be the last id of the table////////
if you want to find next id please try this
$shiftidd = $conn->query("SELECT MAX(shiftID)+1 FROM shift_user LIMIT 1");
$row = mysqli_fetch_row($shiftidd);
echo print_r($row[0])///////it will be the next id of the table///////////
I've got an issue related with getting some data from a serialized data stored in a table and to use it for queuing another table. My approach is the following, but I'm quite new to programming and it doesn't work
table wppg_album
id - name - gallery_list
1 - album1 - a:1:{i:0;s:1:"1";i:1;s:1:"8"}
2 - album2 - a:2:{i:0;s:2:"17";i:1;s:2:"19";i:2;s:2:"18";}
3 - album3 - a:3:{i:0;s:2:"13";}
I'm stuck with getting SQL string and deserialize data in cell gallery_list for a selected id.
The album to work on is by now fixed, tell #2 i have to get array(17,18,19) to finally get results from galleries table.
I wrote this code but the getting of the serial data doesen't work
<?php
$sql = "SELECT * FROM ".WPPG_TBL_ALBUM." WHERE id = 2";
$result = mysql_query($sql);
$stringGalleries = mysql_fetch_object($result);
$galleries = unserialize($stringGalleries);
$ids = join(',',$galleries);
$data = $wpdb->get_results("SELECT * FROM ".WPPG_TBL_GALLERY." WHERE id IN ($ids)");
?>
Any help will be appreciated.
you have a typo, $ and ; missing
$galleries = unserialize($stringGalleries);
also you are using mysql_fetch_object in wrong way... here is correct code
<?php
$sql = "SELECT * FROM ".WPPG_TBL_ALBUM." WHERE id = 2";
$result = mysql_query($sql);
while ($galleriesRow = mysql_fetch_object($result)) {
$galleries = unserialize($galleriesRow->gallery_list);
$ids = join(',',$galleries);
$data = $wpdb->get_results("SELECT * FROM ".WPPG_TBL_GALLERY." WHERE id IN (" . $ids. ")");
}
?>
I'm trying to make multiple queries in order to find the most recent entry in a database by username.
Here's my code:
<?php
require_once("../includes/db_connection.php");
$userID = $_POST["userID"];
$returnString = array();
// Query the max id value of a given key_id (find the most recent upload)
$query = "SELECT MAX(id) FROM photos WHERE key_id = {$userID}";
$result = mysqli_query($connection, $query);
//additional while loop could go here
//now get the url where from the max id value that we just queried
$query = "SELECT url FROM photos WHERE id = {$urlID}";
$result = mysqli_query($connection, $query);
$returnString['url'] = $urlID;
mysqli_free_result($result);
echo json_encode($returnString);
?>
I think the problem lies in the first query. When I return the result from that, I get:
{"maxID": "current_field":null,"field_count":null,"lengths":null,"num_rows":null,"type":null}
When I create a while loop to capture the array (why I need to do this is beyond me because it will only ever return 1 value):
while($row = mysqli_fetch_assoc($result)) {$returnString[] = $row;}
Then I get this funky result:
[{"MAX(id)":"30"}]
30 is the correct value, but then I don't know how to use that result in my next mySQL query.
**********UPDATE*************
The query :
SELECT url FROM photos WHERE id = (SELECT MAX(id) FROM photos WHERE key_id = {$userID});
Works perfectly when making the query from within mySQL, but doesn't work from my php script. It returns this weird string:
{"url":{"current_field":null,"field_count":null,"lengths":null,"num_rows":null,"type":null}}
Here's the updated script:
require_once("../includes/db_connection.php");
$userID = $_POST["userID"];
$returnString = array();
$query = "SELECT url FROM photos WHERE id = (SELECT MAX(id) FROM photos WHERE key_id = {$userID})";
$result = mysqli_query($connection, $query);
mysqli_free_result($result);
$returnString['url'] = $result;
echo json_encode($returnString);
Unless I'm missing something in the schema that's not apparent from code and comments, you can save yourself a roundtrip by combining your SQL commands.
$query = "SELECT id AS urlID, url FROM photos WHERE id = (SELECT MAX(id) FROM photos WHERE key_id = {$userID})";
Then interface with your results like you normally would.
Updated answer:
$query = "SELECT url FROM photos WHERE id = (SELECT MAX(id) FROM photos WHERE key_id = {$userID})";
$result = mysqli_query($connection, $query);
$row = mysqli_fetch_array($result):
$url = $row['url'];
echo json_encode($url);
mysqli_free_result($result);
I think the real problem is in the loop and use of an array for the results.
You should change to a single query like:
SELECT url, MAX(id) as id FROM photos WHERE key_id = {$userID}
MAX(id) as id returns the aggregate column name as id
You don't need to loop with a while if you are only expecting one row. Just change the while to if to test if any row is returned, and assign the values to single variables:
$id = {$row['id']};
$url = {$row['url']};
The "funky" result is from trying to print the array which is not needed and has stored the column name and value.
I have the following code that fetches a single row:
$query = "SELECT *
FROM translations
WHERE iddoc = '$id'
AND submitted = 1;";
$result = mysqli_query($query);
$row = mysqli_fetch_array($result);
I know this is useful in a while loop, where you can just loop through the results.
But I need to be able to grab specific rows that meet this condition. Something following this pseudo code:
$query = "SELECT *
FROM translations
WHERE iddoc = '$id'
AND submitted = 1;";
$result = mysqli_query($query);
$arrayofrows = mysqli_fetch_arrayofrows($result);
$arrayofrows[0] //(to get the first row)
$arrayofrows[1] //(2nd row)
and so on...
How can I do this?
You can try something like this
$arrayofrows = array();
while($row = mysqli_fetch_array($result))
{
$arrayofrows = $row;
}
You can now have
$arrayofrows[0] //(to get the first row)
$arrayofrows[1] //(2nd row)
I think the function you are looking for is mysqli_fetch_all as shown below. This avoids the need to create an extra looping structure or a function to do something already provided.
$query = "SELECT *
FROM translations
WHERE iddoc = '$id'
AND submitted = 1;";
$result = mysqli_query($query);
$arrayofrows = mysqli_fetch_all($result);
$arrayofrows[0] //(to get the first row)
$arrayofrows[1] //(2nd row)
It depends on whether you require the entire result set back or not but I think the LIMIT could be used like:
$query = "SELECT *
FROM translations
WHERE iddoc = '$id'
AND submitted = 1 LIMIT 200,200;";
Otherwise as others say you will need to convert to an array (which is what fetch_all does) and then get the element from that array.