Re: [PHP-DB] Re: What is my Mistake?

[email protected] ("Ethan Rosenberg, PhD") Tue, 24 Jun 2014 20:12:49 -0400
Newsgroups php.db
Organization Hygeia Biomedical Research, Inc.
Message-ID <[email protected]>
On 06/24/2014 01:55 PM, Jim Giner wrote:
> Looking at your code again, I think that your query failed and your lack
> of error reporting isn't showing the error that occurred when  you tried
> to access a property of the result which is not a resource but merely a
> value of 'false'.
>
> One should ALWAYS check the result of operations before assuming that
> they succeeded and proceeding onwards in code.  Saves lots of lost time.
>

Jim -

Thanks.

Error checking as per your instructions.
No errors.

Attached are two files.  I tried to mimic the behavior of the program.
One file does the data input and the second processes it.

The display on the browser screen is

select Cust_Num, Lname, Fname from Customers where Phone = 
'845-123-3298' here2here3
result.....
Last Name 	First Name

When the above query is run from the command line -

mysql> select Cust_Num, Lname, Fname from Customers where Phone = 
'845-123-3298';
+----------+--------+-------+
| Cust_Num | Lname  | Fname |
+----------+--------+-------+
|        1 | Kitten | Gingy |
|     1153 | Puppy  | Woofy |
|     1154 | Puppy  | Woofy |
+----------+--------+-------+

TIA

Ethan
StartTestPh.php (application/x-httpd-php, 2.1 KB)
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <title></title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
	
			<div align="center">
				<form method="post" action="TestPh.php">
            <input type='text' name=phone></input>
            <input type='submit'>
			<br /><br /><br />
            </form>
			</div>
<?php
			ini_set('display_errors', '1');
			ini_set('display_startup_errors', 'on'); 
			error_reporting(-1);

			ini_set('error_reporting', 'E_ALL | E_STRICT');
			ini_set('html_errors', 'On');
			ini_set('log_errors', 'On'); 

			require '/home/ethan/PHP/ethan.inc';
			$db = "Store";
			$cxn = mysqli_connect($host,$user,$password,$db);
		//	$cxn = mysqli_connect("A","B","C","D");
//			$cxn = mysqli_connect("localhost", "root", "SdR3908", "Store");
//			print_r($cxn);
			
			if (!$cxn) 
			{
	    		die('Connect Error (' . mysqli_connect_errno() . ') '
	            . mysqli_connect_error());
			}
			
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}			

//echo $_SERVER["DOCUMENT_ROOT"];
//echo 'Success... ' . mysqli_get_host_info($link) . "\n";			
//echo 'connection  <br />';
//print_r($cxn); 
//print_r($_POST);			
			$phn = $_POST[phone];
			$phn = (string)$phn;
			
//echo $phn;
//echo "<br />$phn[0]$phn[1]$phn[2]<br />";
			$dsh = '-';
			$Phn = $phn[0].$phn[1].$phn[2].$dsh.$phn[3].$phn[4].$phn[5].$dsh.$phn[6].$phn[7].$phn[8].$phn[9];
//echo $Phn;			
			$sql1 ="select Cust_Num, Lname, Fname from Customers where Phone = '$Phn' ";

//			echo $sql1;
			$result1 = mysqli_query($cxn, $sql1);
//echo '<br />result1....$result1';	
//print_r($result1);
//var_dump($result1);

			if ( 0 === $result1->num_rows ) 
			{
?>				
				<div align="center">
				
				<strong>No Match Found</strong>
				<br /><br />
				</div>
<?php
			}

?>
        <div>TODO write content</div>
    </body>
</html>
TestPh.php (application/x-httpd-php, 2 KB)
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
<?php
									ini_set('display_errors', '1');
									ini_set('display_startup_errors', 'on'); 
									error_reporting(-1);

									ini_set('error_reporting', 'E_ALL | E_STRICT');
									ini_set('html_errors', 'On');
									ini_set('log_errors', 'On'); 

									$phn = $_POST['phone'];
									$phn = (string)$phn;
									$dsh = '-';
									$Phn = $phn[0].$phn[1].$phn[2].$dsh.$phn[3].$phn[4].$phn[5].$dsh.$phn[6].$phn[7].$phn[8].$phn[9];
									$sql1 ="select Cust_Num, Lname, Fname from Customers where Phone = '$Phn' ";
									echo $sql1;
									$result1 = mysqli_query($cxn, $sql1);
									echo 'here2';									
									if ( 0 === $result1->num_rows ) 
									{
?>				
										<div align="center">

										<strong>No Match Found</strong>
										<br /><br />
										</div>
<?php
									} 
									
									echo 'here3'; //stops at this point
									echo "<br />result.....  $result1";
									print_r($result1);
									$result = 0;
?>
					 
									<div align="center">
									<table border="4" cellpadding="5" cellspacing="55" rules="all" frame="box">
									<tr class='heading'>
									<th>Last Name</th>     
									<th>First Name</th>
<?php

									while($row1 = mysqli_fetch_row($result1))
									{
										$Cust_Num  = $row1[0];
										$Lname     = $row1[1];
										$Fname     = $row1[2];



?>
										<tr>
										<td> <?php echo $Cust_Num; ?> </td>
										<td> <?php echo $Lname; ?> </td>
										<td> <?php echo $Fname; ?> </td>
										</tr>
<?php                                    
									}
								
?>
              </table>
			</div>		
    </body>
</html>