Good day,
i have the following code in php
$sellast2 = "SELECT id, staffid, password FROM staff WHERE staffid=$staffid";
$result4 = $pdo->prepare($sellast2);
$result4->execute();
$rowcount = $result4->rowCount();
echo $rowcount;
I am expecting that the row count would be one since this table only has one record in it.
The variable is outputting -1 and not 1 as expected.
What does the minus mean and why does it output a minus?
I am using Microsoft sql server management studio as the database.
$sellast2 = "SELECT id, staffid, password FROM staff WHERE staffid='$staffid'";
use your $staffid in single quotes like i did, then pass a valid staff id and get your result
Related
When we update a MySQL record with php, we can check if it has effect using:
$mysqli->affected_rows;
But how do I check which column has been modified?
Example, in my table have the columns: id / name / age
In a record we have the data: 1 / Woton / 18
If I send an: UPDATE mytable SET name = 'Woton', age = '20' WHERE id = '1'
Only the age field has changed. How can I determine this?
You cannot directly get the updated columns from the query result.
It can be get from some php query. Firstly we will have to select the row from database which we are going to update in a array variable. Than run the update query for the same row.
Lastly get the same row from database from select query in the new array variable.
Finally we get two arrays.
We can get the updated column with the array_diff_assoc php function.
See the below code for the same.
$sql = "SELECT * from mytable where id=1 limit 1";
$prev = mysqli_fetch_assoc(mysqli_query($conn, $sql));
//Get the column data in the array. Before update.
$sql = "UPDATE mytable SET name = 'Woton', age = '20' WHERE id = '1'";
$conn->query($sql);
// Update data
$sql = "SELECT * from mytable where id=1 limit 1";
$updated = mysqli_fetch_assoc(mysqli_query($conn, $sql));
// Again run the select command to get updated data.
$UpdatedColumns=array_diff_assoc($updated,$prev);
In a different note: If QueryLog has been enabled in the DB then you (or your script in PHP or Python or any) can easily see/read which part of the content has been updated and even you can monitor the DB.
The good news is, even you can target which table, which query etc.
Basically, I have a database with 10 exam types. Each type has two parts, and is updated as pass or fail. I need to list the total count of exams that have not been completed (both parts passed).
I've tried this and it returns the count if either part shows pass, not both.
$query = sprintf(
"SELECT * FROM candidate_exams
WHERE gID='1' AND canID='%d' AND exResult='y'
GROUP BY gEID",
(int) $canID
);
$result = $con->query($query);
$rowCount = 10 - mysqli_num_rows($result);
'gID' is an identifier that tracks what group these 10 exams come from,
'canID' is a candidate identifier,
'gEID' is an exam type.
SELECT COUNT(*) FROM tabele WHERE type_a = 'fail' OR type_b = 'fail'
something like this?
It would help a lot to see your table structure to be able to answer this question properly.
echo $query = "SELECT user_id FROM login WHERE username = '8578896785'";
echo $result = $mysql_query($query);
cannot fetch contact number when use in php
I executed the same query on mysql and it gives results. The same query does not work in PHP.
The column is of varchar(200) datatype and contains data as string,alphanumeric as well as numbers
if u use numeric date to fetch then u have to use
SELECT user_id FROM login WHERE username = 123 withought ''
and when you try to fetch string data
SELECT user_id FROM login WHERE username = 'abc' with ''
you can't output a resource with the echo statement, you have to fetch the data in the result before you can output it.
$query = "SELECT user_id FROM login WHERE username = '8578896785' LIMIT 1";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
echo $row['user_id'];
1st Edit: removed unnecessary echo's
2nd Edit: adding a LIMIT 1 to your query, the username schould be just one time in the table ;)
3rd Edit: removed $ in front of a function (mysql_query())
4th Edit: fetching data from the ressource (as array, for other methods to fetch the data look into the php manuals)
5th Edit: output the userid
I want to insert data to database. I have a table, named member that has 7 column (ID, User, Password, Address, Phone, Gender, Email). I used count to make auto number like this
$no = "SELECT COUNT(ID)FROM member";
$nors = mysql_query($no);
$nors = $nors + 1;
$query = "INSERT INTO member VALUES (".$nors.",'".$user."','".md5($pass)."','".$name."','".$addr."',".$hp.",'".$gender."','".$email."')";
Why, the result of nors is 6 not 2, although I only have 1 data?
mysql_query returns a result object, not the value. Your query also lacks a needed space between COUNT(ID) and FROM...
$no = "SELECT COUNT(ID) AS count FROM member";
$result = mysql_query($no);
$row = mysql_fetch_object($result);
$nors = $row->count;
You should consider using something more modern like PDO, though, as mysql_* functions have been deprecated and will eventually go away entirely.
edit: #andrewsi noted in the comments that you really should be using MySQL's built-in auto increment functionality for IDs, anyways. Much better than what you're currently doing.
If you're using this to generate the next ID number for a new member, you should look at making ID an auto_increment field instead - as it stands, it's possible that you'll get two members signing up at the same time, and both getting assigned the same ID
Replace this line
$nors = mysql_query($no);
By these lines :
$result_handler = mysql_query($no);
$result = mysql_fetch_array($result_handler);
$nors = $result[0];
If your id field is set to be an auto number you don't need to insert it. MySql will handle that for you. Anytime you add a new row the autonumber is incremented. If you delete a row the autonumber does not decrement.
If you currently only have 1 row but you've added and deleted rows then your insert will produce a row with an ID that is not consecutive.
I have an application to add friends. I need to have my script to check, if the users' already friends. I thought I could do this by a COUNT. I did like this:
$username = $_GET[user];
$ven_til_id = $_SESSION['userID'];
$num = 1;
if(isset($_GET['add_friend'])){
$check=("SELECT username,ven_til_id, COUNT(*) AS num FROM friends WHERE username=$username AND ven_til_id=$ven_til_id GROUP BY username,ven_til_id")or die(mysql_error());
$result=mysql_query($check);
if(!$result){
echo "Cant run query.";
} else {
$num = mysql_num_rows($result);
}
if($num>0){
header("Location: /profil/$username?add_error");
} else {
$sql=mysql_query("INSERT INTO friends (username,ven_til_id)VALUES('$username', '$ven_til_id')")or die(mysql_error());
header("Location: /profil/$username");
}
}
?>
But when I'm adding one friend it's fine. It adds it and everything is fine. But then when I try to add another, it says we're already friends. I guess it's because it's counting how many times my ID (ven_til_id) is listed in the tables.
You're missing a comma:
SELECT
username,ven_til_id COUNT(*) AS num FROM ...
should be
SELECT
username,ven_til_id, COUNT(*) AS num FROM ...
Also, your reference to the count field is incorrect - it should be the third column or $row[2]
You may want to make your code more robust by referring to fields by name eg $row['num']
One final thing to confirm is that the value being retrieved with a count is being treated as an integer not a string. I don't think it's the problem here but you may want to explicitly cast it to avoid possible issues later eg...
$num = (int) $row[2];
Option 1
Just select the appropriate rows and see how many records you get back...
SELECT username,
ven_til_id
FROM friends
WHERE username=$username
AND ven_til_id=$ven_til_id
Then just count the number of records returned using PHP - eg mysql_num_rows() (I think that's the correct function name)
Clarification:
Change
$row = mysql_fetch_row($result);
$num = $row[2];
to
$num = mysql_num_rows($result);
Option 2
Get MySQL to do the counting for you - in qhich case you need to tell it to group multiple record together...
SELECT username,
ven_til_id,
COUNT(*) as Num
FROM friends
WHERE username=$username
AND ven_til_id=$ven_til_id
GROUP BY username,
ven_til_id
Then just read the 3rd value of the first row (num) and you'll have a count
NB: The second method may be overkill if you're only ever expecting a 1 or a 0