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

[email protected] ("Ethan Rosenberg, PhD") Wed, 25 Jun 2014 00:33:58 -0400
Newsgroups php.db
Organization Hygeia Biomedical Research, Inc.
Message-ID <[email protected]>
On 06/24/2014 08:12 PM, Ethan Rosenberg, PhD wrote:
> 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
>

Jim -

I think I know the problem.

These two lines of code were added -

									$nmbr=mysqli_num_rows ( $result1 );
									echo '<br />number of rows $nmbr';

The echo statement does not return a number.
Therefore ... bad connection. The script failed at the output stage 
because there were no results.

I'm again attaching the two files. They are somewhat modified.  The only 
thing in the first file is the form. The second file contains the 
calculations

TIA

Ethan



I have made the linnk $cxn a global variable in the in both scripts.
TestPh.php (application/x-httpd-php, 2.3 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>
<?php
									global $cxn;
									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';
			$cxn = mysqli_connect($host,$user,$passwd);
			$db = "Store";
			echo 'hello2';
			
			if (!$cxn) 
			{
	    		die('Connect Error (' . mysqli_connect_errno() . ') '
	            . mysqli_connect_error());
			}
			

									$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
									} 
									$nmbr=mysqli_num_rows ($result1);
									echo '<br />number of rows $nmbr';
									echo 'here3'; //stops at this point
									echo "<br />result.....  $result1";
									print_r($result1);
?>
					 
									<div align="center">
									<table border="4" cellpadding="5" cellspacing="55" rules="all" frame="box">
									<tr class='heading'>
									<th>Customer Number</th>
									<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>
StartTestPh2.php (application/x-httpd-php, 619 B)
<!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>
        <div></div>
    </body>
</html>