I want to select the from values from database - php

How can i select values from database
Here is my code:
$sql = 'SELECT * FROM ' . $wpdb->base_prefix . 'item WHERE uname = "'. $_POST['login_name'] . '" '; $result = $wpdb->get_results($sql) or die(mysql_error());
foreach($result as $results) {
$results->salt;
$results->password;
}
echo $sal->$results[0];
echo $pwd->$results[1];

Use Sql Query in Wordpress as below.
global $wpdb;
$results = $wpdb->get_results ( "SELECT * FROM TABLE" );
foreach ($results as $result)
{
$getdata[] = $result->$VALUE;
}
var_dump($getdata);

Related

How to fetch recent 3 post from a blog table: magento

I have a table in database as "neotheme_blog_post" and there are many post in there, now i want to fetch recent 3 posts from this table and show them on home page: I have tried to fetch the data as follows but nothing worked:
<?php $connection =
Mage::getSingleton('core/resource')->getConnection('core_read');
$query = "Select * FROM 'neotheme_blog_post'";
$rows = $connection->fetchAll($query);
foreach ($rows as $values) {
echo $name = $values['name'];
}?>
You may use LIMIT over here.
$connection = Mage::getSingleton('core/resource')->getConnection('core_read');
$query = "Select * FROM neotheme_blog_post LIMIT 3";
$rows = $connection->fetchAll($query);
foreach ($rows as $values){
echo $name = $values['name'];
}
In my case i get the three recent posts as like follows using neotheme blog extension:
$connection =
Mage::getSingleton('core/resource')->getConnection('core_read');
$query = "Select * FROM neotheme_blog_post ORDER BY created_at DESC LIMIT 3 ";
$rows = $connection->fetchAll($query);
foreach ($rows as $values) {
$post_titile = $values['cms_identifier'];
echo '<div>';
echo '<h1>' . $name = $values['title'] . '</h1>';
echo $summery = $values['summary'];
echo 'Read More';
echo '</div>';

PHP output array into SELECT mysql

Need to find array, and then run a MYSQL SELECT where array values are present (or not present).
$symbol = "abc";
$sql = "SELECT * FROM around";
$results = $conn->query($sql);
foreach($results as $row) {
$stop = preg_replace("/[0-9]/", "", $row['tip']);
if ($stop == $symbol)
{$sword = $row['tip'];
}}
So we need $sword to serve as an array in the event that there are multiple outputs. After we have that array, we need to run a mysql query that shows only those that have $sword array.
$query = "
SELECT * FROM ms WHERE `big` = '$sword'";
$result = mysql_query( $query );
So then we can do something like:
while ( $row = mysql_fetch_assoc( $result ) ) {
echo '"time": "' . $row['time'] . '",'; }
Here's the code, converting $sword to an array and using it in your 2nd query:
$symbol = array("abc", "def", "ghi");
$sword = array();
$sql = "SELECT * FROM around";
$results = $conn->query($sql);
foreach($results as $row) {
$stop = preg_replace("/[0-9]/", "", $row['tip']);
if (in_array($stop, $symbol)) {
$sword[] = $row['tip'];
}
}
if ($sword) {
$in = '';
$sep = '';
foreach ($sword as $s) {
$in .= "$sep '$s'";
$sep = ',';
}
/* $in now contains a string like "'123abc123', '12abc12', '1abc1'" */
$query = "
SELECT * FROM ms WHERE `big` IN ($in)";
$result = mysql_query( $query );
while ( $row = mysql_fetch_assoc( $result ) ) {
echo '"time": "' . $row['time'] . '",';
}
}
Now I'm going to go wash my hands to get all that mysql_query off me... :-)

Select multiple rows from DB

I have the array with some IDs(all are unique). I want to select data from database for every id in array. I try this code, but its not working, where is my mistake ?
$array = ....;
foreach ($array as $key => $id) {
$query = "SELECT * FROM user WHERE id = '$id'";
$result = mysql_query($query);
$rows = mysql_fetch_assoc($result)
}
Try this
$array = ......;
$id = implode(",", $array);
$query = mysql_query("SELECT * FROM `user` where id IN($id)");
while($row = mysql_fetch_array($query))
{
$user_id=$row['id'];
}
I want to propose another solution, try it :
$query = "SELECT * FROM user WHERE 1=1 ";
if(count($array){
$query .= " AND (";
foreach ($array as $key => $id) {
$query .= ' OR id ='.$id;
}
$query .= ")";
}
$result = mysql_query($query);
and I hope this help you.

Get Categories (multiple levels)

I am making a script which will load all categories from a database. I just don't know how I can do this in a way which is shorter.
I am trying to figure out a way in which I can theoretically load infinite levels of categories.
$stmt1 = $db->query("SELECT * FROM Rubriek WHERE Hoofdrubriek IS NULL");
while($row1 = $db->fetch($stmt1))
{
$stmt2 = $db->query("SELECT * FROM Rubriek WHERE Hoofdrubriek = '" . $row1['Rubrieknummer'] . "'");
while($row2 = $db->fetch($stmt2))
{
$stmt3 = $db->query("SELECT * FROM Rubriek WHERE Hoofdrubriek = '" . $row2['Rubrieknummer'] . "'");
while($row3 = $db->fetch($stmt3))
{
$stmt4 = $db->query("SELECT * FROM Rubriek WHERE Hoofdrubriek = '" . $row3['Rubrieknummer'] . "'");
while($row4 = $db->fetch($stmt4))
{
$row3['CATEGORY3'][] = array(
'CATEGORY4_NAME' => ucfirst(strtolower($row4['Rubrieknaam'])),
);
}
$row2['CATEGORY3'][] = array(
'CATEGORY3_NAME' => ucfirst(strtolower($row3['Rubrieknaam'])),
'CATEGORY4' => $row2['CATEGORY4'],
);
}
$row1['CATEGORY2'][] = array(
'CATEGORY2_NAME' => ucfirst(strtolower($row2['Rubrieknaam'])),
'CATEGORY3' => $row2['CATEGORY3'],
);
}
$catagories[] = array(
'CATEGORY_NAME' => ucfirst(strtolower($row1['Rubrieknaam'])),
'CATEGORY2' => $row1['CATEGORY2'],
);
}
I would like to have some ideas on how to make this happen.
(I am using a little template system that processes the array that is being made)
EDIT:
Thanks to Ivijan Stefan Stipić I was able to solve this.
This is what I ended up doing
$categories = build_category(0);
And the function:
function build_category($parent, $row = NULL)
{
global $db;
// Initialise array
$data = array();
// Basic SQL statement
$sql = "SELECT * FROM Rubriek";
// Where condition based on $row
if(is_null($row))
{
$where = " WHERE Hoofdrubriek IS NULL";
}
else
{
$where = " WHERE Hoofdrubriek = '" . $row['Rubrieknummer'] . "'";
}
// Execute query
$stmt = $db->query($sql . $where);
// Next level parent
$next = $parent + 1;
// Fetch results
while($row = $db->fetch($stmt))
{
$data[] = array(
'CATEGORY' . $parent . '_NAME' => ucfirst(strtolower($row['Rubrieknaam'])),
'CATEGORY' . $next => build_category($next, $row),
);
}
// Return data
return $data;
}
What do you think about this solution?
function next_level($val)
{
global $db;
$stmt = $db->query("SELECT * FROM Rubriek WHERE Rubrieknummer != '" .$val. "' AND Hoofdrubriek = '" .$val. "'");
while($row = $db->fetch($stmt))
{
echo ucfirst(strtolower($row['Rubrieknaam']));
echo next_level($row['Rubrieknummer']);
}
}
$stmt = $db->query("SELECT * FROM Rubriek WHERE Hoofdrubriek IS NULL");
while($row = $db->fetch($stmt))
{
echo ucfirst(strtolower($row['Rubrieknaam']));
echo next_level($row['Rubrieknummer']);
}
If I understand correctly this is the number of categories with unlimited subcategories.

Query to return records in array

I have a multiple select list (links) which posts values to $links. I then want to run a query on table 'link' returning records that match values in $links. I am using the following code, but not getting any results:
<select name="links[]" size="9" multiple="multiple" id="links">
<?php
$query = mysql_query("SELECT * from link ORDER BY link_title ASC");
for($i=0;$i<mysql_num_rows($query);$i++) {
$row=mysql_fetch_assoc($query);
?>
<option value="<?php echo $row['link_pk']; ?>"><?php echo $row['link_title']; ?></option>
<?php
}
?>
</select>
And the submit code:
$author_pk = $_GET['author_pk'];
$title = $_POST['title'];
$topic_introduction = $_POST['topic_introduction'];
$selected_topic = $_POST['selected_topic'];
$links = $_POST['links'];
$majors = $_POST['majors'];
$majors_string = implode(",", $majors);
$sub_discipline = $_POST['sub_discipline'];
if(isset($_POST['submit'])){
$query_links = "SELECT * FROM link WHERE link_pk IN ('.implode(',',$links).')";
$result_links = mysql_query($query_links, $connection) or die(mysql_error());
while ($row_links = mysql_fetch_assoc($query_links)){
$topic_links = array();
$topic_links[$row_links['url']] = $row_links;
} if($result_links){
$topic = $topic_introduction . '<p>' . $topic_links;
$query = "INSERT INTO topic (topic_pk,title,topic,majors,sub_discipline_fk,author_fk,created)
VALUES ('','$title','$topic','$majors_string','$sub_discipline','$author_pk',NOW())";
$result = mysql_query($query, $connection) or die(mysql_error());
if($result){
$message = "- The topic '" . $title . "' has been created";
}
}
}
This line is wrong:
while ($row_links = mysql_fetch_assoc($query_links)){
... because $query_links is actually a string (your SQL query). You should use $result_links, instead.
Also, the $query_links string isn't being defined correctly. You have to use the same delimiter at the end of a string literal that you use at the beginning. Instead of:
$query_links = "SELECT * FROM link WHERE link_pk IN ('.implode(',',$links).')";
Try:
$query_links = 'SELECT * FROM link WHERE link_pk IN (' . implode(',', $links) . ')';

Categories