PUG
PUG

Reputation: 4472

Select * wont print anything in php mysql

i made a php script as below ran it and ouput is Record Added to Table

<?PHP

$user_name = "root";
$password = "";
$database = "test_db";
$server = "127.0.0.1";
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);

if ($db_found) {

$SQL = "INSERT INTO name (FirstName) VALUES ('bill')";
$result = mysql_query($SQL);

mysql_close($db_handle);

print "Records added to the database";
}
else {
print "Database NOT Found ";
mysql_close($db_handle);
}

?>

now to see the content of the table i did

<?PHP

$user_name = "root";
$password = "";
$database = "test_db";
$server = "127.0.0.1";
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);

if ($db_found) {

$SQL = "Select * from name";
$result = mysql_query($SQL);

print "$result";
mysql_close($db_handle);

print "Records added to the database";
}
else {
print "Database NOT Found ";
mysql_close($db_handle);
}

?>

But the output is Resource id #3Records added to the database, while i want to see the contents of the table

Upvotes: 1

Views: 261

Answers (5)

Teodor Talov
Teodor Talov

Reputation: 1943

If you want to select from the database try this:

<?PHP

$user_name = "root";
$password = "";
$database = "test_db";
$server = "127.0.0.1";
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);

if ($db_found) {

$SQL = "Select * from name";
$result = mysql_query($SQL);

 while ($row = mysql_fetch_array($result))
 {
  print $row[0]." Record is selected";
 }
mysql_close($db_handle);


}
else {
print "Database NOT Found ";
mysql_close($db_handle);
}

?>

Upvotes: 1

knittl
knittl

Reputation: 265845

mysql_query() will return a resultset object which you have to read in a loop to get its rows:

while($row = mysql_fetch_array()) {
   echo htmlspecialchars($row['column1']);
}

Upvotes: 1

rexem
rexem

Reputation: 109

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
    printf("Result: %s", $row[0]);  
}

Upvotes: 1

jcarlosn
jcarlosn

Reputation: 408

The return value of mysql_query can not be sent to the output like you try.

mysql_query returns a resultset, to be used with http://www.php.net/mysql_fetch_row for example.

Example:

while($row=mysql_fetch_array($result)) {
    print_r($row);
}

Upvotes: 3

manji
manji

Reputation: 47978

$result contains the return value from mysql_query which is a resource ( http://php.net/manual/en/function.mysql-query.php)

use mysql_fetch_assoc($result) or mysql_fetch_array($result) or ... to have the actual data.

Upvotes: 1

Related Questions