I have a table t_s_list
having s_id
as primary key. Now I want to retrieve primary value if the corresponding column is already inserted. If the corresponding column is instantly I can easily get the values using mysql_insert_id
function. But what should if value is already exist.
NOTE: If I want to get the primary values of already exist column I am getting value as Resource id #5
as result.
My code is..
if($txtbx1!="") {
$result=mysql_query("select title from t_s_list where title='$txtbx1'");
if(mysql_num_rows($result)!=0){
$resultid=mysql_query("select s_id from t_s_list where title='$txtbx1'");
print_r($resultid); }
else
{
$insert=mysql_quer开发者_如何学JAVAy("Insert into t_s_list(title) values ('$txtbx1') ");
$resultid=mysql_insert_id();
print_r($resultid);
}
}
else {
$msg="Text box empty";
}
Emil is correct, http://php.net/manual/en/function.mysql-query.php mysql_query returns a Resource not a string or integer.
There are many ways to access the results table from the resource. mysql_fetch_assoc() will return an associative array where the column heads are used to read the given value. i.e. $resource = mysql_query(...); $results = mysql_fetch_assoc(); $id = $results["columnIdentifier"]; mysql_fetch_array() will return an array of values i.e. $resource = mysql_query(...); $results = mysql_fetch_array(); $id = $results[0]; assuming the id is the first element returned on the table
$result = mysql_query("select s_id from t_s_list where title='$txtbx1'");
$row = mysql_fetch_assoc($result);
$resultid = $row['sid'];
print_r($resultid);
精彩评论