I'm wondering if i can use 2 variables with the same name on the same code and it will still work or i need to give every query a diff name? (I know its a super simple question) ;)
Here is an example:
$sql="DELETE FROM apps WHERE app_id='".$app_id."'";
mysql_query($sql) or die(mysql_error());
$sql="DELETE FROM statistics_apps WHERE app_id='".$app_id."'";
mysql_query($sql) or die(mysql_error());
Do i need to change the second $sql to $sql2?
Thanks!
Do i need to change the second $sql to $sql2?
No. as long as you make sure to execute $sql (version1) before $sql (version2)
There's no need.
On the other hand if you are doing:
$sql="SELECT * FROM apps WHERE app_id='".$app_id."'";
$result = mysql_query($sql) or die(mysql_error());
$sql="SELECT * FROM statistics_apps WHERE app_id='".$app_id."'";
$result2 = mysql_query($sql) or die(mysql_error());
You must safe the $result query handle in different vars, if you do not fetch all rows from query1 before you are start with query2.
as long as you call your first mysql_query($sql) before you refill your $sql-variable, it is no problem. You just use one variable which you just give another value :)
You don't need to change your $sql variable. What you need is keep the return value of mysql_query.
$sql="DELETE FROM apps WHERE app_id='".$app_id."'";
$query1 = mysql_query($sql) or die(mysql_error());
$sql="DELETE FROM statistics_apps WHERE app_id='".$app_id."'";
$query2 = mysql_query($sql) or die(mysql_error());
var_dump(mysql_affected_rows($query1)); // deleted in apps
var_dump(mysql_affected_rows($query2)); // deleted in statistics_apps
No, you do not need to have a separate variable for each query, unless you need/want to refer to the original query at some later point in the code.
No you do not... because at the end it like write that:
mysql_query("DELETE FROM apps WHERE app_id='".$app_id."'") or die(mysql_error());
mysql_query("DELETE FROM statistics_apps WHERE app_id='".$app_id."'") or die(mysql_error());
the value of your variable $sql is changed
Related
session_start();
$user=$_SESSION['username'];
$con=mysqli_connect("$host","$username","$password","$db_name");
$sql1="SELECT UserID FROM User WHERE username='$user'";
$query=mysqli_query($con,$sql1);
echo $query;
Im not sure what's the problem with this, I've tried searching around here and tried different ways of writing the syntax and checking for errors in parameter but still no output for the command. What am i missing here?
UPDATE
$sql1="SELECT UserID FROM User WHERE username='$user'";
$query=mysqli_query($sql1,$con);
$row=mysqli_fetch_array($query);
$uid=$row[0];
echo $uid;
As answered by the others, I tried doing this. There is still no output or an output of 0 was given. Where if I tried the query in sql, it shows the right one.
UPDATE 2
It tried the var_dump() command and it returns NULL. Does that mean it can't read the database?
mysqli_query gives no output. A successfully query returns a resource. You have to handle the resource with mysqli_fetch_object, mysqli_fetch_assoc or mysqli_fetch_array.
Also you gave $con parameter before $sql1 which is wrong. You have to put the connection first, then the query variable.
Example:
<?php
session_start();
$user = $_SESSION['username'];
$con = mysqli_connect("$host", "$username", "$password", "$db_name");
$sql1 = "SELECT UserID FROM User WHERE username='$user'";
$query = mysqli_query($con, $sql1);
$fetch = mysqli_fetch_assoc($query);
var_dump($fetch);
?>
$query will be a "Resource". You can use for example $result = mysqli_fetch_array($query); and then echo $result[0];May there's another way, but that's a simple one!
I was able to do some workaround with my problem using mysql syntax. Still not able to determine the problem with mysqli but able to finished my task. Thanks for answering guys.
After a search here I couldn't see anyone with the same (strange) problem as me.
I have a very simple task which is to check whether some name already exists on a table, but the thing is the mysql_num_rows is returning wrong values.
I'm sorry, I forgot to mention that this only happens when I try to look for words with special chars. Bebês, Câmeras, Calção are examples.
$sql = "
SELECT cattitle as category
FROM categories
WHERE cattitle = '$title'
";
$res = mysql_query($sql, $con) or die(mysql_error());
$num = mysql_num_rows($res);
I even have tried with mysql_result
$sql = "
SELECT count(cattitle) as category
FROM categories
WHERE cattitle = '$title'
";
$res = mysql_query($sql, $con) or die(mysql_error());
$num = mysql_result($res,0);
The worst thing is, when I run the query directly, I get the correct results ($num > 0).
I'm not that experienced programmer and, at first, I thought it was returning values from other queries, but I've checked and changed the name of those vars and the problem persisted.
Could be some kind of conflict? Can someone help me with this error?
Kind regards,
Your example is basic one, and it should work. It is even in PHP manual as example.
Try this way:
$sql = "
SELECT cattitle
FROM categories
WHERE cattitle = '$title'
LIMIT 1
";
$res = mysql_query($sql, $con) or die(mysql_error());
if ($row = mysql_fetch_assoc($res)) {
# category already exists, do smth?
}
I think your problem may be in wrong charsets for your mysql database and for string in your PHP script.
When you use mysql console then it convert symbols in correct charset.
Check charset and collate for your table and field cattitle.
Best solution will change it to UTF8
Also use UTF8 when write your script
I am trying to get the difference in seconds between the date/time in the column 'last_visited' in my table 'users' and the current date/time for a particular user. I think the following query should do it:
$query ="SELECT unix_timestamp(NOW()) - unix_timestamp(last_visit) from users WHERE username='$user'";
$result=mysql_query($query) or die(mysql_error());
However, I am not sure how to get the result from this as a variable so that I can pass it to a PHP function. Could someone help with this?
Return the result with a name:
$query ="SELECT unix_timestamp(NOW()) - unix_timestamp(last_visit) AS time_diff from users WHERE username='$user'";
$result=mysql_query($query) or die(mysql_error());
while($row=mysql_fetch_assoc($result))
{
$diff= $row['time_diff']
}
$query ="SELECT unix_timestamp(NOW()) - unix_timestamp(last_visit) AS time_difference from users WHERE username='$user'";
^
I guess it will be easier to use this variable if it has a proper name.
Can I get from PHP a value back like the new id from the row I've just added to the database or should I make a SELECT to retrieve it?
<?php
$sql = "INSERT INTO my_table (column_1, column_2) VALUES ('hello', 'ciao')";
$res = mysql_query ($sql) or die (mysql_error ());
$sql = "SELECT column_id FROM my_table WHERE column_1 = 'hello'";
$res = mysql_query ($sql) or die (mysql_error ());
$row = mysql_fetch_assoc ($res);
$id = $row["column_id"];
print "my id is = $id";
?>
Use this: http://php.net/manual/en/function.mysql-insert-id.php
Selecting can be dangerous because an auto-increment often means that records may not otherwise be unique, and therefore not uniquely selectable without the id.
The proper way of getting the id is via mysql_insert_id(), as others have stated. The reason for this is that you may have other inserts taking place immediately following yours, and simply requesting the last id is not guaranteed to return the id that you expected.
$result = mysql_query("INSERT INTO tableName (col1) VALUES ('foo')");
print mysql_insert_id();
There is builtin support for it, mysql_insert_id() or something.
i have a field in table opt named confirm of type tinyint. i want to insert value(1) by this statement but it is not working can any one help??
$connect= mysql_connect("localhost","root") or die ("Sorry, Can not connect to database");
mysql_select_db("login") or die (mysql_error());
$user=$_POST['staff'];
echo $user;
$query="SELECT * from users where username='$user' ";
$result=mysql_query($query,$connect) or die(mysql_error());
$row=mysql_fetch_array($result);
$uid=$row['userid'];
echo $uid;
$query="SELECT * from opt where userid='$uid' ";
$result=mysql_query($query,$connect) or die(mysql_error());
$row=mysql_fetch_array($result);
if($row['confirm']==0)
{
$query = "INSERT INTO opt (confirm) values(1)";
echo 'The user selected options has confirmed';
}
?>
You are not executing the query.
add an extra
$result=mysql_query($query,$connect) or die(mysql_error());
after the line
$query = "INSERT INTO opt (confirm) values(1)";
Apart from not executing the "InSERT STATEMENT",
You should probably be using an
"UPDATE OPT SET CONFIRM = '1' WHERE USERID = $user;"
as the row already exists ('cause you managed to select it!).
$query is a variable and there's no reason that it would cause a record to magically get inserted into the opt table.
You need to insert the following line after $query = "...":
mysql_query($query);
Also, I hopethat's not the code you're running in production.
You need to have the following somewhere:
$user = mysql_real_escape_string($user);
Why is not working? what error is throwing?
Check the other fields of the table...