Need Help on Outputting SQL

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

Post Reply
admaster
Forum Newbie
Posts: 12
Joined: Tue Nov 28, 2006 7:16 pm

Need Help on Outputting SQL

Post by admaster »

I have an sql database with a table in it. This table has two rows. ONe is username and another is password. I want to have a php page that dynamically displays all of the information under the username category.

I tried to create a code, but it does not work. The code I tried is:

Code: Select all

<?php
$link = mysql_connect('localhost', 'root', 'my_password_was_here');

  $sql = "SELECT username FROM beta";
  $query = mysql_query($sql, $var);

    while($row = mysql_fetch_row($query)) {
  
    echo $row[0].'<br />';

  }

?>
However, it gives these errors:

Code: Select all

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in C:\Program Files\xampp\htdocs\php\db_test2.php on line 5

Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in C:\Program Files\xampp\htdocs\php\db_test2.php on line 7
Thanks. :P
Flamie
Forum Contributor
Posts: 166
Joined: Mon Mar 01, 2004 3:19 pm

Post by Flamie »

$var doesnt exist :) I think you want to use $link instead ;)
admaster
Forum Newbie
Posts: 12
Joined: Tue Nov 28, 2006 7:16 pm

Post by admaster »

Now this:

Code: Select all

Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in C:\Program Files\xampp\htdocs\php\db_test2.php on line 7
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

missing mysql_select_db

Code: Select all

<?php
$link = mysql_connect('localhost', 'root', 'my_password_was_here') or die(mysql_error());
mysql_select_db('name_of_the_database', $link) or die(mysql_error());

$query = "SELECT username FROM beta";
$result = mysql_query($query, $link) or die(mysql_error().': '.$query);


while( $row=mysql_fetch_array($result) ) {
	echo $row['username'], "<br />\n";
}
?>
admaster
Forum Newbie
Posts: 12
Joined: Tue Nov 28, 2006 7:16 pm

Post by admaster »

Thanks volka. It works now. :D
Post Reply