Trying to display results from an sql query in PHP:
SELECT *
FROM wp_celebcount
ORDER BY count DESC
I'm trying to display two columns: wp_celebcount.name & wp_celebcount.count
Having trouble getting the result to show with PHP - I want to show this result on my index.php Wordpress theme file. Thanks for the help...
If you're using Wordpress, it would be something like this:
global $wpdb;
$result = $wpdb->get_results('SELECT name, count FROM wp_celebcount');
foreach($result as $row) {
echo 'Name: '.$row->name.', Count: '.$row->count.'<br/>';
}
It's recommended to use the $wpdb global as it takes care of all the database setup for you.
More information on $wpdb can be found here.
mysql_fetch_assoc
Presuming you've done something like
$resultSet = mysql_query('SELECT * FROM wp_celebcount ORDER BY count DESC');
You should be able to pull out the results with
while ($row = mysql_fetch_assoc($resultSet))
{
var_dump($row);
//print an element named 'name' with echo $row['name'];
}
Related
I am using Magento 1.9.0.1 developing a custom extension.
For that purpose i have to ask how can i run a custom MySQL query.
I want to run simple MySQL query with while loop. If it was a simple PHP script i am going to make it this way:
$r = mysql_query("SELECT * FROM `extensa_econt_city`");
while($rowi = mysql_fetch_array($r))
{
$name = $rowi['name'];
$city_id = addslashes($rowi['city_id']);
echo "<option value='$city_id'>$name</option>";
}
With this code in simple PHP i'll get all the rows and make them as options.
I do not know however how can i get the information from table extensa_econt_city which is in the Magento database.
I will use this in a custom template file where i'll display that select menu.
So guys can you please show me how can i run custom MySQL queries with while loop in Magento ?
Thanks in advance!
Try like this
$connection = Mage::getSingleton('core/resource')->getConnection('core_read');
$query = "Select * from `extensa_econt_city`";
$rows = $connection->fetchAll($query);
foreach ($rows as $values) {
$name = $values['name'];
$city_id = addslashes($values['city_id']);
echo "<option value='$city_id'>$name</option>";
}
Ok here's the trick.
In the query I'm getting the right results from a table named messages. (Its fetching the last 10 messages ordered by the time inserted in reversed order) here's the query:
$query = mysql_query("SELECT time, username, message
FROM messages ORDER BY time DESC LIMIT 10");
And than later those messages are printed inside div via ajax with
while ($item = mysql_fetch_assoc($query)) {
echo $item['time']." - ".$item['username']." - ".$item['message'];
}
But the printed result I want to print them only in the reversed order.
Tip: If I use ASC in the ORDER BY clause I don't get the last inserted messages.
Is this possible to do inside php?
Store your data & than use array_reverse,
while($row = mysql_fetch_assoc($result)){
$items[] = $row;
}
$items = array_reverse($items ,true);
foreach($items as $item){
echo $item['time']." - ".$item['username']." - ".$item['message'];
}
I would use a subquery personally. Im kinda anal about having my data come out of MySQL exactly how I want it, but the array_reverse method will work fine too. Here is my example:
$query = mysql_query("SELECT * FROM (
SELECT time, username, message
FROM messages ORDER BY time
DESC LIMIT 10) result
ORDER BY time ASC
");
Use: array_unshift - Prepend one or more elements to the beginning of an array
$items = array();
while($row = mysql_fetch_assoc($result)){
array_unshift($items,$row);
}
foreach($items as $item){
echo $item['time']." - ".$item['username']." - ".$item['message'];
}
Heres a useless but quick example. I hope it's still there: Simple example
Ok, I have the following code:
$array = mysql_query("SELECT artist FROM directory WHERE artist LIKE 'a%'
OR artist LIKE 'b%'
OR artist LIKE 'c%'");
$array_result= mysql_fetch_array($array);
Then, when I try to echo the contents, I can only echo $array_result[0];, which outputs the first item, but if I try to echo $array_result[1]; I get an undefined offset.
Yet if I run the above query through PHPMyAdmin it returns a list of 10 items. Why is this not recognized as an array of 10 items, allowing me to echo 0-9?
Thanks for the help.
That's because the array represents a single row in the returned result set. You need to execute the mysql_fetch_array() function again to get the next record. Example:
while($data = mysql_fetch_array($array)) {
//will output all data on each loop.
var_dump($data);
}
You should be using while to get all data.
$array_result = array();
while ($row = mysql_fetch_array($array, MYSQL_NUM)) {
$array_result[] = $row;
}
echo $array_result[4];
I prefer to use this code instead:
$query = "SELECT artist FROM directory WHERE artist LIKE 'a%'
OR artist LIKE 'b%'
OR artist LIKE 'c%'";
$result = mysql_query($query) or die(mysql_error());
while(list($artist) = mysql_fetch_array($result))
{
echo $artist;
}
If you add more fields to your query just add more variables to list($artist,$field1,$field2, etc...)
I hope it helps :)
I use a mysql database. When I run my query I want to be able to put each row that is returned into a new variable. I dont know how to do this.
my current code:
<?php
$result=mysql_query("SELECT * FROM table WHERE var='$var'");
$check_num_rows=mysql_num_rows($result);
while ($row = mysql_fetch_assoc($result))
{
$solution=$row['solution'];
}
?>
The thing is that check num rows can return a row of an integer 0-infinity. If there are more solutions in the database how can I assign them all a variable. The above code works fine for 1 solution, but what if there are more? Thanks.
You can't give each variable a different name, but you can put them all in an array ... if you don't know how this works I suggest looking at a basic tutorial such as http://www.w3schools.com/php/php_arrays.asp as well as my code.
A very simple way (obviously I haven't included mysql_num_rows etc):
$solutions = array()
while($row = mysql_fetch_assoc($result)) {
$solutions[] = $row['solution'];
}
If you have three in your result solutions will be:
$solutions[0] -> first result
$solutions[1] -> second
$solutions[2] -> third
<?php
$result=mysql_query("SELECT * FROM table WHERE var='$var'");
$solution = array();
$check_num_rows=mysql_num_rows($result);
while ($row = mysql_fetch_assoc($result))
{
$solution[]=$row['solution'];
}
?>
A friend of mine has asked me a question which i do not know how.
The problem is he wants to use a result set of a query more than one time. Whenever he wants.
There is example tables and example output attached.
I will query two times only:
Select * from ornek1_ust
Select * from ornek1_alt
Is it possible to roam in a result set we already have with PHP to have some output like example output. I want to query database with full data for once. Then i want to use it wherever i want whenever i want.
Example Tables:
Example Output:
You'd want to "cache" the results within PHP. To fetch the data, you'd do a simple join query:
SELECT ornek_ust1.isim AS ust, ornek1_alt.ism AS alt
FROM ornek_ust1
JOIN ornek1_alt ON ornek_ust1.id = ornek1_alt.ust_ID
ORDER BY ornek_ust1.isim, ornek1_alt.ism
and within PHP, do something like:
$data = array();
$sql = "SELECT ...";
$res = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($res)) {
$data[$row['ust'][] = $row['alt'];
}
which will build an array that looks like your desired data:
$data = array(
'ust data1' => array(
0 => 'alt data 1'
1 => 'alt data 2'
),
'ust_data2' => ... etc...
)
It sounds like you want to be using mysql_result() to be able to pull out data from a resultset at will.
If you want to loop through the whole dataset and still have it available to loop through again, you can use mysql_data_seek() to set the internal pointer back to the beginning.
Just store the query results in an array....
$sql = "Select * from ornek1_ust";
$results = array();
$query = mysql_query($sql);
while ($row = mysql_fetch_array($query, MYSQL_ASSOC)) {
$results[] = $row;
}
// Use $results further on in your code