php help please
jamesfinney2005@hotmail.com
Member Posts: 27
**** please could any one see where am going wrong am trying to get it to only wright to the table if the table is empty.****
$sql = mysql_query("SELECT * FROM ".$tableName);
if (mysql_num_rows($sql) =="0"){
// construct SQL statement
$sql="INSERT INTO ".Order1."(item1, item2, item3, item4, item5)VALUES('$item1', '$item2', '$item3', '$item4', '$item5')";
// insert SQL statement
$result=mysql_query($sql);
}
else {
// if failure, write to custom log
$sqlError = "Error writing to database\n";
file_put_contents($sqlErrorLog, $sqlError, FILE_APPEND);
}
// catch any errors
if($result){
// if successful do nothing for now.
}
else {
// if failure, write to custom log
$sqlError = "Error writing to database\n";
file_put_contents($sqlErrorLog, $sqlError, FILE_APPEND);
Comments
Did you create a string linking to the MySQL code?
I think your problem is that you didn't define a string.
do you mean at the start of the script if so then
yes the rest of the script works fine if i cut this out, but for the app i need the rule so the table data only wrights when the table has been emptyed.
i was trying to use... mysql_num_rows... to tell if the table is empty or not . cant seem to get it to work it currently just stops the scrip even if the table is empty or full.
thank you for your help
hi sorry to be a pain i realy new to this so realy trying to pick it up with a lot of googleing lol could you give me an example of how it should be
thanks agen
from
$sql = mysql_query("SELECT * FROM ".$tableName);
to
$sql = "mysql_query"("SELECT * FROM ".$tableName);
Lol no one's a pain bro. We're all just helping each other out.
But hold on, let me go over it
Thanks
Can anyone help me on this ? Please
Bump
You may want to consider using an ORM like Propel (http://propelorm.org/) to avoid problems with sql.
Also, you may want to do a "select count(*)" instead of counting the result of a "select *" as less data will move back and forth.
Thank you for your help