PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
doggy
Forum Commoner
Posts: 80 Joined: Tue Dec 09, 2003 5:01 am
Location: South Africa
Post
by doggy » Wed Jan 21, 2004 12:38 am
I get this error on this line
///// mainfile.php ////
Code: Select all
<?php
class DB {
function DB() {
$this->host = "localhost";
$this->db = "phpatlas";
$this->user = "root";
$this->pass = "root";
$this->link = mysql_connect($this->host, $this->user, $this->pass);
mysql_select_db($this->db);
}
}
?>
/// test.php ///
Code: Select all
<?php
include("mainfile.php");
$DB = new DB;
$DB->$sql = mysql_query("SELECT id, testfield FROM test");
while (
$DB->$row = mysql_fetch_array($sql)) {
printf("ID: %s Name: %s", $row["id"], $row["testfield"]);
}
?>
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\FoxServ\www\phpatlas\test.php on line RED
doggy
Forum Commoner
Posts: 80 Joined: Tue Dec 09, 2003 5:01 am
Location: South Africa
Post
by doggy » Wed Jan 21, 2004 12:59 am
and this just gives me a blank page again
Code: Select all
<?php
include("mainfile.php");
$DB = new DB;
$DB->sql = "SELECT id, testfield FROM test";
$DB->result = mysql_query($DB->sql);
$DB->row = mysql_fetch_row($DB->result);
$id = $DB->row[id];
$testfield = $DB->row[testfield];
echo "$id<br>";
echo "$testfield<br>";
?>
LonelyProgrammer
Forum Contributor
Posts: 108 Joined: Sun Oct 12, 2003 7:10 am
Post
by LonelyProgrammer » Wed Jan 21, 2004 1:04 am
Alas, the code shall read as:
Code: Select all
while (
$DB->$row = mysql_fetch_array([b]$DB->sql[/b])) {
printf("ID: %s Name: %s", $row["id"], $row["testfield"]);
}
Actually, if you are already using a class, why not have:
Code: Select all
class DB
{
var $result;
// ....constructors and blah...
function query($query)
{
$this->result = mysql_query($query)
}
function getArray()
{
return mysql_fetch_array($result);
}
// foobar foobar foobar....
Just a suggestion
LonelyProgrammer
Forum Contributor
Posts: 108 Joined: Sun Oct 12, 2003 7:10 am
Post
by LonelyProgrammer » Wed Jan 21, 2004 1:09 am
Corrections made in
bold .
<?php
include("mainfile.php");
$DB = new DB;
$DB->sql = "SELECT id, testfield FROM test";
$DB->result = mysql_query($DB->sql);
$DB->row = mysql_fetch_array ($DB->result);
$id = $DB->row["id" ];
$testfield = $DB->row["testfield" ];
echo "$id<br>";
echo "$testfield<br>";
?>
mysql_fetch_array will return an array with key values same as the column headers.
mysql_fetch_row will return an indexed array. So, you shall code the above as:
Code: Select all
<?php
include("mainfile.php");
$DB = new DB;
$DB->sql = "SELECT id, testfield FROM test";
$DB->result = mysql_query($DB->sql);
$DB->row = mysql_fetch_row($DB->result);
$id = $DB->row[[b]0[/b]];
$testfield = $DB->row[[b]1[/b]];
echo "$id<br>";
echo "$testfield<br>";
?>
Hope this is of help!
doggy
Forum Commoner
Posts: 80 Joined: Tue Dec 09, 2003 5:01 am
Location: South Africa
Post
by doggy » Wed Jan 21, 2004 1:13 am
cant some one maybe just help me out to get a samply select script working with that mainfile i have ... please that i just can see and then play around with the script. ok thank you so much guys.
doggy
Forum Commoner
Posts: 80 Joined: Tue Dec 09, 2003 5:01 am
Location: South Africa
Post
by doggy » Wed Jan 21, 2004 1:15 am
thank you that last post before i made a post helped so much thanks man
doggy
Forum Commoner
Posts: 80 Joined: Tue Dec 09, 2003 5:01 am
Location: South Africa
Post
by doggy » Wed Jan 21, 2004 1:21 am
but now how can i display all the rows in the db lets say my table looks like this
id testfield
1 2213
2 21321
3 1323
4 21312
how will i display all that info .
LonelyProgrammer
Forum Contributor
Posts: 108 Joined: Sun Oct 12, 2003 7:10 am
Post
by LonelyProgrammer » Wed Jan 21, 2004 1:55 am
You almost done it with your previous code. Just add some HTML formatting. If you don't know HTML, it's a good time to learn some.
doggy
Forum Commoner
Posts: 80 Joined: Tue Dec 09, 2003 5:01 am
Location: South Africa
Post
by doggy » Wed Jan 21, 2004 1:57 am
???
doggy
Forum Commoner
Posts: 80 Joined: Tue Dec 09, 2003 5:01 am
Location: South Africa
Post
by doggy » Wed Jan 21, 2004 1:59 am
Code: Select all
<?php
include("mainfile.php");
$DB = new DB;
$DB->sql = "SELECT * FROM test";
$DB->result = mysql_query($DB->sql);
$DB->row = mysql_fetch_array($DB->result);
$id = $DB->row[id];
$testfield = $DB->row[testfield];
echo "<table width="100%" bgcolor="orange"><tr><td>";
echo "<table width="100%" bgcolor="lightyellow"><tr><td bgcolor="orange"><b>id</b></td><td bgcolor="orange"><b>field</b></td></tr>";
echo "<tr><td>$id</td>";
echo "<td>$testfield</td></tr>";
echo "</table>";
echo "</td></tr></table>";
?>
doggy
Forum Commoner
Posts: 80 Joined: Tue Dec 09, 2003 5:01 am
Location: South Africa
Post
by doggy » Wed Jan 21, 2004 3:44 am
thanks guys i have found the right script that works very good ... or not found but i wrote this ... hehe and i don`t even know php that well , you see what people can do with a little help.
Code: Select all
<?php
include("mainfile.php");
$DB = new DB;
$DB->sql = "SELECT * FROM users";
$DB->result = mysql_query($DB->sql);
echo "<table width="100%" bgcolor="orange"><tr><td>";
echo "<table width="100%" bgcolor="lightyellow" border="1"><tr>";
echo "<td bgcolor="orange"><b>id</b></td><td bgcolor="orange"><b>account</b></td>";
echo "<td bgcolor="orange"><b>name</b></td><td bgcolor="orange"><b>surname</b></td>";
echo "<td bgcolor="orange"><b>age</b></td><td bgcolor="orange"><b>sex</b></td>";
echo "<td bgcolor="orange"><b>lang</b></td><td bgcolor="orange"><b>email</b></td>";
echo "<td bgcolor="orange"><b>homea</b></td><td bgcolor="orange"><b>homet</b></td>";
echo "<td bgcolor="orange"><b>worka</b></td><td bgcolor="orange"><b>workt</b></td>";
echo "<td bgcolor="orange"><b>postala</b></td><td bgcolor="orange"><b>medname</b></td>";
echo "<td bgcolor="orange"><b>mednb</b></td></tr>";
while($DB->row = mysql_fetch_array($DB->result)) {
$id = $DB->row[id];
$account = $DB->row[account];
$name = $DB->row[name];
$surname = $DB->row[surname];
$age = $DB->row[age];
$sex = $DB->row[sex];
$lang = $DB->row[lang];
$email = $DB->row[email];
$homea = $DB->row[homea];
$homet = $DB->row[homet];
$worka = $DB->row[worka];
$workt = $DB->row[workt];
$postala = $DB->row[postala];
$medname = $DB->row[medname];
$mednb = $DB->row[mednb];
echo "<tr><td>$id</td>";
echo "<td>$account</td>";
echo "<td>$name</td>";
echo "<td>$surname</td>";
echo "<td>$age</td>";
echo "<td>$sex</td>";
echo "<td>$lang</td>";
echo "<td>$email</td>";
echo "<td>$homea</td>";
echo "<td>$homet</td>";
echo "<td>$worka</td>";
echo "<td>$workt</td>";
echo "<td>$postala</td>";
echo "<td>$medname</td>";
echo "<td>$mednb</td></tr>";
}
echo "</table>";
echo "</td></tr></table>";
?>