Adrian M.
Adrian M.

Reputation: 7443

Select one value from database

I have the code bellow, it's ok but I want to be able to use for example the 4th value extracted from the database, use it alone, not put all of them in a list, I want to be able to use the values from database individually. How do I echo them?

Edit: I was thinking to simplify things, to be able to add the values from database into one array and then extract the value I need from the array (for example the 4th - ordered by "order_id"). But how?

Right now I can only create a list with all the values one after the other..

(Sorry, I am new to this). Thank you for all your help..

<?php
include '../../h.inc.php';
$con = mysql_connect($db['host'],$db['user'],$db['passwd']);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("database", $con);

$result = mysql_query("SELECT * FROM options WHERE Name LIKE 'x_swift%' ORDER BY order_id");

echo "<table border='1'>
<tr>
<th>Values</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
//  echo "<td>" . $row['Name'] . "</td>";
  echo "<td>" . $row['VALUE'] . "</td>";
  echo "</tr>";
$array = array(mysql_fetch_array($strict));
  }
echo "</table>";

mysql_close($con);
?>

Upvotes: 1

Views: 11964

Answers (2)

user428517
user428517

Reputation: 4193

To select the value in the value column of the row where order_id is 4, use this SQL:

$query = 'select value from options where order_id = 4';

Then you can access this result in many ways. One is to get the entire result row (which in this case is just one cell) as an associative array:

if ($result = mysql_query($query)) {
    $row = mysql_fetch_assoc($result);
    echo 'value = ' . $row['value'];
}

You can also get the value directly:

if ($result = mysql_query($query)) {
    echo 'value = ' . mysql_result($result, 'value');
}

Upvotes: 2

Kylie
Kylie

Reputation: 11749

It would just be a query like...

$result = mysql_query("SELECT * FROM options WHERE ID = 3");
mysql_fetch_row($result);

Unless Im misunderstanding you....let me know

But you really should use PDO, instead of deprecated mysql_* functions

http://php.net/manual/en/book.pdo.php

Upvotes: 0

Related Questions