dmccabe Posted June 24, 2008 Share Posted June 24, 2008 Very simple one this. I want to select just one record from my mysql db and assign variables to the values of the columns. This is what I have: if (isset($_GET['edit'])) { $branch_id = $_GET['edit']; echo "branch id = $branch_id <br />"; $selectbranch = "SELECT * FROM `tbl_branches` WHERE `id` LIKE '$branch_id'"; if (!mysql_query($selectbranch)) { die('Error: ' . mysql_error()); } else { $result = mysql_query($selectbranch); $row = mysql_fetch_row($result); $id = $row['id']; echo "id = $id <br />"; $region_id = $row['region_id']; $manager_id = $row['manager_id']; $name = $row['name']; $code = $row['code']; } } am bamboozled! Link to comment https://forums.phpfreaks.com/topic/111685-solved-selecting-just-the-one-record-from-a-mysql-db/ Share on other sites More sharing options...
fanfavorite Posted June 24, 2008 Share Posted June 24, 2008 if (isset($_GET['edit'])) { $branch_id = $_GET['edit']; echo "branch id = $branch_id <br />"; $selectbranch = mysql_query("SELECT * FROM `tbl_branches` WHERE `id` LIKE '$branch_id'"); if (!$selectbranch)) { die('Error: ' . mysql_error()); } else { $row = mysql_fetch_row($selectbranch); $id = $row['id']; echo "id = $id <br />"; $region_id = $row['region_id']; $manager_id = $row['manager_id']; $name = $row['name']; $code = $row['code']; } } If you want to update, you use mysql_query("UPDATE tbl_branches SET region_id = '$regionid', manager_id = '$managerid', name = '$name', code= '$code' WHERE `id` LIKE '$branch_id'"); Now I am not sure whether you know how to use "LIKE" properly. If you want an exact match, us id = '$branch_id'. You use like if you are looking for a part of a string within the id field. Link to comment https://forums.phpfreaks.com/topic/111685-solved-selecting-just-the-one-record-from-a-mysql-db/#findComment-573274 Share on other sites More sharing options...
dmccabe Posted June 24, 2008 Author Share Posted June 24, 2008 Ok I tried as you suggested, but am still not getting any results: if (isset($_GET['edit'])) { $branch_id = $_GET['edit']; echo "branch id = $branch_id <br />"; $selectbranch = mysql_query("SELECT * FROM `tbl_branches` WHERE `id` = '$branch_id'"); if (!$selectbranch) { die('Error: ' . mysql_error()); } else { $row = mysql_fetch_row($selectbranch); $id = $row['id']; echo "id = $id <br />"; $region_id = $row['region_id']; The line that says echo "id = $id <br />"; still comes up blank as in "id = " Link to comment https://forums.phpfreaks.com/topic/111685-solved-selecting-just-the-one-record-from-a-mysql-db/#findComment-573288 Share on other sites More sharing options...
nashruddin Posted June 24, 2008 Share Posted June 24, 2008 mysql_fetch_row() returns enumerated array. you access the values with $row[0], $row[1], ...,$row[n]. if you want to access it with $row['id'], $row['region_id'], use mysql_fetch_assoc() instead. Link to comment https://forums.phpfreaks.com/topic/111685-solved-selecting-just-the-one-record-from-a-mysql-db/#findComment-573323 Share on other sites More sharing options...
dmccabe Posted June 25, 2008 Author Share Posted June 25, 2008 ignore Link to comment https://forums.phpfreaks.com/topic/111685-solved-selecting-just-the-one-record-from-a-mysql-db/#findComment-573921 Share on other sites More sharing options...
dmccabe Posted June 25, 2008 Author Share Posted June 25, 2008 mysql_fetch_row() returns enumerated array. you access the values with $row[0], $row[1], ...,$row[n]. if you want to access it with $row['id'], $row['region_id'], use mysql_fetch_assoc() instead. A winner is you! Thanks mate! Link to comment https://forums.phpfreaks.com/topic/111685-solved-selecting-just-the-one-record-from-a-mysql-db/#findComment-573926 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.