Equivalent of mysql_insert_id() for ms sql server [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
SQL: How to get the id of values I just INSERTed?
For PHP, What's equivalent of mysql_insert_id() for sql server 2012?
sqlsrv_insert_id() does not seem to exist.
Say this is the code
if ($result) {
// get user details
$uid = mysql_insert_id(); // last inserted id
$result = mysql_query("SELECT * FROM users WHERE uid = $uid");
// return user details
return mysql_fetch_array($result);
} else {
return false;
}
for mysql,
how should I do it for sql server?

Check this out: http://fr2.php.net/manual/en/book.mssql.php
Over there at the comments you have the php syntax at the comments:
function mssql_insert_id() {
$id = 0;
$res = mssql_query("SELECT ##identity AS id");
if ($row = mssql_fetch_array($res, MSSQL_ASSOC)) {
$id = $row["id"];
}
return $id;
}

Or query it:
SELECT `column_id` FROM `table` ORDER BY `column_id` DESC TOP 1
TOP is the MySql equivalent of LIMIT

Related

Die if Data Not Exisits in php mysql table [duplicate]

This question already has answers here:
PHP mysql_num_rows die error
(3 answers)
Closed 5 years ago.
I'm getting an error message when the data is not available in data base table. I would like to stop the qry search without getting an error when the data is not available.
It works good if the data is available in table but not works while the data is not available. For an example, when i search this city name in my table the requested city's data's are not available in that table. During this time i'm getting an "Undefined variable" error.
<?php
$city = 'chennai';
$cityQry ="SELECT * FROM area_data WHERE city ='$city' LIMIT 1";
$cityQryResult = mysql_query($cityQry);
while($row = mysql_fetch_array($cityQryResult)) {
$citypop = $row['population'];
}
?>
Check if mysql_num_rows gives you count more than 0, If count is 0 call die()
$city = 'chennai';
$cityQry ="SELECT * FROM area_data WHERE city ='$city' LIMIT 1";
$cityQryResult = mysql_query($cityQry);
if(mysql_num_rows($cityQryResult) == 0)
{
die("No Data Exists");
} else {
while($row = mysql_fetch_array($cityQryResult)) {
$citypop = $row['population'];
}
}
Please try Below Code (Not Tested Yet) :
<?php
$city = 'chennai';
$cityQry ="SELECT * FROM area_data WHERE city ='$city'";
$result = mysqli_query($cityQry);
// Mysql_num_row is counting table row
$count = mysqli_num_rows($result);
if($count > 0)
{
$cityQryResult = mysql_query($cityQry);
while($row = mysql_fetch_array($cityQryResult)) {
$citypop = $row['population'];
}
} else {
echo "No Data Exists";
}
?>

resource id # 53 error in mysql [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
How do i “echo” a “Resource id #6” from a MySql response in PHP?
Hey guys, I got an error when I try to run my code in PHP.
It displays resource id #53 in my screen. All I want is to count only the total of one of my field but I'm stuck with this error. Here's my code below:
$last_points = mysql_insert_id();
//echo $last_points , display like 12... no error
$fkid = $last_points; // no error....
$sql = "SELECT COUNT(*) FROM downline WHERE fkmember = {$fkid}";
$execute = mysql_query($sql) or die (mysql_error());
echo $execute; //display error why?
Help me guys please. I think it's my query.
First off, resource id #53 is not an error. You are displaying a resource, not the output of the query.
To show the output, use:
$last_points = mysql_insert_id();
//echo $last_points , display like 12... no error
$fkid = $last_points; // no error....
$sql = "SELECT COUNT(*) FROM downline WHERE fkmember = {$fkid}";
$execute = mysql_query($sql) or die (mysql_error());
print_r(mysql_fetch_array($execute)); //display error why?
Secondly, the mysql_* functions are deprecated. You should look into learning and utilising the mysqli or PDO libraries accordingly.
Instead of trying to echo a resultset(as received because of mysql_query) do this:
print_r( mysql_fetch_array($execute) );
$execute is an array so you need to print it among echoing it
print_r($execute);
By codeigniter way
In Model:
function getCount($fkid)
{
$Qry = "SELECT * FROM downline WHERE fkmember = $fkid};
$query = $this->db->query($Qry);
return $query->num_rows();
}
In controller:
echo $Count = $this->modelname->getCount($id);

Why the mysql query is not evaluated properly? [duplicate]

This question already has answers here:
why this mysql query is not working?
(7 answers)
Closed 8 years ago.
Please help me regarding the specified problem:
The code section:
$result = mysql_query("SELECT *, UNIX_TIMESTAMP(eventdate) AS eventdate,
UNIX_TIMESTAMP(throughdate) AS throughdate FROM events where
id='$_GET[id]' ORDER BY eventdate");
// the above query is not working
if (! $result) {
echo mysql_errno() . ": " . mysql_error(). "\n";
}
if ( mysql_num_rows($result) == 0 ) {
print "<p>No events right now.</p>\n";
}
else {
$lasteventmonth = '';
while ($row = mysql_fetch_array($result)) {
$eventmonth="";
$eventmonth = date("F Y",$row['eventdate']);
if ($lasteventmonth != $eventmonth) {
print "<p style='font-size: 18px;'><b>$eventmonth</b></p>";
}
$lasteventmonth = $eventmonth;
showEvent($row);
}
}
?>
........................
........................//other codes
when the code evaluates as follows:
No events right now.
But specific id is present in the database and if $_GET['id'] is echoed in the page the value is shown.
what is id in id='$_GET[id]' at the beginning?
If you have a query http:// ... ?id=123, I would put id in quotes. Having said that, better like this:
$id = mysql_real_escape_string($_GET['id']); // safe against SQL injection
$sql = "SELECT *, UNIX_TIMESTAMP(eventdate) AS eventdate, UNIX_TIMESTAMP(throughdate) AS throughdate FROM events where id='$id' ORDER BY eventdate";
$result = mysql_query($sql);
If you are still getting trouble, use echo to check the variables $id and $result before the query runs; then you will have a clearer idea why it is not running the query you expect.
I am sure id=$_GET[id] is checking an int versus an int where you have it checking an int vs a string. Remove the single quotes around $_GET['id'] and try again. The single quotes define it as a string rather than an int.

How to check if value already exists in MySQL database [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I update if exists, insert if not (aka upsert or merge) in MySQL?
I know this is pretty basic.. but for some reason this is not working for me. I have a form that stores a Facebook user's ID to check if they already submitted the form. I have the form that submits the User ID into the database working perfectly. Its just this part of checking if the User ID value exists in the database that is tripping me up.
Here's my code....
$user_id = 1234567890;
$checkUserID = mysql_query("SELECT fbUserID from submissions WHERE fbUserID = '$user_id'");
if ($checkUserID) {
echo "GTFO BRO";
}
Whenever I do an "echo" on the $checkUserID variable I get this returned.. "Resource id #9"
mysql_query returns a resource containing the result of the query, you need to use something like this:
$user_id = 1234567890;
$checkUserID = mysql_query("SELECT fbUserID from submissions WHERE fbUserID = '$user_id'");
if (!$checkUserID) {
die('Query failed to execute for some reason');
}
if (mysql_num_rows($checkUserId) > 0) {
echo "User id exists already.";
$user = mysql_fetch_array($checkUserId);
print_r($user); // the data returned from the query
}
I think you query string is wrong. If you're using double quotes, you'd have to change it to
.... WHERE fbUserId = '{$user_id}'"
or you have to concatenate it
..... WHERE fbUserId = '" . $user_id . "'"
try the following piece of code:
$checkUserID = mysql_query("SELECT fbUserID from submissions WHERE fbUserID = '$user_id'");
while($test = mysql_fetch_array($checkUserID))
if ($test ) {
echo "GTFO BRO";
}
i hope this will work properly for you..

Cleaner PHP SQL query to value of 0 when no Results exists

I have written some PHP code that will return a value if there are results returned and return 0 if no results are returned. However, the function is very cumbersome and difficult to parse. I was wondering if there was a cleaner way to rewrite my code. Thanks in advance!
$last_question_sql="SELECT DISTINCT QUESTION_ID
FROM branching_survey_responses
WHERE QUESTION_ID=(SELECT Max(QUESTION_ID)
branching_survey_responses
WHERE CUSTOMER_ID=232
AND SET_ID=2)
AND CUSTOMER_ID=232
AND SET_ID=3";
$last_question_result=mysql_query($last_question_sql);
if($last_question_status=mysql_fetch_assoc($last_question_result)){
$last_question=$last_question_status['QUESTION_ID'];
}
else{
$last_question= 0;
}
EDIT:
i'm not sure, but if you are using this code for get back the last inserted item you can just use mysql_insert_id() example:
after the INSERT query has successfully executed you can retrieve the last id like
$last_question = mysql_insert_id();
function last_question($cid, $sid1, $sid2)
{
$question = 0;
$result = mysql_query(
"SELECT DISTINCT QUESTION_ID
FROM branching_survey_responses
WHERE QUESTION_ID=(SELECT Max(QUESTION_ID) branching_survey_responses
WHERE CUSTOMER_ID={$cid}
AND SET_ID={$sid1})
AND CUSTOMER_ID={$cid}
AND SET_ID={$sid2}");
if ($status = mysql_fetch_assoc($result)) {
$question = $status['QUESTION_ID'];
}
return $question;
}

Categories