Am making a really simple php page which pulls data from a really simple database and displays it as charts for a wall monitor.
I've got the data from the db, no problem, but I seem to be struggling splitting that data up
For now i'm just echoing the data to make sure i have what i need.
My code looks like this:
<?php
mysql_connect("host", "user", 'password' or die(mysql_error());
mysql_select_db("databax") or die(mysql_error());
$dbdata = mysql_query("SELECT * FROM dbcpman_resources")
or die(mysql_error());
$column = mysql_fetch_array( $dbdata );
echo $column[0]."<br>";
echo $column[1]."<br>";
echo $column[2]."<br>";
echo $column[3]."<br>";
echo $column[4]."<br>";
echo $column[5]."<br>";
?>
Indeed, it works - it will echo data from the database, but as i've not specified the row anywhere, its just giving me the first row.
I need to be able to work with each row seperately.
There will only ever be 6 rows in this table.
So can anyone help me out with how I go about replicating this for rows 2,3,4,5 and 6?
Thanks in advance!! :)
You need to loop over the result set. The easiest way is to use a while loop as this automatically terminates at the end of the result set, like this.
while ( $row = mysql_fetch_array( $dbdata );
echo $row [0]."<br>";
echo $row [1]."<br>";
echo $row [2]."<br>";
echo $row [3]."<br>";
echo $row [4]."<br>";
echo $row [5]."<br>";
}
Also if you were to change the function that returns the resuilts to use mysql_fetch_assoc() you can reference each field with the name it has on the database so the code is easier to read, like this:
I dont know your field names so I made some up.
while ( $row = mysql_fetch_array( $dbdata );
echo $row ['name']."<br>";
echo $row ['date']."<br>";
echo $row ['time']."<br>";
echo $row ['value1']."<br>";
echo $row ['value2']."<br>";
echo $row ['value3']."<br>";
}
First of all, I'd rather use mysql_fetch_assoc() instead of mysql_fetch_array() since it doesn't srew up your result, if the table structure changes.
It would be even better if you used either mysqli or PDO instead of mysql_* functions, since they are marked deprecated already!
Second please note, that either function just fetches ONE record from your resultset at a time. To fetch all records try the following:
$records = array();
while($row = mysql_fetch_assoc($dbdata)) {
$records[] = $row;
}
You can do a print_r($records); to see what's inside $records after fetching all.
put this line $column = mysql_fetch_array( $dbdata ); in while loop like this
while($column = mysql_fetch_array( $dbdata ))
{
echo $column[0]."<br>";
echo $column[1]."<br>";
echo $column[2]."<br>";
echo $column[3]."<br>";
echo $column[4]."<br>";
echo $column[5]."<br>";
}
Related
I'm a big noob here, so I'm trying to figure it out as a I go.
I want to take a SQL request for "id, fist and last" and store each of those in a variable.
the next half of the code would be doing things with those variable.
The lower statements are simply to see if the var is begin assigned... Apparently it is not, but I get no error, just the blank lines. How can I get the info in a set to do something with?
$newIDs = mysql_query("SELECT per_ID, per_FirstName, per_LastName FROM person_per WHERE DATE_SUB(NOW(),INTERVAL 6 MONTH)<per_FriendDate ORDER BY per_FriendDate DESC") or die(mysql_error());
while($row = mysql_fetch_assoc($newIDs)){
echo $row ['per_ID'] = $per_ID;
echo $row ['per_FirstName'] = $per_FirstName;
echo $row ['per_LastName'] = $per_LastName;
//below is for testing purposes only
echo $per_FirstName;
echo "<br/>";
}
print $per_ID;
echo $per_LastName;
I'm thinking you wanted something more like this for your test:
while ($row = mysql_fetch_assoc($newIDs)) {
$per_ID = $row['per_ID'];
$per_FirstName = $row['per_FirstName'];
$per_LastName = $row['per_LastName'];
// below is for testing purposes only
echo $per_FirstName;
echo "<br/>";
}
When you actually want to keep all the results from your query, you'll need to do something like:
$rows = array();
$i = 0;
while ($row = mysql_fetch_assoc($newIDs)) {
$rows[$i] = $row;
// below is for testing purposes only
echo $rows[$i]['per_LastName'];
echo "<br/>";
$i++;
}
Also, you should note that mysql_fetch_assoc() is actually a deprecated PHP function, according to the manual page: http://php.net/manual/en/function.mysql-fetch-assoc.php
echo $row ['per_ID'] = $per_ID;
should be
$per_ID=$row['per_ID'];
you echo of $per_ID; should then work
As its in a loop you will overwrite $per_ID each time and end up wit the last value.
It looks like your problem is in these statements:
echo $row ['per_ID'] = $per_ID;
echo $row ['per_FirstName'] = $per_FirstName;
echo $row ['per_LastName'] = $per_LastName;
The equals sign there assigns the value of variable $per_ID (which at that time is unassigned) to the array. That's not what you want.
Instead you probably want something like this:
$per_ID = $row ['per_ID'];
Things get further complicated by the fact that you have a while loop in which you would keep writing to the same three variables. So after the loop ends you would only have the value of the last set of fields.
it's just fetching the first element from the table.
Table name is categories which contains 2 columns : id, category
I cant understand why is it fetching just first row from the table.
<?php
$sql = "SELECT category FROM categories";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
//print_r($row);
?>
You need to iterate through the result set in order to retrieve all the rows.
while($row = mysql_fetch_assoc($result)) {
print($row);
}
Also, stop using mysql_ functions. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which.
Use this in while loop :
while ($row = mysql_fetch_assoc($result)) {
print_r($row);
}
Just like you wrote mysql_fetch_assoc($result); will get only one row. You have to use loop to get it all.
If you call mysql_fetch_assoc just once, you'll get only the first row...
while(false !== $row = mysql_fetch_assoc($result)) {
print_r($row)
}
The function you are using only does one record.
Try. ..while($row = mysqli_fetch_array($result))
{
echo $row['id'] . " " . $row['category'];
echo "";
}
For retrieve result set Use loop as per your need
foreach
Use when iterating through an array whose length is (or can be) unknown.
as
foreach($row as $val)
{
echo $val;
}
for
Use when iterating through an array whose length is set, or, when you need a counter.
for(i=0;i<sizeof($row);i++)
{
echo $row[$i];
}
while
Use when you're iterating through an array with the express purpose of finding, or triggering a certain flag.
while($row=mysqli_fetch_array($query))
{
echo $row['flag'];
}
Here is my PHP MySQL query:
$query = "SELECT falsegoto FROM timeconditions WHERE [timeconditions_id] = 0";
$result = mysql_query($query);
There should be only a single result from this query, and I'm not sure how display it in PHP?
mysql_result() seems to only work with larger data sets?
Any help or explanation would be valued.
As Peeha mentiod, your using mysql, but it's better to use mysqli
So the code will then look like this:
$query = "SELECT falsegoto FROM timeconditions WHERE [timeconditions_id] = 0";
$result = mysqli_query($query);
while($row = myslqi_fetch_assoc($result){
// DO STUFF
}
I use this for everything. It just loops through every row in the result. and if there's just one row, it while's only one time....
use it like this :
while ($row = mysql_fetch_assoc($result)) {
echo $row['firstname'];
echo $row['lastname'];
echo $row['address'];
echo $row['age'];
}
You have to fetch the row, there are a few methods of doing it: mysql_fetch_object, mysql_fetch_row, mysql_fetch_array and mysql_fetch_assoc.
These methods will read a single line from the result and remove it from the handler, so if you loop the call it will read all the rows, one by one until it reaches the end and returns false.
example:
while($obj = mysql_fetch_object($result)){
echo $obj->name;
}
PHP.net documentation:
mysql_fetch_object,
mysql_fetch_row,
mysql_fetch_array,
mysql_fetch_assoc
I have the following query:
$result = mysql_query("SELECT option_value FROM wp_10_options WHERE option_name='homepage'");
$row = mysql_fetch_array($result);
print_r ($row);
and the output I am getting is:
Resource id #2
Ultimately, I want to be able to echo out a single field like so:
$row['option_value']
Without having to use a while loop, as since I am only trying to get one field I do not see the point.
I have tried using mysql_result with no luck either.
Where am I going wrong?
Try with mysql_fetch_assoc .It will returns an associative array of strings that corresponds to the fetched row, or FALSE if there are no more rows. Furthermore, you have to add LIMIT 1 if you really expect single row.
$result = mysql_query("SELECT option_value FROM wp_10_options WHERE option_name='homepage' LIMIT 1");
$row = mysql_fetch_assoc($result);
echo $row['option_value'];
$result = mysql_query("SELECT option_value FROM wp_10_options WHERE option_name='homepage'");
$row = mysql_fetch_assoc($result);
echo $row['option_value'];
Functions mysql_ are not supported any longer and have been removed in PHP 7. You must use mysqli_ instead. However it's not recommended method now. You should consider PDO with better security solutions.
$result = mysqli_query($con, "SELECT option_value FROM wp_10_options WHERE option_name='homepage' LIMIT 1");
$row = mysqli_fetch_assoc($result);
echo $row['option_value'];
use mysql_fetch_assoc to fetch the result at an associated array instead of mysql_fetch_array which returns a numeric indexed array.
Though mysql_fetch_array will output numbers, its used to handle a large chunk.
To echo the content of the row, use
echo $row['option_value'];
Try this one if you want to pick only one option value.
$result = mysql_query("SELECT option_value FROM wp_10_options WHERE option_name='homepage'");
$row = mysql_fetch_array($result);
echo $row['option_value'];
What you should get as output with this code is:
Array ()
... this is exactly how you get just one row, you don't need a while loop. Are you sure you're printing the right variable?
Ultimately, I want to be able to echo out a signle field like so:
$row['option_value']
So why don't you? It should work.
It is working for me..
$show = mysql_query("SELECT data FROM wp_10_options WHERE
option_name='homepage' limit 1"); $row = mysql_fetch_assoc($show);
echo $row['data'];
is this is a WordPress?
You shouldn't do it like you've done!
To get option from DB use get_option!
this shoude work
<?php
require_once('connection.php');
//fetch table rows from mysql db
$sql = "select id,fname,lname,sms,phone from data";
$result = mysqli_query($conn, $sql) or die("Error in Selecting " . mysqli_error($conn));
//create an array
$emparray = array();
for ($i = 0; $i < 1; $i++) {
$row =mysqli_fetch_assoc($result);
} $emparray[] = $row;
echo $emparray ;
mysqli_close($connection);
?>
make sure your ftp transfers are in binary mode.
I have a query which is designed to retireve the "name" field for all records in my "tiles" table but when I use print_r on the result all I get is the first record in the database. Below is the code that I have used.
$query = mysql_query("SELECT name FROM tiles");
$tiles = mysql_fetch_array($query);
I really cant see what I have done wrong, I have also tried multiple searches within google but I cant find anything useful on the matter at hand.
<?php
// Make a MySQL Connection
$query = "SELECT * FROM example";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo $row['name']. " - ". $row['age'];
echo "<br />";
}
?>
'mysql_fetch_array'
Returns an array that corresponds to the fetched row and moves the internal data pointer ahead.
This means that it returns array (contains values of each field) of A ROW (a record).
If you want other row, you call it again.
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
// Do something with $row
}
Hope this helps. :D
Use "mysql_fetch_assoc" instead of "mysql_fetch_array".
$query = mysql_query('SELECT * FROM example');
while($row = mysql_fetch_assoc($query)) :
echo $row['whatever'] . "<br />";
endwhile;
I believe you need to do a loop to invoke fetch array until it has retrieved all the rows.
while ($row = mysql_fetch_array($query) ) {
print_r( $row );
}