0

This is one thing I can't understand. I'm using a function which fetches records from the database. And if that record doesn't exist. It performs an insert query. I even echoed out to ensure that those 2 parameters contains something. And it really contain something. What I can't understand is that it doesn't perform the insert query. I also tested the insert query below on another script which doesn't use function. And it worked.

function fetch_customer($cust, $credit){

$getcnum=query_database("SELECT Cust_Name, CUSID FROM customer_credit WHERE Cust_Name='$cust'","onstor",$link);

if(mysql_num_rows($getcnum)==0){
    $fullname=explode(",", $cust);
    $lname= $fullname[0];
    $fname= $fullname[1];


    query_database("INSERT INTO customer_credit(Cust_Name, CREDIT) VALUES('$cust','$credit')",'onstor' ,$link);

    echo "customer: ".$cust."<br/>";

    echo "credit: ".$credit;
    query_database("INSERT INTO customer_table(CLNAME, CFNAME) VALUES('$lname', '$fname')",'onstor',$link);

}

Then I would call the function later on.

fetch_customer($customer, $custcred);

Where do I start if I want to debug this one?

4
  • Where does your $link come from ? if it's defined out of the function it may be the problem :) Commented Mar 4, 2011 at 15:53
  • What does 'query_database' do? Commented Mar 4, 2011 at 15:53
  • whats this query_database ? ,if you want to query or insert it should be mysql_query Commented Mar 4, 2011 at 15:53
  • the code for your query_database function may help Commented Mar 4, 2011 at 15:53

5 Answers 5

2

I'm thinking that your insert queries aren't working because, to wit, there is no query_database function defined (unless it's one that you've defined yourself).

The function that (I believe) you are looking for is mysql_query.

Here's a link to the docs: mysql_query

Sign up to request clarification or add additional context in comments.

1 Comment

seems like the problem is with using functions. I have an include statement which contains the function query_database. And I think the function cannot see the include. That's why it doesn't work. But thanks
1

Are you sure that it goes into the if statement? mysql_num_rows($getcnum)==0 echo mysql_num_rows($getcnum) and see how many rows contains, I have also notice the single quotes into 'onstor' , could that be a problem in your function?

Comments

1

It's hard to answer without seeing the query_database() function. I'm not sure why you're doing that instead of just using mysql_query("INSERT INTO...") or whatever. It's possible that your query_database() function isn't returning the proper query variable.

The only thing I can tell you is that in the code above, you're missing a closing bracket. The if statement is closed, but the function isn't. That might be the problem.

Good luck.

Comments

0

Here is php code to query mysql

function fetch_customer($cust, $credit){


    $link = mysql_connect( 'localhost' , 'username' , 'password' );

    mysql_select_db( 'onstor' , $link ); 

    $getcnum=mysql_query("SELECT Cust_Name, CUSID FROM customer_credit WHERE Cust_Name='$cust'");

        if(mysql_num_rows($getcnum)==0){
            $fullname=explode(",", $cust);
            $lname= $fullname[0];
            $fname= $fullname[1];


            mysq_query("INSERT INTO customer_credit(Cust_Name, CREDIT) VALUES('$cust','$credit')");

            echo "customer: ".$cust."<br/>";

            echo "credit: ".$credit;
            mysql_query("INSERT INTO customer_table(CLNAME, CFNAME) VALUES('$lname', '$fname')");

        }

1 Comment

$link = mysql_connect( 'localhost' , 'username' , 'password' ); here you have the link. Previously, you didn't have any link. You were passing undefined var as second parameter
0

You are passing the $link to the database in your query_database($sql, $db, $link), but it is not defined anywhere. So it must be a global variable, that you forget to declare. So, declare $link as being global:

function fetch_customer($cust, $credit)
{
     global $link;
     // rest of your function
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.