0

I am new to phpMySQL and php. and I am trying to get a value from database and put it in a textbox. Currently, I only have 1 value in my table to test the code:

This is my code:

<?php
$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "";
$mysql_database = "dbtest";
$tbl_name="tbltest";

$con = mysql_connect("$mysql_hostname","$mysql_user","$mysql_password");

if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("$mysql_database", $con);
$all = mysql_query("SELECT Balance FROM tbltest");
?>

And then in my HTML side, I have this:

<input type="text" name="Balance" value="<?php echo $all; ?>" />

But when I tried to run the code the value that appears in the textbox is:

Resource Id #4

What did I miss? Thank you for the help

Innistrad
  • 63
  • 1
  • 1
  • 7
  • 1
    Please check the corresponding documentation page http://php.net/mysql_query before you ask. Do that **EVERY TIME** you use a function you don't know how it works – zerkms Jan 27 '14 at 02:13
  • You've done nothing with `$all`. You need to perform standard abstraction from it. `mysql_fetch_row` comes to mind. – Ohgodwhy Jan 27 '14 at 02:14
  • Since you are learning, now is the time to learn the right way. The `mysql_*()` functions are deprecated and will be removed in a future PHP version, at which point all your code would need to be rewritten. You should start learning to use either PDO or MySQLi instead -- whatever tutorial you're working with should be considered outdated. [I'm partial to this PDO tutorial](http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers) – Michael Berkowski Jan 27 '14 at 02:16

2 Answers2

2

Mysql_query() returns a result resource.

When you echo the return, php is converting it into a string. You will need to use a mysql_fetch_array() function:

$all = mysql_query( "SELECT Balance FROM tbltest" );
foreach(mysql_fetch_array($all) as $row)
{
    $balance = $row['Balance'];
}

echo '<input type="text" value="' . $balance . '">';

Also, these functions are depreciated. They are not supported any more. You should look into PDO or mysqli.

Phil Cross
  • 9,017
  • 12
  • 50
  • 84
  • If I do this "$all = mysql_fetch_array("SELECT Balance FROM tbltest");", there is no value in the textbox, thanks >. – Innistrad Jan 27 '14 at 02:25
  • Read the answer again. Your sql query goes in the mysql_query() function. The return of that function ($all) goes in the mysql_fetch_array() function – Phil Cross Jan 27 '14 at 07:38
0

mysql_query does not return the result itself, but returns the resource that holds the result.

You can use mysql_fetch_assoc or other relevant to fetch one row from that resource.

The code should be like: $con = mysql_connect($mysql_hostname, $mysql_user, $mysql_password);

if (!$con) {
    die('Could not connect: ' . mysql_error());
}

mysql_select_db($mysql_database, $con);
$res = mysql_query("SELECT Balance FROM tbltest");

$all = mysql_fetch_assoc($res); //actually $all is not all the result, it's first row.
                                // $all might be like array('Balance' => '100');

and HTML part, as arrays could not be echoed directly('Array' is shown instead), you have to specify the index of $all:

<input type="text" name="Balance" value="<?php echo $all['Balance']; ?>" />