PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
StephenL
Forum Newbie
Posts: 3 Joined: Sun Jun 27, 2004 12:05 pm
Post
by StephenL » Sun Jun 27, 2004 12:05 pm
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);
?>
Joe
Forum Regular
Posts: 939 Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow
Post
by Joe » Sun Jun 27, 2004 12:10 pm
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);
?>
StephenL
Forum Newbie
Posts: 3 Joined: Sun Jun 27, 2004 12:05 pm
Post
by StephenL » Sun Jun 27, 2004 12:18 pm
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);
?>
markl999
DevNet Resident
Posts: 1972 Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)
Post
by markl999 » Sun Jun 27, 2004 12:25 pm
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());
kettle_drum
DevNet Resident
Posts: 1150 Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England
Post
by kettle_drum » Sun Jun 27, 2004 12:29 pm
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?
Joe
Forum Regular
Posts: 939 Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow
Post
by Joe » Sun Jun 27, 2004 1:05 pm
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!
StephenL
Forum Newbie
Posts: 3 Joined: Sun Jun 27, 2004 12:05 pm
Post
by StephenL » Sun Jun 27, 2004 8:38 pm
Got it Working