I have 3 queries like
$q1 = select email from tbl students
$q2 = select email from tbl corporates
$q3 = select email from tbl institutes
i want check to all 3 tables if email count is zero from all above then only do proceed like that,,
can i write like this:
if(mysqli_num_rows($q1) || mysqli_num_rows($q2) || mysqli_num_rows($q3) ==0){
proceed to process.
}else{
do nothing
}
My question is it a correct way to approach or anything is better left which i need to learn.
you need to use & instead of || and also check rows count for each of your mysql object
if(mysqli_num_rows($q1) == 0 && mysqli_num_rows($q2) == 0 && mysqli_num_rows($q3) ==0){
//proceed to process.
} else {
//do nothing
}
You should execute those queries first so you will have:
$db = mysqli_connect("localhost","my_user","my_password","my_db");
$q1 = "select email from tbl students";
$q2 = "select email from tbl corporates";
$q3 = "select email from tbl institutes";
$db_q1 = mysqli_query($db, $q1);
$db_q2 = mysqli_query($db, $q2);
$db_q3 = mysqli_query($db, $q3);
if(mysqli_num_rows($db_q1) == 0 && mysqli_num_rows($db_q2) == 0 && mysqli_num_rows($db_q3) == 0) {
proceed to process.
}
try can do easier using mysqli_multi_query()
$con=mysqli_connect("localhost","my_user","my_password","my_db");// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = select email from tbl students";
$sql .= select email from tbl corporates";
$sql .= "select email from tbl institutes";
// Execute multi query
if (mysqli_multi_query($con,$sql)){
do{ // Store first result set
if ($result=mysqli_store_result($con)) { // Fetch one and one row
if (mysqli_fetch_row($result) == 0) {
//your code here
}
}
}
Related
names of the tables includes (epin and user)
checks include;
if epin exist in tableA(epin) and TableB(user) then echo "epin taken"
if epin exist in tableA(epin) only then insert epin to tableb(user)
if epin do not exist in tableA then echo "Invalid pint"
any help please
You can find code with PHP and MySQL.
Suppose we are storing epin search text in below variable.
$searchText = 'searchValue'; // or $searchText = $_GET['searchValue'];
here tableA is epin and TableB is user.
Suppose field name of epin in tableA is epinFieldA and in tableB is epinFieldB.
First point
$sql1 = mysqli_query($link, "SELECT count(*) as cnt1 FROM epin where epinFieldA = '" .$searchText. "'");
$result1 = mysqli_fetch_assoc($sql1);
$sql2 = mysqli_query($link, "SELECT count(*) as cnt2 FROM user where epinFieldB = '" .$searchText. "'");
$result2 = mysqli_fetch_assoc($sql2);
if ($result1['cnt1'] > 0 && $result1['cnt2'] > 0) {
echo "epin taken";
}
Second point
if ($result1['cnt1'] > 0 && $result1['cnt2'] == 0) {
//insert data query
}
Third point
if ($result1['cnt1'] == 0) {
echo "Invalid pint";
}
I have two databases and i have one table "TabelaX" in database "Servidor1" with out data and other database "Servidor2" with one table "TabelaY". And i want do one select in table "TabelaY" and with her data do one Update in table "TabelaX" which is in another database. I already made some code but it is not working correctly.
<?php
$conn= mysqli_connect('localhost','root',null,'Servidor2') or die
(mysqli_connect_error());
if (!$conn) {
die("Falha de conexao: ". mysqli_connect_error());
}
$ID = $_POST['ID'];
$sql = "SELECT * FROM TabelaY WHERE ID = $ID";
$result = mysqli_query($conn, $sql);
mysqli_select_db($conn,"Servidor1");
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$row1 = $row["ID"];
$row2 = $row["Data"];
}
} else {
echo "0 results";
}
$sql = "INSERT INTO Servidor1.TabelaX (ID, Data)
SELECT ID, Data
FROM Servidor3.TabelaW
WHERE ID = $ID;";
$sql = "UPDATE Servidor1.TabelaX SELECT ID, Data FROM
Servidor3.TabelaW SET Data = $row2 WHERE $row1 = $ID;";
if (mysqli_multi_query($conn, $sql)) {
echo "Dados Inseridos";
} if (mysqli_multi_query($conn, $sql)) {
echo "Dados Atualizados";
}
mysqli_close($conn);
I have no idea what your query is trying to do, because you assign to $sql twice without ever executing the first query, but if you're asking how to update a row in tableX based on data from tableY, then:
UPDATE Servidor1.TabelaX as x, Servidor2.TabelaY as y
SET x.Data = y.Data
WHERE x.id = y.id
AND x.id = $someIdForWhichYouWantToUpdate
Also, do not do this:
$ID = $_POST['ID'];
$sql = "SELECT * FROM TabelaY WHERE ID = $ID";
Imagine what happens when the user posts 1; DROP DATABASE Servidor1 into the form. This is called SQL injection and your code is full of vulnerabilities to it.
I have this php code:
$query = $database->query("SELECT EXISTS(SELECT * FROM contacts WHERE contact_id = '$contactID')";
if($query == 0){
echo "not registered";
}elseif($query == 1){
echo "registered"
}
If I'm not wrong, the query is suppose to return 0 or 1 and it works in my SQLite manager. What is the correct way on getting that value in Php and use it in IF ELSE statement?
If you only need a single value, you can use querySingle:
$result = $database->querySingle("SELECT EXISTS(SELECT * FROM contacts WHERE contact_id = '$contactID'");
Otherwise, with normal queries, the result returned by ->query isn't actually the data itself, but an identifier you would use to get data from the database:
$results = $db->query('SELECT bar FROM foo');
while ($row = $results->fetchArray()) {
var_dump($row);
}
i am trying to insert into multi table after select query return 0 (not found raws) select query working and insert query never done when submite "displayid" and there is no any syntax error
code:
<?php
if ($_POST["displayid"] == TRUE) {
$sqlid = "SELECT * FROM doc1 WHERE idnum ='$pidnum' AND stats='$ok'";
$result = mysqli_query($conn, $sqlid);
if (mysqli_num_rows($result) > 0) {
$sqlup = "UPDATE doc1 SET m_phone='$pm_phone', seen='$dataseen' WHERE idnum ='$pidnum'";
mysqli_query($conn, $sqlup);
$found = 1;
} else {
$found = 0;
$sqlfail = "INSERT INTO fail(fname,lname,tname,funame,idnum,m_phone,reg_date)
VALUES ('$pfname','$plname','$ptname','$pfuname','$pidnum','$pm_phone','$todaydate')";
$conn->query($sqlfail)
}
}
?>
Use this code:
$sqlfail = "INSERT INTO fail(fname,lname,tname,funame,idnum,m_phone,reg_date)
VALUES ('".$pfname."','".$plname."','".$ptname."','".$pfuname."','".$pidnum."','".$pm_phone."','".$todaydate."')";
make similar changes for update command as well
you actually have one error
$conn->query($sqlfail)
should be
$conn->query($sqlfail);
AND stats='$ok'";
i can't see a variable with this name i think you mean AND stats='ok'";
When getting data from the databas my if statement isn't working as expected, even though I have the $badgename in the database for that user I got "You get a new badge". But it dosen't put it on.
Im new to MySQLI so it's probably something I missed...
$numberofposts=$row['posts'];
$userid = $_SESSION['userid'];
$badgename = "Legend";
if($numberofposts >= 10){
$SQL = $mysqli->query("SELECT 1 FROM `badges` WHERE `mid`='$userid' AND 'badge' = '$badgename'");
$num = $SQL->num_rows;
if($num > 0){
echo "You got a new badge";
$mysqli->query("INSERT INTO badges ('mid', 'badge') VALUES ('$userid', '$badgename')");
}
else
{
echo "You already have this badge";
}
Thanks.
YOu have typo's in both queries:
replace the single quotes( ' ) on badge with backticks( ` )
from your select query.
then replace the single quotes with backticks in the insert query. ex:
$numberofposts=$row['posts'];
$userid = $_SESSION['userid'];
$badgename = "Legend";
if($numberofposts >= 10){
$SQL = $mysqli->query("SELECT 1 FROM `badges` WHERE `mid`='$userid' AND `badge` = '$badgename'");
$num = $SQL->num_rows;
if($num > 0){
echo "You got a new badge";
$mysqli->query("INSERT INTO `badges` (`mid`, `badge`) VALUES ('$userid', '$badgename')");
}
else
{
echo "You already have this badge";
}
Your INSERT query is wrong.
You have single-quotes around your column names, but you should be using backticks instead (like in the SELECT query)