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. ")");
}
?>
Related
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];
}
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'm trying to get data from a table column that has a corresponding ID, but when I try to get an ID it gives me a 'resource ID'. My column ID holds INTs, and I found that if I add or subtract to the resource ID i get a number so I tried adding then subtracting 1, but it still isn't working.
<?php
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("blog") or die(mysql_error());
$strSQL = "SELECT id FROM blogtable ORDER BY id DESC";
$rs = mysql_query($strSQL);
$artID = $rs-1+1;//So if I don't do this, I get resource ID#'#'
?>
This code is on the same page, but in a separate container.
<?php
$getArticle = "SELECT title FROM blogtable Where id = '$artID'";
$selectedArticle = mysql_query($getArticle);
echo $selectedArticle;
?>
I was hoping this would get me the text from my title column, but it just gives me another resource id.
You can use
$result = mysql_fetch_object($rs);
$artID = $result->id;
...
$selectedArticle = mysql_query($getArticle);
$res = mysql_fetch_object($selectedArticle);
echo $res->title;
But i will recommende using PDO or mysqli functions since mysql_ functions will become obsolete.
mysqli examples: http://www.w3schools.com/php/php_ref_mysqli.asp
or
PDO tutorials: http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers
mysql_query gives you resource id. If you want to get your ID;
$id = 0;
while ($row = mysql_fetch_assoc($rs)) {
$id = $row['ID'];
}
Or
$row = mysql_fetch_array($rs);
and get id from $row array
Ok Im getting a bit confused with arrays in php. This is the code
$search = "%" . $search ."%";
// Step 1: Establish a connection
$db = new PDO("mysql:host=localhost;dbname=########", "#########", "###########");
// Step 2: Construct a query
$query = "SELECT * FROM movies WHERE title LIKE " . $db->quote($search);
// Step 3: Send the query
$result = $db->query($query);
// Step 4: Iterate over the results
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
var_dump($row);
echo $row['title'];
}
// Step 5: Free used resources
$result->closeCursor();
$db = null;
Now when I search for say "Candyman" it returns 6 results, what I am tring to do is something like $row['title'][3] (So I can grab the title of the 3rd film in the array)
however this doesn't work $row['title']; displays all 6 titles how do I access the next level?
Below is a link to the search I am using
http://www.gorrors.com/moviesearch.php?search=candyman
Sorry for the newbie question but Im at a loss any help would be greatly appreciated.
Change the query, like, $query = "SELECT * FROM movies WHERE title LIKE " . $db->quote($search); use limit, $query = "SELECT * FROM movies WHERE title LIKE " . $db->quote($search) limit $start,$numbers; http://php.about.com/od/mysqlcommands/g/Limit_sql.htm
Try:
$rows = $result->fetchAll();
echo $row[3]['title']'
PDO::query:
http://www.php.net/manual/en/pdo.query.php
Which returns a PDOStatement object:
http://www.php.net/manual/en/class.pdostatement.php
...which has a method named fetchAll(), which returns an array of all the rows, which by default you can access using either numeric indexes or strings.
You can try Grouping all values by a single column :
//do not use * in your sql if you only need the imfromation of movie title
$query = "SELECT title FROM movies WHERE title LIKE " . $db->quote($search);
$result = $dbh->prepare($query);
$result->execute();
$row = $result->fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_GROUP);
var_dump($row);
//$row['title'][2] will give the third result,not $row['title'][3]
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.