Dingbats
; Send PM
Posts: 1970
Threads: 50 Mood: Optative Money: £268.57 (D) (+ Friend)
|
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] |
Zogger!
Looking For Status Send PM Posts: 3954
Threads: 62 Money: £93.82 (D) (+ Friend)
|
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] |
ReadMe
Absent Send PM Posts: 2820
Threads: 85 Money: £43.42 (D) (+ Friend)
|
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] |
Zogger!
Looking For Status Send PM Posts: 3954
Threads: 62 Money: £93.82 (D) (+ Friend)
|
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
Posts: 1970
Threads: 50 Mood: Optative Money: £268.57 (D) (+ Friend)
|
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] |
tundraH
Statusless Send PM Posts: 566
Threads: 20 Money: £1.26 (D) (+ Friend)
|
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
Posts: 1970
Threads: 50 Mood: Optative Money: £268.57 (D) (+ Friend)
|
No, didn't work... 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] |
ReadMe
Absent Send PM Posts: 2820
Threads: 85 Money: £43.42 (D) (+ Friend)
|
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']} | n";
} while ($row = mysql_fetch_assoc($result));
} else {
echo "None found | ";
}
\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. |
04.12.03 20:49 Post #8 | [Hide Sig (7)] [Profile] [Quote] |
Dingbats
; Send PM
Posts: 1970
Threads: 50 Mood: Optative Money: £268.57 (D) (+ Friend)
|
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] |
ReadMe
Absent Send PM Posts: 2820
Threads: 85 Money: £43.42 (D) (+ Friend)
|
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
Posts: 1970
Threads: 50 Mood: Optative Money: £268.57 (D) (+ Friend)
|
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] |
Your Comments: