pheromix
pheromix

Reputation: 19287

How to convert a String to an Integer in PHP?

I want to insert/update data into a MySQL table by using a csv-like data :

function execInsertOrUpdateCompteRendu($compterendu)  // here $compterendu is something like this : -1;2012/03/06 09h 50mn;3;1; 
{
        $resultat = explode("\n",$compterendu);
        $connec = mysql_connect("192.168.1.123:3306","root", "mysqlroot");
        mysql_set_charset('utf8', $connec);
        mysql_select_db("finance",$connec);
        for($i = 0; $i < count($resultat) ; $i++)
        {
            $valeur=explode(";",$resultat[$i]);
            if ($valeur[0] > 0)
            {
                $requete = mysql_query("UPDATE compte_rendu SET  
                cr_date    = '".convertFromDateHM($valeur[1])."',
                cr_lieu    = '".$valeur[2]."',
                cred_id    = '".$valeur[3]."',
                cr_resultat = '".$valeur[5]."',
                cr_comment = '".$valeur[6]."',
                adc_id = '".$valeur[7]."' 
                WHERE cr_id = '".$valeur[0]."'");
            }
            else
            {
                $requete = mysql_query("INSERT INTO compte_rendu SET 
                                        cred_id = '".$valeur[3]."',
                                        cr_date = '".convertFromDateHM($valeur[1])."',
                                        cr_lieu = '".$valeur[2]."',
                                        cr_resultat = '".$valeur[5]."',
                                        cr_comment = '".$valeur[6]."',
                                        adc_id = '".$valeur[7]."'");
            }
        }
        return $resultat[0];
}

This PHP code is a web-service which I call from a J2ME client. The problem is that new data are not inserted in the table when I run the J2ME application ! I debugged the java program and there are no errors there. So what is wrong in my PHP code ?

Upvotes: 0

Views: 138

Answers (2)

Kurt
Kurt

Reputation: 7212

Use MySQLi prepared statements. You can then bind parameters as integers, doubles, strings or blobs. Have a read of the Mysqli Manual and in regards to binding the bind_param section.

In regards to your queries: You shouldn't have to wrap the integer values in quotes

Upvotes: 1

solarise
solarise

Reputation: 453

First, you could try escaping all the text you're entering into your database with mysql_real_escape_string - otherwise you're leaving potential for issues to arise. e.g.

//cr_lieu    = '".mysql_real_escape_string($valeur[2])."',

Also - to convert a string to integer?

$int = (int)$string;

Upvotes: 0

Related Questions