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..
Related
This question already has answers here:
Single result from database using mysqli
(6 answers)
Closed 4 years ago.
I need to get only the id of member who's username is X from the mysql database.
Can this only be done with a while loop or is there any other way around this?
What I'm thinking of is something like:
$id = mysqli_query($con,'SELECT id FROM membrs WHERE username = '$username' LIMIT 1)
Thanks,
You can use:
mysqli_fetch_array();
// For Instance
$id_get = mysqli_query($con, "SELECT id FROM membrs WHERE username='$username' LIMIT 1");
$id = mysqli_fetch_array($id_get);
echo $id['id']; // This will echo the ID of that user
// What I use is the following for my site:
$user_get = mysqli_query($con, "SELECT * FROM members WHERE username='$username'");
$user = mysqli_fetch_array($user);
echo $user['username']; // This will echo the Username
echo $user['fname']; // This will echo their first name (I used this for a dashboard)
without while loop we can do it by the following code, if you are selecting more than 1 record you need to loop it
$row = mysqli_fetch_array($id);
echo $row['id'];
I am trying to get four different values from my database. The session variable username and usernameto are working, but I want to get 4 different values -- two each from username and usernameto:
<?php
session_start(); // startsession
$Username=$_SESSION['UserID'];
$Usernameto= $_SESSION['UserTO'];
$db = mysql_connect("at-web2.xxxxxx", "yyyyy", "xxxxxxx");
mysql_select_db("db_xxxxxx",$db);
$result1 = mysql_query("SELECT user_lon and user_lat FROM table1 WHERE id = '$Usernameto'");
$result2 = mysql_query("SELECT user_lon and user_lat FROM table1 WHERE id = '$Username'");
$myrow1 = mysql_fetch_row($result1);
$myrow2 = mysql_fetch_row($result2);
while($myrow1)
{
$_Mylon=$myrow1[0];
$_Mylat=$myrow1[1];
}
while($myrow2)
{
$_Mylon2=$myrow2[0];
$_Mylat2=$myrow2[1];
}
?>
Edit - just realized that you didn't tell us what wasn't working about the code you provided. Are you getting an error message or are you not getting the correct data back? You still should fix your query, but we'll need some more information to know what's wrong.
Your query statements shouldn't have "and" between the select parameters, so it should be:
Edit 2 - I just noticed that you had a while loop that you don't need, try this:
$result1 = mysql_query("SELECT user_lon, user_lat FROM table1 WHERE id = '$Usernameto'");
$result2 = mysql_query("SELECT user_lon, user_lat FROM table1 WHERE id = '$Username'");
$myrow1 = mysql_fetch_row($result1);
$myrow2 = mysql_fetch_row($result2);
if (isset($myrow1)) {
$_Mylon=$myrow1[0];
$_Mylat=$myrow1[1];
}
if (isset($myrow2)) {
$_Mylon2=$myrow2[0];
$_Mylat2=$myrow2[1];
}
An example from the php manual echoing an html table
I don't know if you can derive what you need from this?
More specific: You can use:
$line = mysql_fetch_array($result, MYSQL_ASSOC);
I am using the following with a quiz I am making, it checks the table to see if the user has already posted the answer to the question and isn't hammering the submit button, the problem I am having is its not posting the info to the database for some odd reason. If I take out the if statement and the first database looking and just post the data it works fine, but when I check the result of the first query first, it doesn't appear to work, even if the user hasn't submitted an answer yet.
<?php
$quizID = $_GET['quiz'];
$userID = $_GET['user'];
$quizselectanswer = $_POST['quizselectanswer'];
$cf_created = date("y/m/d");
$questionID = $_POST['questionID'];
// Check to see if user answered question already
$result = mysql_query("SELECT questionID,userID FROM itsnb_chronoforms_data_answerquiz WHERE questionID='$questionID' AND userID='$userID' LIMIT 1") or die(mysql_error());
while($row = mysql_fetch_array($result))
{
if (empty($row))
{
mysql_query("INSERT INTO itsnb_chronoforms_data_answerquiz (cf_created, questionID,quizselectanswer,quizID, userID)
VALUES ('$cf_created', '$questionID', '$quizselectanswer', '$quizID','$userID')")
or
die(mysql_error());
}else{
}
}
?>
My database looks like this itsnb_chronoforms_data_answerquiz cf_id, cf_uid, cf_created, cf_modified ,cf_ipaddress ,cf_user_id ,questionID, quizselectanswer, quizID ,userID.
Try like this it may help you
// Check to see if user answered question already
$result = mysql_query("SELECT * FROM itsnb_chronoforms_data_answerquiz WHERE questionID='$questionID' AND userID='$userID' LIMIT 1") or die(mysql_error());
$row = mysql_fetch_array($result);
if(!empty($row))
{
while($row){
//some statement
}
}else{
mysql_query('INSERT INTO itsnb_chronoforms_data_answerquiz (cf_created, questionID,quizselectanswer, quizID, userID)
VALUES ("'.$cf_created.'", "'.$questionID.'", "'.$quizselectanswer.'", "'.$quizID.'","'.$userID.'")') or die(mysql_error());
}
?>
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
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.