PDA

Click to See Complete Forum and Search --> : Show table data


Daok
Apr 27th, 2003, 06:13 PM
Hello
I am new to php and I wanted to know how to show all the value of my table. I am using mysql.

Here is my code:




<?
// db variables
// inserts variables accordingly
$host = "misterp2d.dyndns.org";
$user = "bye";
$pass = "allo";
$db = "Jeux";
$table = "Usager";

// connecting to the db
$link = mysql_connect ($host, $user, $pass)
or die("Impossible de se connecter : " . mysql_error());

// the db query
$q = "SELECT * from $table";

$results = mysql_select_db ($db);

// fetching the results
$result = mysql_query("SELECT id_Usager, name_Usager FROM Usager");

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

mysql_free_result($result);

// closing the db link
mysql_close ($link);
?>



Here is the errors I got:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in c:\program files\easyphp\www\test.php on line 29

Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource in c:\program files\easyphp\www\test.php on line 34

Daok
Apr 27th, 2003, 07:54 PM
it work now!!!:)

The Hobo
Apr 28th, 2003, 10:12 AM
Since I don't agree with some of the stuff in that code, I thought I'd give my thoughts:

<?php //#1
// db variables
// inserts variables accordingly
$host = "misterp2d.dyndns.org";
$user = "bye";
$pass = "allo";
$db = "Jeux";
$table = "Usager";

// connecting to the db
$link = mysql_connect ($host, $user, $pass)
or die("Impossible de se connecter : " . mysql_error());
mysql_select_db($db, $link); //#2

// the db query
//$q = "SELECT * from $table"; //#3

// fetching the results
$result = mysql_query("SELECT id_Usager, name_Usager FROM Usager");

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

mysql_free_result($result); //#4

// closing the db link
mysql_close ($link); //#5
?>

[list=1]
It is recommended that you not use short tags (<?). See here (http://pear.php.net/manual/en/standards.tags.php).
Since mysql_select_db returns a BOOL, all you're doing is storing true and false, and since you're not checking whether it returned true or false, there's no need to assign it to a value.

If you are connection to more than one database, you might want to get in the habit of specifying a resource identifier in this function as well.
This is not used in the code. Why have it?
This is generally not required in projects unless you are worried about memory. All results are freed when the script finishes execution.
This is not required. The resource is closed when the script finishes execution.
[/list=1]

Daok
May 3rd, 2003, 10:07 PM
Thx Hobo,
PHP is so new to me, I do not understand all, thx man :) I will check all my code to clear it :)

phpman
May 5th, 2003, 01:57 PM
actually, to add on to hobo's insight,

mysql_select_db($db, $link); //#2

that is generally used if you connect to more than one database at a time. other than that, all you need is

mysql_select_db($db);

The Hobo
May 5th, 2003, 01:59 PM
Originally posted by phpman
actually, to add on to hobo's insight,

mysql_select_db($db, $link); //#2

that is generally used if you connect to more than one database at a time. other than that, all you need is

mysql_select_db($db);

I already said that:

If you are connection to more than one database, you might want to get in the habit of specifying a resource identifier in this function as well.

:p

phpman
May 5th, 2003, 02:14 PM
sorry, I didn't even see that line.