|
-
Apr 27th, 2003, 06:13 PM
#1
Thread Starter
Ya ya Baby!!!Me is Back
Show table data
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:
PHP 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
-
Apr 27th, 2003, 07:54 PM
#2
Thread Starter
Ya ya Baby!!!Me is Back
it work now!!!
-
Apr 28th, 2003, 10:12 AM
#3
Stuck in the 80s
Since I don't agree with some of the stuff in that code, I thought I'd give my thoughts:
PHP Code:
<?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.[*]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]
-
May 3rd, 2003, 10:07 PM
#4
Thread Starter
Ya ya Baby!!!Me is Back
Thx Hobo,
PHP is so new to me, I do not understand all, thx man I will check all my code to clear it
-
May 5th, 2003, 01:57 PM
#5
Frenzied Member
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);
-
May 5th, 2003, 01:59 PM
#6
Stuck in the 80s
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.
-
May 5th, 2003, 02:14 PM
#7
Frenzied Member
sorry, I didn't even see that line.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|