0

I want to retrieve info from the following table and display it in a text field

I have a table named items. Within items I have the fields item_number, description, item_id, custom1, custom2, custom3, custom4

How do I echo or print the value of the custom fields?

mysql_connect("####", "####", "####") or die(mysql_error());
mysql_select_db("####") or die(mysql_error());

// Get all the data from the "example" table
$result = mysql_query("SELECT * FROM items") or die(mysql_error());
while($row = mysql_fetch_array($result))
{
  echo '<input type="text" value='.$row['custom1'].'><br/>'; 
}
Paul Roub
  • 35,848
  • 27
  • 79
  • 88
  • Quite a generic question. Assuming you don't know how to connect and run SQL query; have you tried tutorials: http://www.w3schools.com/php/php_mysql_select.asp – Egg Feb 03 '16 at 14:17
  • Possible duplicate of [retrieve data from db and display it in table in php .. see this code whats wrong with it?](http://stackoverflow.com/questions/4331353/retrieve-data-from-db-and-display-it-in-table-in-php-see-this-code-whats-wron) – Nana Partykar Feb 03 '16 at 14:17
  • 1
    Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Feb 03 '16 at 14:31
  • im trying to figure out it u right sql connections – walter humphreys Feb 03 '16 at 14:44
  • this the error im getting A PHP Error was encountered Severity: Notice Message: Trying to get property of non-object Filename: sales/register.php Line Number: 184 0 results – walter humphreys Feb 03 '16 at 14:47
  • you do not require while loop to fetch only one data, – Sanzeeb Aryal Feb 03 '16 at 15:21
  • ok i got it to echo the info of the custom fields, but is echo-ing absolutely all custom fields from different items it should only echo the item that is seleccted and not all. im using following this http://www.w3schools.com/php/php_mysql_select.asp – walter humphreys Feb 03 '16 at 21:56

2 Answers2

0

Taken from the first result on Google:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "sandbox";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT item_number, description, custom1, another_field FROM items";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<p>item_number: " . $row["item_number"]. "<br />description:" . $row["description"]. "<br />custom1: " . $row["custom1"] . "<br />another_field: " . $row["another_field"] . "</p>";
    }
} else {
    echo "0 results";
}
$conn->close();
?>
Egg
  • 1,757
  • 1
  • 12
  • 27
  • i need to echo the custom fields values – walter humphreys Feb 03 '16 at 14:25
  • ... so add them to the code to the `$sql` variable and the echo within the `while` command (answer updated) :) – Egg Feb 03 '16 at 14:28
  • A PHP Error was encountered Severity: Warning Message: mysqli::mysqli(): (28000/1045): Access denied for user '0'@'localhost' (using password: YES) Filename: sales/register.php Line Number: 174 Connection failed: Access denied for user '0'@'localhost' (using password: YES) – walter humphreys Feb 03 '16 at 14:38
  • You'll need to update the first four lines (servername, username, password, dbname) with the correct credentials for your database. – Egg Feb 03 '16 at 14:39
  • A PHP Error was encountered Severity: Notice Message: Trying to get property of non-object Filename: sales/register.php Line Number: 184 0 results – walter humphreys Feb 03 '16 at 14:47
  • 1
    Example updated. You will need to check these field names match what you have in your database. This code works. – Egg Feb 03 '16 at 15:07
  • You're welcome, don't forget to accept the answer & vote up to help others. Thanks. – Egg Feb 04 '16 at 09:50
0

here is my code

  // Create connection
 $conn = new mysqli($servername, $username, $password, $dbname);
 // Check connection
  if ($conn->connect_error) {
   die("Connection failed: " . $conn->connect_error);
  } 

$sql = "SELECT custom1, custom2, custom3 FROM ospos_items  ";
$result = $conn->query($sql);

 if ($result->num_rows > 0) {
 // output data of each row
   while($row = $result->fetch_assoc()) {
    echo "custom:" . $row["custom1"]. " custom2: " .    $row["custom2"]. " custom3 :" . $row["custom3"]. "<br>";
}
 } else {
echo "0 results";
}

$conn->close(); ?>

and it outputs thid

i need it to to only output the customs fields for selected item so i add
$sql = "SELECT custom1, custom2, custom3 FROM ospos_items where item_id = 'item_id' ";

because that is the unique number but it outputs 0 resuls i was thinking if it need an array or something to let it work but im new with php, i used item_id as it listed in within the table