php - set int from sql query -
i trying set int value sql query. in ios app can assign int value photo id, store , retrieve fine. problem comes if want overwrite photo new jpg still using existing idphoto , therefore same filename, e.g. 1.jpg. first check whether user exists. if update photo (this need set idphoto) otherwise create photo new id (works fine).
function uploaddetails($name, $location, $photodata, $idphoto) { $uploads = query("select name, idphoto users name = '%s' limit 1",$name); if (count($uploads['result'])>0) { $result = query("update users set name='$name', location='$location', idphoto='$idphoto' name = '%s'", $name); //need define idphoto users table if (move_uploaded_file($photodata['tmp_name'], "icons/".$idphoto.".jpg")) { thumb("icons/".$idphoto.".jpg", 180); //i can print out confirmation iphone app print json_encode(array('$idphoto'=>$idphoto)); } else { //print out error message iphone app errorjson('upload on server problem'); }; } else { if ($photodata['error']==0) { $result = query("insert users(name, location) values('%s','%s')", $name, $location); if (!$result['error']) { // fetch active connection database (it's initialized automatically in lib.php) global $link; // last automatically generated id in table $idphoto = mysqli_insert_id($link); if (move_uploaded_file($photodata['tmp_name'], "icons/".$idphoto.".jpg")) { thumb("icons/".$idphoto.".jpg", 180); print json_encode(array('successful'=>1)); } else { errorjson('upload on server problem'); }; } else { errorjson('upload database problem.'.$result['error']); } } } } so problem lies in first part of code need update photo still use same idphoto
edit
the piece of code needed follows:
$getid = mysqli_fetch_assoc(mysqli_query($link, "select idphoto users name = '$name'")); $idphoto = $getid['idphoto']; although got answer myself, appreciate feedback of how phrase questions better in future. , writing out full code helped me @ bigger picture , see going wrong.
your question extremely vague i'm going take following assumptions:
- your query ever return single record
- you using mysqli
once have run query , stored results $result can use following code idphoto:
//store row results variable $row = $result->fetch_assoc(); //store idphoto variable $idphoto = $row['idphoto']; note: selecting name database when have name since you're using fetch record
Comments
Post a Comment