Viewed   75 times

A->b->c might exist but c might not exist. How do I check it?


if($A->b->c != null) //c exists

If c does not exist, its value will be null (or, to be more precise, it will have no value). Note, however, that for this to work, both A and b need to not be null. Otherwise, PHP will throw an error (I think).

Saturday, December 24, 2022

You have to use CURL

function does_url_exists($url) {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    if ($code == 200) {
        $status = true;
    } else {
        $status = false;
    return $status;
Saturday, October 8, 2022

Cast as whatever type you want (and makes sense...). By concatenating, you're implicitly casting to string, so

$value = (string) $xml->someNode->innerNode;
Monday, August 15, 2022

This is because the dot . is the string concatenator in php. In your case it tries to concatenate $xmlobj->News->News (which doesn't exists and is therefore empty) with the constant Env (which doesn't exists too and is treated as a string. you would get a notice about this with an appropriate error_level)

$tmp = 'News.Env';

or in short


Update: If you use SimpleXML (and according the syntax you do it) it $xmlobj "starts" with the News-(root-)Element.

Monday, October 17, 2022

The following are tried, tested and proven methods to check if a row exists.

(Some of which I use myself, or have used in the past).

Edit: I made an previous error in my syntax where I used mysqli_query() twice. Please consult the revision(s).


if (!mysqli_query($con,$query)) which should have simply read as if (!$query).

  • I apologize for overlooking that mistake.

Side note: Both '".$var."' and '$var' do the same thing. You can use either one, both are valid syntax.

Here are the two edited queries:

$query = mysqli_query($con, "SELECT * FROM emails WHERE email='".$email."'");

    if (!$query)
        die('Error: ' . mysqli_error($con));

if(mysqli_num_rows($query) > 0){

    echo "email already exists";


    // do something


and in your case:

$query = mysqli_query($dbl, "SELECT * FROM `tblUser` WHERE email='".$email."'");

    if (!$query)
        die('Error: ' . mysqli_error($dbl));

if(mysqli_num_rows($query) > 0){

    echo "email already exists";


    // do something


You can also use mysqli_ with a prepared statement method:

$query = "SELECT `email` FROM `tblUser` WHERE email=?";

if ($stmt = $dbl->prepare($query)){

        $stmt->bind_param("s", $email);


            $email_check= "";         

            if ($stmt->num_rows == 1){

            echo "That Email already exists.";


Or a PDO method with a prepared statement:

$email = $_POST['email'];

$mysql_hostname = 'xxx';
$mysql_username = 'xxx';
$mysql_password = 'xxx';
$mysql_dbname = 'xxx';

try {
$conn= new PDO("mysql:host=$mysql_hostname;dbname=$mysql_dbname", $mysql_username, $mysql_password); 
     $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
     exit( $e->getMessage() );

// assuming a named submit button

        try {
            $stmt = $conn->prepare('SELECT `email` FROM `tblUser` WHERE email = ?');
            $stmt->bindParam(1, $_POST['email']); 
            while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {

        catch(PDOException $e) {
            echo 'ERROR: ' . $e->getMessage();

    if($stmt->rowCount() > 0){
        echo "The record exists!";
    } else {
        echo "The record is non-existant.";

  • Prepared statements are best to be used to help protect against an SQL injection.


When dealing with forms and POST arrays as used/outlined above, make sure that the POST arrays contain values, that a POST method is used for the form and matching named attributes for the inputs.

  • FYI: Forms default to a GET method if not explicity instructed.

Note: <input type = "text" name = "var"> - $_POST['var'] match. $_POST['Var'] no match.

  • POST arrays are case-sensitive.



Error checking references:


Please note that MySQL APIs do not intermix, in case you may be visiting this Q&A and you're using mysql_ to connect with (and querying with).

  • You must use the same one from connecting to querying.

Consult the following about this:

  • Can I mix MySQL APIs in PHP?

If you are using the mysql_ API and have no choice to work with it, then consult the following Q&A on Stack:

  • MySql php: check if Row exists

The mysql_* functions are deprecated and will be removed from future PHP releases.

  • It's time to step into the 21st century.

You can also add a UNIQUE constraint to (a) row(s).


  • How to check if a value already exists to avoid duplicates?
  • How add unique key to existing table (with non uniques rows)
Wednesday, October 12, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :