mysql fetch array prob (again)

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

weasel
Forum Commoner
Posts: 27
Joined: Sat Aug 16, 2003 3:27 pm

mysql fetch array prob (again)

Post by weasel »

<?php

include('config.php');

$result = mysql_query("SELECT name, mname, discription FROM maps where id = 1");
$row = mysql_fetch_array($result);
$name = $row["name"];
$mname = $row["mname"];
$dis = $row["discription"];

echo $row[name];
echo $row[mname];
echo $row[discription];
?>



<table>
<tr>
<td>
map name:
</td>
<td>
<?php echo $row["mname"]; ?>
</td>
</tr>
<tr>
<td>
Map creator:
</td>
<td>
<?php echo $row["name"]; ?>
</td>
</tr>
</table>

this is the code im using... and i get this error

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/triggerh/public_html/maps/layout.php on line 6

any help please?
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

First off: Does config.php connect you to the mySQL server and select the correct database for you? If it doesn't you will need to do that.

Second:

Try this:

Code: Select all

$result = mysql_query("select name, mname, discription from maps where id='1'");
if ($result)
&#123;
  $row = mysql_fetch_array($result);
  $name = $row&#1111;"name"];
  $mname = $row&#1111;"mname"];
  $dis = $row&#1111;"discription"];

  echo $name;
  echo $mname;
  echo $dis;
&#125;
else &#123; &#125;
instead of:

Code: Select all

$result = mysql_query("SELECT name, mname, discription FROM maps where id = 1"); 
$row = mysql_fetch_array($result); 
$name = $row&#1111;"name"]; 
$mname = $row&#1111;"mname"]; 
$dis = $row&#1111;"discription"]; 

echo $row&#1111;name]; 
echo $row&#1111;mname]; 
echo $row&#1111;discription];
weasel
Forum Commoner
Posts: 27
Joined: Sat Aug 16, 2003 3:27 pm

Post by weasel »

this is what the table looks like....

id name mname discription dllink
1 sleepwalker test.bms test discription http://testlink.com


the id was hopfully for the map and have it auto increase by 1... (to easily add more maps)

ok... if your wondering... this is what im trying to do...(ill just copy and past from where i posted this on mss...

1. User registration/login
2. User upload(to upload there maps, and assign them info)(only be abil to upload .zip and .bms, .bms is the file type that novalogic uses for its maps)
3. An admin approval system(to make sure no one enters abusive content for map info)
4. an easy to use rating system
5. must be logged in to vote
6. posibly use phpBB table(so people can just use the forum to register, and be abil to add/vote on maps)(this being an option)

7. has 20 or so maps per page

8. when it reaches 20 maps per page it makes a new page, and has like a ... <| 1 2 3 |> type of a deal...

9. can sort between the diffrent map types (tdm, dm, tkoth, koth, ctf, a&d) and im sure thers a few more im missing...

and i would like for it to read the info out of a mysql table...


so far i have the user log in, and the news system...
and working on the map view/download/rating page...(thats what this will be)

well thx for any help
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

Did my code not work?
weasel
Forum Commoner
Posts: 27
Joined: Sat Aug 16, 2003 3:27 pm

Post by weasel »

nope, a guy from mss said that $row wasent defined... whatever that means :?
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

Hmm... what is mss? I belive row was defined on this line:

Code: Select all

$row = mysql_fetch_array($result);
weasel
Forum Commoner
Posts: 27
Joined: Sat Aug 16, 2003 3:27 pm

Post by weasel »

ok... instead of using the include config.php... i put this

<?php

$dbhost = "localhost"; // host name, usually localhost
$dbuser = "triggerh_maps"; // username used to login
$dbpwd = "maps"; // password used to login
$dbname = "triggerh_THCSTATS"; // the name of your database

$result = mysql_query("select name, mname, discription from maps");
if ($result)
{
$row = mysql_fetch_array($result);
$name = $row["name"];
$mname = $row["mname"];
$dis = $row["discription"];

echo $name;
echo $mname;
echo $dis;
}
else { }
?>


<table>
<tr>
<td>
map name:
</td>
<td>
<?php echo $row["mname"]; ?>
</td>
</tr>
<tr>
<td>
Map creator:
</td>
<td>
<?php echo $row["name"]; ?>
</td>
</tr>
</table>


i diddent get any errors, but it diddent work...

heres there link
http://triggerhappycheaters.com/maps/layout1.php
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

It didn't work because you never utilized your host, user, pass, etc. vars.

You forgot:

Code: Select all

mysql_connect($dbhost, $dbuser, $dbpwd);

mysql_select_db($dbname);
See what I mean?
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

row not defined

Post by phpScott »

$row isn't defined because there is more then likely a problem with you query.
If you have access to phpMyAdmin cut and paste the query in there to see if it comes up with a error as it will tell you.
Another thing to watch for is capitalization in your query. If table names or columns are using capitals then your query must as well.
-------->OK capitalization must be correct on unix systems and windows is
more forgiving.



If none of those work then try to do a really basic search like

Code: Select all

SELECT * FROM maps
or something to see if there is any results coming from the table.

phpScott
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

Scott < He didn't even connect to the dbase. Isn't that why $row wasn't defined?
weasel
Forum Commoner
Posts: 27
Joined: Sat Aug 16, 2003 3:27 pm

Post by weasel »

!!! ty very much!
so i dont need the
$dbhost = "localhost"; // host name, usually localhost
$dbuser = "triggerh_maps"; // username used to login
$dbpwd = "maps"; // password used to login
$dbname = "triggerh_THCSTATS"; // the name of your database

then?
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

You don't need if you don't plan on using those variables to connect to the dbase? Those variables do nothing if not used somewhere. You used them nowhere.
weasel
Forum Commoner
Posts: 27
Joined: Sat Aug 16, 2003 3:27 pm

Post by weasel »

alright, thx a lot for the help and fast replys
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

.... did it work? ...
weasel
Forum Commoner
Posts: 27
Joined: Sat Aug 16, 2003 3:27 pm

Post by weasel »

yep, thx a lot!
Post Reply