Page 1 of 1
PHP & MYsql Newbie
Posted: Sun Jun 27, 2004 12:05 pm
by StephenL
For some reason this insert code doesn't work
Code: Select all
<?php
$linkID = @mysql_connect("****", "****", "******");
mysql_select_db("*****", $linkID);
$result = mysql_query("INSERT INTO customers (firstname, lastname) VALUES ('bob', 'bob1')", $linkID);
if ($result != false)
{
echo 'Success!';
}
else
{
echo 'failure';
}
mysql_close($linkID);
?>
Posted: Sun Jun 27, 2004 12:10 pm
by Joe
Try:
Code: Select all
<?php
$linkID = @mysql_connect("****", "****", "******");
mysql_select_db("*****");
$result = mysql_query("INSERT INTO customers (firstname, lastname) VALUES ('bob', 'bob1')");
if (mysql_num_rows($result))
{
echo 'Success!';
}
else
{
echo 'failure';
}
mysql_close($linkID);
?>
Posted: Sun Jun 27, 2004 12:18 pm
by StephenL
I get an error
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in
Code: Select all
<?php
$linkID = @mysql_connect("*", "*", "*");
mysql_select_db("*");
$result = mysql_query("INSERT INTO customers (firstname, lastname) VALUES ('bob', 'bob1')");
if (mysql_num_rows($result)) \\\\\\\\This line
{
echo 'Success!';
}
else
{
echo 'failure';
}
mysql_close($linkID);
?>
Posted: Sun Jun 27, 2004 12:25 pm
by markl999
Use
mysql_error to help debugging.
Code: Select all
$linkID = @mysql_connect("*", "*", "*") or die(mysql_error());
mysql_select_db("*") or die(mysql_error());
$sql = "INSERT INTO customers (firstname, lastname) VALUES ('bob', 'bob1')";
$result = mysql_query($sql) or die(mysql_error());
Posted: Sun Jun 27, 2004 12:29 pm
by kettle_drum
Then your mysql is causing the error. Is there a table called 'customers' in your database and does it have two fields called 'firstname' and 'lastname'? Can both of these fields accept strings?
Posted: Sun Jun 27, 2004 1:05 pm
by Joe
Im not sure if this will make a difference but try:
Code: Select all
<?php
$linkID = @mysql_connect("****", "****", "******");
mysql_select_db("*****") or die(mysql_error());
$query = "INSERT INTO customers (firstname, lastname) VALUES ('bob', 'bob1')";
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result))
{
echo 'Success!';
}
if (!mysql_num_rows($result))
{
echo 'failure';
}
mysql_close($linkID);
?>
Also have a read at what kettle_drum suggested, that could be your problem!
thanks
Posted: Sun Jun 27, 2004 8:38 pm
by StephenL
Got it Working
