
October 31st, 2003
02:10 PM
checking if something is already in mysql db
im sorry for the stupid title of this thread but...
how would i be do
if (sumthing in mysql table already exists) { showerr(" Data Already Exists");
sorry for the bad language, im a *little* hungover
___________________
everything we do in life echo's in eternity
Last edited by Tooli0, October 31st, 2003 08:14 PM (Edited 1 times)

October 31st, 2003
06:23 PM
any 1 ? :
:
___________________
everything we do in life echo's in eternity

October 31st, 2003
06:28 PM
overall Funny Guy
Status: Offline!
<?php
$id = '41';
$result = mysql_query('select * from tabel where id = ' . $id);
if($result)
echo 'already existst!';
?>
not tested, but should work.
what it does is that it select a row from the database and if something is returned it exists!
___________________
"I find it kinda funny, I find it kinda sad,
The dreams in wich I'm dying are the best i've ever had"

October 31st, 2003
07:06 PM
not workin :S
<?php
if (isset($_POST['createbox'])) { $createbox = $_POST['createbox']; }
$result = $test_sql->query($db,'select * from $db[prefix]_departments WHERE dep_name = ' . $createbox, 0);
if ($result) { echo "already assigned"; } else { echo "not assigned"; }
?>
could u please correct tha for us :S probably totally wrong :S
___________________
everything we do in life echo's in eternity

October 31st, 2003
08:19 PM
instead of if $result, do:
<?php
while($row = mysql_fetch_array($result)) {
$count = $count + 1
}
if($count > 0) {
//already assigned
} else {
//not assigned
}
?>
___________________
//damell

October 31st, 2003
08:31 PM
MySQL Returned this error: Unknown column 'test' in 'where clause' Error number: 1054
when i use that
this is the fulll code
<?if (isset($_GET['id']) && $_GET['id'] == "c") {
if (isset($_POST['createbox'])) { $createbox = $_POST['createbox']; }
$result = $toolios_sql->query($db,'select * from ' . "$db[prefix]_departments" . ' WHERE dep_name = ' . $createbox, 0);
while($row = mysql_fetch_array($result)) {
$count = $count + 1;
}
if($count > 0) {
echo "already assigned";
} else {
echo "not assigned";
}
}
?>
___________________
everything we do in life echo's in eternity

October 31st, 2003
09:45 PM
28, Male & single, what else do you need to know?
Status: Offline!
First thing, you don't need to actually retrieve the rows to count them. Just use mysql_num_rows($result), and if it is > 0 then that means your select found the data you where asking for.
Now, for the query:
<?php
$result = $toolios_sql->query($db,'select * from ' . $db['prefix'].'_departments WHERE dep_name = "' . $createbox.'"', 0);
?>
This should work. Just keep in mind you have to quote (') or double-quote (") your strings when comparing in your SQL queries. I guess $createbox = "test", right?
___________________
-~-~-~oO··Oo~-~-~-~-
I wish I was a messenger and all the news was good
I wish I was the full moon shining off your camaros hood
...