Stupid PHP question

software development

#1

Ok, I’ve got a real simple question about a php script I’m trying to develop.
I’m trying to access a database, and display information based on the search. This is the code I am using to query teh DB and display the info regarding the query:

[code]$query = “select * from DB_TABLE where domain=$domain”;
$result = mysql_db_query (“DATABASE_NAME”, $query);

echo “
$domain

$username

$password

$storefront

$admin

”;
[/code]Is there any reason that this code wouldn’t work at all? I can get no results using this.

Thanks.

================================
I’d give my right arm to be ambidexterous!


#2

The PHP variables are not automatically created by the mysql_query function.

You’ll need to create them based on the result identifier $result. Here’s a possibility:

$query = “select * from DB_TABLE where domain=’$domain’”;

$result = mysql_query ($query);

while($line=mysql_fetch_array($result) {
echo “
$line[‘domain’]

$line[‘username’]

$line[‘password’]

$line[‘storefront’]

$line[‘admin’]

”;
}

… where domain, username, etc. are the fields of the table.

MMORPG Strategy Guides by guidefox.com


#3

[quote]$query = “select * from DB_TABLE where domain=’$domain’”;

$result = mysql_query ($query);

while($line=mysql_fetch_array($result) {
echo “
$line[‘domain’]

$line[‘username’]

$line[‘password’]

$line[‘storefront’]

$line[‘admin’]

”;
}[/quote]
That won’t work either. Try this:

$query = "select * from DB_TABLE where domain='$domain'"; $result = mysql_query($query); while($line=mysql_fetch_array($result)) { echo $line['domain']."\n"; echo $line['username']."\n"; echo $line['password']."\n"; echo $line['storefront']."\n"; echo $line['admin']; }---------------
Simon Jessey
Keystone Websites | si-blog


#4

Touché.

Include the br tags, and you’ll be fine.

MMORPG Strategy Guides by guidefox.com


#5

It definitly did something, but now I’m getting this error:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource It would appear to me $result cannot be used for the argument in this case.
Does this have anything to do with the fact that I’m only using one form field to base the search on (I know it shouldn’t, but it might change the code somewhere)?

Anyway, thanks again everyone! As is obvious I am still very new at the whole PHP game, I’m trying :slight_smile:

================================
I’d give my right arm to be ambidexterous!


#6

Maybe you are not connecting to the database properly. Try dumping your data with this script, to see if it works:

[code]<?php

$hostname = “”; // eg. mysql.yourdomain.com (unique)
$username = “”; // the username specified when setting-up the database
$password = “”; // the password specified when setting-up the database
$database = “”; // the database name chosen when setting-up the database (unique)

$link = mysql_connect($hostname,$username,$password);
mysql_select_db($database) or die(“Unable to select database”);

$sql = “SELECT * FROM tablename”;
$result = mysql_query($sql,$link) or die("Unable to select: ".mysql_error());
print “

\n”;
while($row = mysql_fetch_row($result)) {
print “\n”;
foreach($row as $field) {
print “\n”;
}
print “\n”;
}
print “
$field
\n”;
mysql_close($link);

?>[/code]That script is taken from a write-up I did on the wiki.


Simon Jessey
Keystone Websites | si-blog


#7

Awesome!!
Thanks a bunch. With on minor change - $sql = “SELECT * FROM tablename” WHERE domain=’$domain’; - the variable is passed from the search script to the dump script and all works exactly! Just one question - do I still need to use the while statement if I know there can only be ONE value returned? Does it really make a difference? Ok, so I asked two questions… sorry.
Now to work on getting it to return a default message if the searched domain does not exist in the DB. I know… it’s easy for all you programmers… give me chance to hack away at this myself for a bit. I’m sure I’ll screw something up and come back here, tail between my legs, asking for help :slight_smile: .

Again, thanks for all the help!

  • Man I get so worked up about such simple things -

================================
I’d give my right arm to be ambidexterous!


#8

You don’t need to use the while loop if you know there is only one result. But it doesn’t hurt to have it. If at a later date you know you maybe need two then you’ll probably pull your hair trying to figure where it’s only displaying one :wink:

Also, you might want to use
$sql = “SELECT * FROM tablename” WHERE domain=’".addslashes($domain)."’;

Because if someone enters a ’ in the search box, it’ll probably break your script (although DH seem to add slashes automatically for some reason).