Jump to content

[SOLVED] selecting just the one record from a mySQL db


dmccabe

Recommended Posts

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!

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. 

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 = "

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.