Page 1 of 1

Mysql Metadata

Posted: Mon Jul 04, 2011 2:16 pm
by anon2010
Hi Everybody,

I´m new in PHP-MySql world. I come from the Visual Basic MySql Chrystal Report world.

In vb a have a "solution prototype" that I only need to pass a database as parameter and then program the bussiness logic and from there I hava an application in "seconds" . I´m trying to do the same in PHP and MYSQL and everything went fine until I found the following problem.

I crataed a php page called "editrecord.php" that will recibe a query and from it, it will show the fields from the query in edit mode. But I only have learned so for how to show de fields name but no the fields values or content.

if I send the query "select id,name,address from customers WHERE ID = '01' " then the page shows:

ID _______
NAME______
ADDRESS______

And I want to show:

ID 01
NAME : ORANGEL
ADDRESS: FIRST STREET.

Im usign the metada values as show in the PHP MANUAL

This is the code:

Code: Select all

<h1 align="center">EDICION DE REGISTROS
</h1>
<?php
$aCampos = array();


$conn = mysql_connect('localhost', 'root', 'monosofo');
if (!$conn) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db('bd001');
$result = mysql_query("select * from presupuestos where codpres = '2008'");
if (!$result) {
    die('Query failed: ' . mysql_error());
}
/* get column metadata */
ECHO '<CENTER>';
ECHO '<FORM action="editar.php">';
ECHO '<table width="200" border="1">';
$i = 0;
while ($i < mysql_num_fields($result)) {
//    echo "Information for column $i:<br />\n";      
    $meta = mysql_fetch_field($result, $i);
	
    if (!$meta) {
        echo "No information available<br />\n";
    }
	
ECHO "<TR>";
ECHO "<TD>";
ECHO "$meta->name";
ECHO "</TD>";
ECHO "<TD>";
[color=#FF4000]ECHO "$meta->value" ;
[/color]ECHO '<INPUT TYPE="TEXT"> ' ;
ECHO "</TD>";
ECHO "</TR>";
ECHO $b[1] ;   
    $i++;
}
mysql_free_result($result);
ECHO "</TABLE>";
 ECHO '<INPUT TYPE="submit" VALUE="Actualizar"> ' ;
 ECHO '<INPUT TYPE="submit" VALUE="Cancelar"> ' ;
ECHO '</FORM>';
ECHO '</CENTER>';
?>
IN SHORT: How do I get the field "content" or "value"

Thnak you all

(Excuse My English)

THIS IS WHAT I GET:
Image

AND I NEED THIS:
Image

Re: Mysql Metadata

Posted: Mon Jul 04, 2011 9:36 pm
by califdon
Your difficulty is with the mysql_fetch_field() function. That is only to get information about the field, itself, not the values contained in a field. For that you need to use one of the other functions, such as mysql_fetch_assoc(), mysql_fetch_obj(), etc. You could save the field names in an array and then use those to serve as the left hand column. Or if your fields are going to remain constant, I wouldn't advise using mysql_fetch_field() at all, rather you could hard-code the appropriate descriptions for the form, as well as the field names in your query. For a simple login form, this would be the normal way to do it.

I suggest that you check all the mysql_fetch_...() functions: http://www.php.net/manual/en/ref.mysql.php

Re: Mysql Metadata

Posted: Mon Jul 04, 2011 11:20 pm
by anon2010
Thank you, you put me in the right direction I just did It. Now i´m going to construct the UPDATE string and Voila!!!! I´ll share the result with you...