Jump to content

How to check with PHP if a table is empty?


xhelle

Recommended Posts

Hi everyone...

 

Can somebody help me to solve my problem. I have this data needed to be insert into database, but first I must check if the mobile number is already exist on may table, if not it will continue to insert. What is the correct query for this problem...Hope somebody help me...

 

Here's my old query :

insert into tgt_table select * from src_table where mobilenum NOT IN (select distinct mobilenum from tgt_table)

 

Thanks in advance.

Link to comment
Share on other sites

Hi Mchl,

 

Thanks for the reply but I dont think the insert...on duplicate key update will do...Here's my code: 

$sqlmobile = "SELECT mobile_num FROM user";
$query = mysql_query($sqlmobile);
$data = mysql_fetch_array($query);
if ($data["mobile_num"] == $temp[1])
{
echo "Mobile Number already exist!";
}

 

But its still insert into database during i run my catcher...the example i use is already exist on user table, therefore it should not insert...or it should send an error message...Hope you can help me to solve my problem...

 

Thanks again....

Link to comment
Share on other sites

You can do this with INSERT ON DUPLICATE KEY UPDATE.

If you want mobile_num to be unique in the table, then you should create a UNIQUE index on this column.

 

Then you run a query like this.

INSERT INTO table (mobile_num) 
VALUES ('$mobile_num') 
ON DUPLICATE KEY UPDATE
SET mobile_num = mobile_num

 

 

Link to comment
Share on other sites

Hi again Mchl,

 

Thanks for your help. I already solve my problem...^^all i need to do is add the WHERE mobile_num = '".$temp[1].'"

//select mobile number in database....
$sqlmobile = "SELECT mobile_num FROM user WHERE mobile_num = '".$temp[1]."'";
$query = mysql_query($sqlmobile);
$data = mysql_fetch_array($query);
if($data["mobile_num"] == $temp[1])
{
echo "Mobile Number already exist!";
}

 

Thanks again^^

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.