Discussion:
What is my Mistake?
Ethan Rosenberg
2014-06-24 05:27:48 UTC
Permalink
Dear List -

I know I have a mistake here, but I cannot find it.

This is a part of a switch.

The switch is fed with a formatted phone number [123-456-7890], which is
then tested for validity, and if valid the results of the query are
displayed. I cannot get to the display part.

Here is the code:

case 'step28':
{
echo "here we are, step 28";

$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>
<?php

break;
}

TIA

Ethan
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Karl DeSaulniers
2014-06-24 07:46:51 UTC
Permalink
Post by Ethan Rosenberg
Dear List -
I know I have a mistake here, but I cannot find it.
This is a part of a switch.
The switch is fed with a formatted phone number [123-456-7890], which is then tested for validity, and if valid the results of the query are displayed. I cannot get to the display part.
...
Post by Ethan Rosenberg
TIA
Ethan
Hi Ethan,
Try this. I did it on the fly and haven't tested, but I think it will put you on the right path.
You most likely will have to put your own juice on $return_string.
I tried to follow as best as I could to the type of output your wanting.
HTH.

[CODE]

switch (step) {
case 'step28':
$return_string = 'Here we are, Step 28';
$Phone = "";
$phn = $_POST['phone'];
$dsh = '-';
$i = 0;
while($i < strlen($phn)) {
if($i === 2 || $i === 6) {
$Phone .= $phn[$i].$dsh;
} else {
$Phone .= $phn[$i];
}
$i++;
}
$sql1 ="SELECT Cust_Num, Lname, Fname FROM Customers WHERE Phone = '".mysqli_real_escape_string($Phone)."' ";
$result1 = mysqli_query($cxn, $sql1);
$return_string .= 'here2';
if ( 0 === $result1->num_rows ) {
$return_string = '<div style="text-align:center;"><strong style="margin-bottom:32px;">No Match Found</strong></div>';
} else {
$return_string .= 'here3';
$return_string .= '<br />result..... '.$result1;
$result = 0;
$return_string .= '<div style="text-align:center;">
<table border="4" cellpadding="5" cellspacing="55" rules="all" frame="box">
<tr class="heading">
<th>Cust. Number</th>
<th>Last Name</th>
<th>First Name</th>
</tr>';
$row1 = mysqli_fetch_row($result1);
while($row1) {
$return_string .= '<tr>
<td>'.htmlspecialchars($row1[0]).'</td>
<td>'.htmlspecialchars($row1[1]).'</td>
<td>'.htmlspecialchars($row1[2]).'</td>
</tr>';
}
}
$return_string .= '</table></div>';
break;
}

[END CODE]

Best,

Karl DeSaulniers
Design Drumm
http://designdrumm.com
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Karl DeSaulniers
2014-06-24 07:53:42 UTC
Permalink
Post by Karl DeSaulniers
Post by Ethan Rosenberg
Dear List -
I know I have a mistake here, but I cannot find it.
This is a part of a switch.
The switch is fed with a formatted phone number [123-456-7890], which is then tested for validity, and if valid the results of the query are displayed. I cannot get to the display part.
...
Post by Ethan Rosenberg
TIA
Ethan
Hi Ethan,
Try this. I did it on the fly and haven't tested, but I think it will put you on the right path.
You most likely will have to put your own juice on $return_string.
I tried to follow as best as I could to the type of output your wanting.
HTH.
[CODE]
switch (step) {
$return_string = 'Here we are, Step 28';
$Phone = "";
$phn = $_POST['phone'];
$dsh = '-';
$i = 0;
while($i < strlen($phn)) {
if($i === 2 || $i === 6) {
$Phone .= $phn[$i].$dsh;
} else {
$Phone .= $phn[$i];
}
$i++;
}
$sql1 ="SELECT Cust_Num, Lname, Fname FROM Customers WHERE Phone = '".mysqli_real_escape_string($Phone)."' ";
$result1 = mysqli_query($cxn, $sql1);
$return_string .= 'here2';
if ( 0 === $result1->num_rows ) {
$return_string = '<div style="text-align:center;"><strong style="margin-bottom:32px;">No Match Found</strong></div>';
} else {
$return_string .= 'here3';
$return_string .= '<br />result..... '.$result1;
$result = 0;
$return_string .= '<div style="text-align:center;">
<table border="4" cellpadding="5" cellspacing="55" rules="all" frame="box">
<tr class="heading">
<th>Cust. Number</th>
<th>Last Name</th>
<th>First Name</th>
</tr>';
$row1 = mysqli_fetch_row($result1);
while($row1) {
$return_string .= '<tr>
<td>'.htmlspecialchars($row1[0]).'</td>
<td>'.htmlspecialchars($row1[1]).'</td>
<td>'.htmlspecialchars($row1[2]).'</td>
</tr>';
}
}
$return_string .= '</table></div>';
break;
}
[END CODE]
Best,
Karl DeSaulniers
Design Drumm
http://designdrumm.com
Oh and you might have to check if $i is equal to the string length to get the 10th number.
So swap this part.


while($i < strlen($phn)) {


with this...


while($i <= strlen($phn)) {


Thought of it after the fact.. sorry.

Best,

Karl DeSaulniers
Design Drumm
http://designdrumm.com
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Karl DeSaulniers
2014-06-24 08:10:01 UTC
Permalink
Post by Karl DeSaulniers
Post by Karl DeSaulniers
Post by Ethan Rosenberg
Dear List -
I know I have a mistake here, but I cannot find it.
This is a part of a switch.
The switch is fed with a formatted phone number [123-456-7890], which is then tested for validity, and if valid the results of the query are displayed. I cannot get to the display part.
...
Post by Ethan Rosenberg
TIA
Ethan
Hi Ethan,
Try this. I did it on the fly and haven't tested, but I think it will put you on the right path.
You most likely will have to put your own juice on $return_string.
I tried to follow as best as I could to the type of output your wanting.
HTH.
[CODE]
switch (step) {
$return_string = 'Here we are, Step 28';
$Phone = "";
$phn = $_POST['phone'];
$dsh = '-';
$i = 0;
while($i < strlen($phn)) {
if($i === 2 || $i === 6) {
$Phone .= $phn[$i].$dsh;
} else {
$Phone .= $phn[$i];
}
$i++;
}
$sql1 ="SELECT Cust_Num, Lname, Fname FROM Customers WHERE Phone = '".mysqli_real_escape_string($Phone)."' ";
$result1 = mysqli_query($cxn, $sql1);
$return_string .= 'here2';
if ( 0 === $result1->num_rows ) {
$return_string = '<div style="text-align:center;"><strong style="margin-bottom:32px;">No Match Found</strong></div>';
} else {
$return_string .= 'here3';
$return_string .= '<br />result..... '.$result1;
$result = 0;
$return_string .= '<div style="text-align:center;">
<table border="4" cellpadding="5" cellspacing="55" rules="all" frame="box">
<tr class="heading">
<th>Cust. Number</th>
<th>Last Name</th>
<th>First Name</th>
</tr>';
$row1 = mysqli_fetch_row($result1);
while($row1) {
$return_string .= '<tr>
<td>'.htmlspecialchars($row1[0]).'</td>
<td>'.htmlspecialchars($row1[1]).'</td>
<td>'.htmlspecialchars($row1[2]).'</td>
</tr>';
}
}
$return_string .= '</table></div>';
break;
}
[END CODE]
Best,
Karl DeSaulniers
Design Drumm
http://designdrumm.com
Oh and you might have to check if $i is equal to the string length to get the 10th number.
So swap this part.
while($i < strlen($phn)) {
with this...
while($i <= strlen($phn)) {
Thought of it after the fact.. sorry.
Best,
Karl DeSaulniers
Design Drumm
http://designdrumm.com
Also Ethan,
Here are some links I'd like to share with you which I think if you peruse through these,
will give you a better understanding on,

one: how to protect the database

http://us2.php.net//manual/en/mysqli.real-escape-string.php


two: how to display the data from that database.

http://www.php.net//manual/en/function.htmlspecialchars.php

Both are pretty quick reads and will give you more familiarity with what your working on I believe.
I noticed you never use these and IMO they are essential to good database programming.
HTH,

Best,

Karl DeSaulniers
Design Drumm
http://designdrumm.com

PS: Others may have a better way then I, but this was a quick throw together. Forgive me if it isn't 100% on the money.
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Jim Giner
2014-06-24 13:39:27 UTC
Permalink
Ethan -
You say your script stops here : echo 'here3'; To be sure, you mean
that the script DOES echo out 'here3' or does NOT get there? I'm going
to guess that it does get to that echo statement but the very next one
is going to kill you because you cannot echo out a resource.

ONCE AGAIN - do you have php error checking turned on as you've been
told many many times?
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Ethan Rosenberg, PhD
2014-06-24 16:27:24 UTC
Permalink
Post by Jim Giner
Ethan -
You say your script stops here : echo 'here3'; To be sure, you mean
that the script DOES echo out 'here3' or does NOT get there? I'm going
to guess that it does get to that echo statement but the very next one
is going to kill you because you cannot echo out a resource.
ONCE AGAIN - do you have php error checking turned on as you've been
told many many times?
Jim -

Thanks.

ini_set('display_errors', 'on');
ini_set('display_startup_errors', 'on');
error_reporting(-2);

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

from the program.

here3 is never echoed.

The same code in a free standing program [attached] works beautifully.

TIA

Ethan
Jim Giner
2014-06-24 17:26:16 UTC
Permalink
I don't recognize your error reporting configuration. Try this instead:

error_reporting(E_ALL | E_NOTICE);
ini_set('display_errors','1');


As for php startup errors, if you are not running your own installation,
you don't need that.

So - if 'here3' is never echoed, then your code does NOT stop there - it
obviously stopped somewhere prior to that. Add more debugging lines to
isolate exactly how far it gets.

(I assume that all of your other echos did appear?)
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Jim Giner
2014-06-24 17:55:43 UTC
Permalink
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.
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Ethan Rosenberg, PhD
2014-06-25 00:12:49 UTC
Permalink
Post by Jim Giner
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
Ethan Rosenberg, PhD
2014-06-25 04:33:58 UTC
Permalink
Post by Ethan Rosenberg, PhD
Post by Jim Giner
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.
Loading...