Converting a string number to an actual integer

Hi all,

I’m trying to pull a variable from a database that is stored as a varchar…the variable is a 1…

so i want to perform an if statement where if that variable is greater than 1, then do this…
Below is what i have so far…

     $sql = $link->prepare("SELECT * FROM qutSurvey WHERE quote_id = :quote_id LIMIT 1");
    			   $sql->setFetchMode(PDO::FETCH_ASSOC);
    			   $sql->execute([':quote_id' => $a]);
     				if ($row = $sql->fetch()) ;

      $no_of_baths = $row['no_of_baths'];
    			   $no_of_showers = $row['no_of_showers'];

    			   $qty_bathrooms = intval($no_of_baths);

    			   if( $qty_bathrooms > 1  ){

    				echo '<p>more than one bathroom</p>';
    			   
    			  
    			  };



    ;?>

Well, you can “cast” it… Or, just add zero to it. Here is what they say about this process:

Instead of having to choose whether to convert the string to int or float , you can simply add a 0 to it, and PHP will automatically convert the result to a numeric type. You can always add zero to it! In PHP you can use intval(string) or floatval(string) functions to convert strings to numbers.

I would use cast. $num=intval($mystring); It is more clear than adding a zero…

Store numbers using the proper numerical data type. Use an int(integer) data type for integers, so that the values will sort correctly, use the least amount of storage, and result in the fastest queries.

I’m going to guess that you wrote this post because something about this isn’t working. The posted code contains some missing syntax and has some stray ; characters that aren’t doing anything.

The if ($row = $sql->fetch()) ; line of code isn’t a complete if(){} statement, because the ; character on the end of that line terminates the statement. It doesn’t belong there. There’s also no {} present around the statements that should only be executed if the sql query matched a row of data and you should have logic to output a message if the query didn’t match any data.

Next the ; after a } doesn’t do anything - }; <— here.

And, finally a ; as the only thing on a line doesn’t do anything - ;?>

Semi-colons ; terminate php statements that do something, such as assignment statements, function/method calls… They don’t go on the end of everything.

In your example of what you would do, I’ve done something similar;

Your example: $num=intval($mystring);
Mine: $qty_bathrooms = intval($no_of_baths);

Have I missed something?

Thank you.

You’re absolutely right with how to store the correct datatype, as using integer is appropriate, i just wanted to see what it took to convert the string into a digit, a numerical value.

What’s not happening is the value im calling isn’t being looked at as a numerical value so when i performed or wrote, greater than 1, and i knew the value was 2 i wanted it to echo the text if it was.

I did only paste part of the code and added the ; here in the forums input box.

The full code is;
<?php
$a = session_id();
$sql = $link->prepare(“SELECT * FROM qutSurvey WHERE quote_id = :quote_id LIMIT 1”);
$sql->setFetchMode(PDO::FETCH_ASSOC);
$sql->execute([’:quote_id’ => $a]);
if ($row = $sql->fetch()) ;

			$no_of_baths = $row['no_of_baths'];
                            $qty_bathrooms = intval($no_of_baths);

		   if( $qty_bathrooms > 1  ){

			echo '<p>more than one bathroom</p>';

;?>

	<div class="container px-4">

Display design/information here

<?php };?>

Thank you for your help…

Yes, your conversion is good. I have found in my humble experience (40 years of programming) that sometimes you need to store data from other sources and often that means text. So, converting to integer is needed in some cases. Especially when scraping data. But, learning is a process.

Php automatically converts strings consisting of numerical values to a number for magnitude comparisons, i.e. numbers take precedence. If what you are doing isn’t working, its due to something else, such as the logic you have that’s not actually testing if a row of data was fetched before using the data.

This also indicates that you probably don’t have php’s error_reporting set to E_ALL and display_errors set to ON, so that php will help you by reporting and displaying all the errors that it detects.

Here’s a version of your code listing a bunch of programming practices, that in most cases simplifies the code -

<?php

// build the sql query statement in a php variable. this makes debugging easier and separates the sql syntax as much as possible from the php syntax.
// don't use SELECT *. list the columns you want in the SELECT term
// for what you are showing you are doing, there's no point in a LIMIT term in this query
// if you use ? place-holders, it will cut down on the amount of typing, simplifying the code
$sql = "SELECT no_of_baths, no_of_showers FROM qutSurvey WHERE quote_id = ?";

// your database connection variable should be named as to what it is, i.e. $pdo
$stmt = $pdo->prepare($sql);

// the default fetch mode should be set to assoc when you make the connection, so that you don't need to keep setting or specifying it throughout your code
// $sql->setFetchMode(PDO::FETCH_ASSOC);

// variables should be named as to the meaning of the data in the variable. don't use variables with names like $a
$stmt->execute([ $a ]);

// you should fetch the data into an appropriately named variable in the php 'business' logic, that knows how to query for and fetch the data
// then test/use that variable at the correct point in the html document
$quote_data = $stmt->fetch();


// inside the html document...
// test/use the data, displaying a message if there is no matching data
if(!$quote_data)
{
	// because the failure code is usually much shorter than the success code, complement the condition being tested and put the failure code first. this will result in easier to read code
	echo 'No result found.';
}
else
{
	// don't copy variables to other variables for no reason. this is just a waste of time typing. just use the original variables
	// $no_of_baths = $row['no_of_baths'];
	// $no_of_showers = $row['no_of_showers'];
	//$qty_bathrooms = intval($no_of_baths);

	if($quote_data['no_of_baths'] > 1)
	{
		echo '<p>more than one bathroom</p>';
	}
}
Sponsor our Newsletter | Privacy Policy | Terms of Service