$result1 = mysql_query("SELECT id FROM p_u_r WHERE p_name = '$d'");
$S=$row['id'];
if(mysql_num_rows($result1) == 0) {
row not found, do stuff...
}
this code send an error of undefined index error. I have created p_u_r table in my database but no data is inserted.
$result1 = mysql_query("SELECT id FROM p_u_r WHERE p_name = '$d'");
$S=$row['id'];
if(mysql_num_rows($result1) == 0) {
row not found, do stuff...
}
It is likely because $row... doesn't exist. You should write something like that:
// assuming you have created $d already...
$result1 = mysql_query("SELECT id FROM p_u_r WHERE p_name = '$d'");
if(mysql_num_rows($result1) == 0) {
// row not found, do stuff...
}
else {
//fetch your results using mysql_fetch_assoc() or mysql_fetch_row()
//...
}
BTW Do you know using MySQL extension is DEPRECATED and you should create code using MySQLi ?
Related
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'";
I need to check if a record exists in a table before adding it.
I've done some digging and this is what people keep coming back too:
$result= mysql_query("SELECT id FROM mytable WHERE city = 'c7'");
if(mysql_num_rows($result) == 0) {
// row not found, do stuff...
} else {
//row found, do other stuff...
}
or some variation there of.
This logic is exactly what I need except for the fact that $result is never returning a positive result.
The record does exist and should return a positive result.
I also tried
$sql="SELECT COUNT(email) FROM table WHERE email=$mail;";
$yesorno = mysqli_query($sql);
echo $yesorno ;
as a test and the echo returns no value.
you need to check first if the query succeed running maybe there is a problem with it
$Query = "Select id from mytable where city = 'c7' ";
if($result = mysql_query($Query)) {
if ( mysql_num_rows($result) == 0 ) {
// no rows found ;
} else {
// row exist ;
}
} else {
echo "Query failed ". $Query;
}
This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 6 years ago.
I am trying to check if record exist in SQL and get a return -> True or False.
It returns a notice:
Notice: Trying to get property of non-object in.... if($result->num_rows > 0)
$connection = new mysqli('localhost', 'user', 'pass','db_name');
$query = "SELECT * FROM order WHERE telephone = '".$telephone."' AND order_status_id='0' ";
$result = $connection->query($query);
if($result->num_rows > 0) {
echo 'found'; // The record(s) do exist
}
else{
echo 'not found'; // No record found
}
$connection->close();
ORDER is a mysql reserved word. Using it as field name, table name or whatever except ORDER BY fieldname requires using backticks:
SELECT * FROM `order` WHERE ....
Otherwise, you will get a query error, which will turn $result into boolean false.
Also, your code is vulnerable to sql-injection.
I advise you to use prepared statements.
You are checking the rows of the query object. The query object does not contain the rows.
What you want to do is something like
$data = $result->fetch_array();
if ($data->num_rows > 0)
{
//Rows exist
} else {
//No rows exist
}
It happens in case query do not injects object in your $result variable. You can make the following changes to proceed.
if(is_object($result) && $result->num_rows > 0) {
echo 'found'; // The record(s) do exist
} else{
echo 'not found'; // No record found
}
OR
if(!empty($result->num_rows)) {
echo 'found'; // The record(s) do exist
} else{
echo 'not found'; // No record found
}
Also keep the thing simple:
$query = "SELECT * FROM `order` WHERE telephone = '$telephone' AND order_status_id='0' ";
Use like this
$sqlReq = " SELECT * FROM order WHERE telephone = '".$telephone."' AND order_status_id='0' ";
$queryReq = mysql_query($sqlReq) or die(mysql_error());
if(mysql_num_rows($queryReq) > 0)
{
echo "found";
}else{
echo "not found";
}
say I have a variable
$id = mt_rand();
how can I query the mysql database to see if the variable exists in the row id, if it does exist then change the variable $id, once the variable is unique to all other stored ids, then insert it into the database?
Thanks you guys.
$con = mysql_connect("<host>","<login>","<pass>");
if ($con) {
mysql_select_db('<schemata>', $con);
$found = false;
while (!$found) {
$idIamSearching = mt_rand();
$query = mysql_query("SELECT count(*) FROM <table> WHERE <idColumnName>='".$idIamSearching."'");
$result = mysql_fetch_row($query);
if ($result[0] > 0) {
mysql_query("INSERT INTO <table> (<column>) VALUES ('".$idIamSearching."')");
$found = true;
}
}
mysql_close($con);
}
Your description is hard to understand, so, this is something that could give you pointers...
'SELECT COUNT(*) as count from table where row_id="'.$variable.'" LIMIT 1'
make sure to escape the variable if it's user input or if it's going to have more than alphanumeric characters
then fetch the row and check if count is 1 or greater than 0
if one, then it exists and try again (in a loop)
although, auto increment on the id field would allow you to avoid this step
$bExists = 0;
while(!$bExists){
// Randomly generate id variable
$result = mysql_query("SELECT * FROM table WHERE id=$id");
if($result){
if(mysql_num_rows($result) > 0){
$bExists = 1;
} else {
// Insert into database
$bExists = 1;
}
}
1 Randomly generate id variable
2 Query database for it
2.1 Result? exit
2.2 No result? Insert