BlameThePixel!

BTForum » BlameTheOffTopic Forums » BlameTheGeneralOffTopic » PHP/MySQL problem

n"; } while ($row = mysql_fetch_assoc($result)); } else { echo ""; } \n

Thats just the mysql bits tho, you'll need to fit ur other bits in around that.
________________
Cant be arsed to remake my sig.
[]Dingbats
;
Send PM
An Avatar
Posts: 1970
Threads: 50
Mood: Optative
Money: £268.57 (D)
(+ Friend)
Not online within the last half an hour
I'm trying to learn how to use MySQL in PHP, but of course i have some problems. I've typed this in a file called mysql.php (minus the spaces) :

< html >
< body bgcolor="f6fff6" >
< font face="Verdana" style="font-size: 12px; font-color: #11ff11" >
< ?php
$con = mysql_connect("localhost", "root", "" ) ;
$db = mysql_select_db("test" ) ;

if(isset($_POST['name']) && isset($_POST['favcolor'] ) )
{
if(isset($_POST['name'])) $name_ = $_POST['name'];
if(isset($_POST['favcolor'])) $favcolor_ = $_POST['favcolor'];

$sql = "SELECT * FROM test WHERE name = ".$_POST['name'];
$result = mysql_query("$sql" ) ;

if(!($row = mysql_fetch_array($result))) {
$sql = "INSERT INTO test (name, favcolor) VALUES ('".$name_."', '".$favcolor_."' ) ";
$result = mysql_query("$sql" ) ;
}
}

echo "< table bgcolor='eeffee' width='400'

border='1' >< tr >< td >< b >Name< /b >< /td >< td >< b >Favorite color< /b >< /td >< /tr >";

$sql = "SELECT * FROM test ORDER BY name";
$result = mysql_query("$sql" ) ;

while($row = mysql_fetch_array($result)) {
echo "< tr >< td >".$row['name']."< /td >< td >".$row['favcolor']."< /td >< /tr >";
}

mysql_close($con ) ;
?>
< /table >
< br >< br >< br >< br >
< form method="POST" action="mysql.php" >
Name:             

< input type="text" name="name" >< br >
Favorite color:  < input type="text" name="favcolor" >< br >
< input type="submit" value="Submit" >
< /form >
< /font >
< /body >
< /html >

I'm trying to make a page where you can enter your name and favorite color in two boxes, and all names and colors typed in will appear in a table. But I want all names to be unique. But when I run the file, there is an error: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in c:\program\php\easyphp\www\mysql.php on line 16. The database is called test, and the only table I use here is also called test. What is wrong, can anyone help?
03.12.03 18:45
Post #1
Last edited: 03.12.03 18:49 (Dingbats - 1 times) [Softbrain Games] [Hide Sig (2)] [Profile] [Quote]
[S]Zogger!
Looking For Status
Send PM
Posts: 3954
Threads: 62
Money: £93.82 (D)
(+ Friend)
Not online within the last half an hour
you don't have to put the variables outside of quotes. Most of the time it's a lot less confusing if you leave them in...

anyway, change

\n
$sql = "SELECT * FROM test WHERE name = ".$_POST['name'];
\n

to

\n

$sql = "SELECT * FROM test WHERE name = "$_POST['name']"";
\n

I think that should work, not sure you can use backslashed "s in mysql but I don't see why you wouldn't be able to.
The variable has to be in quotes in the mysql, cause it's a string.

________________
You know I'm a dancing machine
03.12.03 19:59
Post #2
Last edited: 03.12.03 20:00 (ZoGgEr! - 2 times) [Hide Sig (8)] [Profile] [Quote]
[S]ReadMe
Absent
Send PM
Posts: 2820
Threads: 85
Money: £43.42 (D)
(+ Friend)
Not online within the last half an hour
basically u need quotes around the name field for your query. You can tell zog doesn't use superglobals too much, cos the code he gave will give a T_variable error, you'll need the following:

\n
$sql = "SELECT * FROM test WHERE name = '{$_POST['name']}'";
\n

________________
Cant be arsed to remake my sig.
03.12.03 21:36
Post #3
[Hide Sig (7)] [Profile] [Quote]
[S]Zogger!
Looking For Status
Send PM
Posts: 3954
Threads: 62
Money: £93.82 (D)
(+ Friend)
Not online within the last half an hour
yeh your right, I rarely use them, which was one of my uncertainties.

________________
You know I'm a dancing machine
04.12.03 08:37
Post #4
[Hide Sig (8)] [Profile] [Quote]
[]Dingbats
;
Send PM
An Avatar
Posts: 1970
Threads: 50
Mood: Optative
Money: £268.57 (D)
(+ Friend)
Not online within the last half an hour
Nope, doesn't work. It still says there is an error at line 16, the first mysql_fetch_array().

Edit: What tecnique do you use for BTP, ZoGgEr?
04.12.03 17:10
Post #5
Last edited: 04.12.03 17:11 (Dingbats - 1 times) [Softbrain Games] [Hide Sig (2)] [Profile] [Quote]
[UA]tundraH
Statusless
Send PM
Posts: 566
Threads: 20
Money: £1.26 (D)
(+ Friend)
Not online within the last half an hour
put echo mysql_error(); underneath the query line. If this prints nothing, the query is ok. If the error still occurs, try changing this:
\n
$sql = "SELECT * FROM test WHERE name = ".$_POST['name']; $result = mysql_query("$sql" ) ; if(!($row = mysql_fetch_array($result))) { $sql = "INSERT INTO test (name, favcolor) VALUES ('".$name_."', '".$favcolor_."' ) "; $result = mysql_query("$sql" ) ; }
\n
to this:
\n
$sql = "SELECT * FROM test WHERE name = ".$_POST['name']; $result = mysql_query($sql); if(mysql_num_rows($result) == 0) {
$sql = "INSERT INTO test (name, favcolor) VALUES ('".$name_."', '".$favcolor_."' ) "; mysql_query($sql) ; }
\n
Sorry if I got it wrong.
04.12.03 18:13
Post #6
[tundraH.com] [Hide Sig (0)] [Profile] [Quote]
[]Dingbats
;
Send PM
An Avatar
Posts: 1970
Threads: 50
Mood: Optative
Money: £268.57 (D)
(+ Friend)
Not online within the last half an hour
No, didn't work... :x It says there is an error with mysql_num_rows() instead. *sigh*
04.12.03 18:33
Post #7
[Softbrain Games] [Hide Sig (2)] [Profile] [Quote]
[S]ReadMe
Absent
Send PM
Posts: 2820
Threads: 85
Money: £43.42 (D)
(+ Friend)
Not online within the last half an hour
two approaches, try the query int phpmyadmin to see what it returns, or use my way of looping through the results from a select statement and for the first query since you're only checking for the existance of a row, the mysql count() function will do nicely:
\n
$sql = "SELECT count(*) FROM test WHERE name = '{.$_POST['name']}'";; $result = mysql_result(mysql_query($sql),0) or die('Error: '.mysql_error(); if($result <= 1) { $sql = "INSERT INTO test (name, favcolor) VALUES ('{$name_}', '{$favcolor_}' ) ";
mysql_query($sql) or die('Error: '.mysql_error()); } $sql = "SELECT * FROM test ORDER BY name"; $result = mysql_query($sql) ; if($row = mysql_fetch_assoc($result)) { do { echo "
{$row['name']}{$row['favcolor']}
None found
04.12.03 20:49
Post #8
[Hide Sig (7)] [Profile] [Quote]
[]Dingbats
;
Send PM
An Avatar
Posts: 1970
Threads: 50
Mood: Optative
Money: £268.57 (D)
(+ Friend)
Not online within the last half an hour
Err... Sorry, but that's too advanced for me, I don't understand. Isn't there an easier way? :?
05.12.03 15:47
Post #9
[Softbrain Games] [Hide Sig (2)] [Profile] [Quote]
[S]ReadMe
Absent
Send PM
Posts: 2820
Threads: 85
Money: £43.42 (D)
(+ Friend)
Not online within the last half an hour
erm, whats advanced about that??/

________________
Cant be arsed to remake my sig.
06.12.03 20:33
Post #10
[Hide Sig (7)] [Profile] [Quote]
[]Dingbats
;
Send PM
An Avatar
Posts: 1970
Threads: 50
Mood: Optative
Money: £268.57 (D)
(+ Friend)
Not online within the last half an hour
Didn't I say that I'm a n00b? Anyway, I've got an answer in a PHP forum which works.
07.12.03 18:18
Post #11
[Softbrain Games] [Hide Sig (2)] [Profile] [Quote]

Post Reply

Jump To:


Your Comments:

Donate to BlameThePixel:
Donate to BTP Via PayPal


[22 Queries, Page Loaded in 0.277854 Seconds]

ShoutMeUp

Xmas Greetings from waka waka waka waka []Unvalidated EmailChristmasRiddle MERRY CHRISTMAS EVERYONE! []Spleet Except for Spleet. []TheAbdBoy Always bummin' a brother out. []Spleet Happy New Year everyone! But Spleet. []TheAbdBoy

Word Association

All

-10 Ago-

MiddleEastern []AlphaWolf camel [S]Bloopy toe []TheAbdBoy moose knuckle [S]Bloopy MeatLoaf []Spleet IdDoAnything4Lo ve []AlphaWolf rub n tug []TheAbdBoy tugboat []The Pope rope [S]Bloopy race []TheAbdBoy

-Latest-


Must be logged in to add new words

FictoLeague

You have to be logged in to vote...

Member Stats

Date: 15.05.24.
Members: 4731.
Latest: []Unvalidated Emailsdsakldsaldklasdsdsa
Active:
0 user(s)
1 guest(s)

On chat:
Lots of people

Files: 3330

Bloopy's Site
Get Firefox Get Opera Donate to BTP Via PayPal