Jump to content

Recommended Posts

I have to admit, I am a PHP NOVICE, and don't get that much practice.  A few years ago I created some code that worked for my purpose, but now my server has upgraded PHP to version 5.3, and the old code won't work at all.

 

Here is the old code that used to work:

<?php

// Connection to DB

mysql_connect("server", "user",
"password") or die('Cannot connect to the database because: ' . mysql_error());

mysql_select_db ("database");

// Retrieve all Region 6 Legislators from the "contact_info" table
$result = mysql_query("SELECT * FROM contact_info WHERE branch = 'House' AND district = '11' ORDER BY district")
or die(mysql_error());  
// Start building the table for showing results using "while" loop

echo "<table width='75%' border='0' cellpadding='10' cellspacing='0' bgcolor='#f0f0f0' style='margin-left:100px; margin-bottom:20px'>";
// Here is the "while" loop
while($row = mysql_fetch_array( $result )) {
	// Print out the contents of each row into a table row
	echo "<tr valign='top'><td width='45%'><b>"; 
	echo "House District: </b><font color='red'>",$row['district'],"</font><br>";
	echo $row['first_name']," ",$row['last_name'],"<br>";
	echo $row['address'],"<br>";
	echo $row['csz'],"</td>";
	echo "<td width='55%'><b>County(ies): </b><font color='red'>",$row['county'],"</font><br>";
	echo "Capitol Phone: <font color='green'>",$row['cap_phone'],"</font><br>";
	echo "Office Phone: <font color='green'>",$row['bus_phone'],"</font><br>";
	echo "Home Phone: <font color='green'>",$row['home_phone'],"</font><br>";
	echo "Email: <a href='mailto:",$row['email'],"'>",$row['email'],"</a></td></tr>";
	} 
	echo "</table>"; 
?>

So, I've been Googling and trying to adapt my code to the new version using some other scripts that I have updated, but I'm not having any luck at all.  Here is the modified code I'm trying to use now, which does NOTHING.  I have even tried to get it to print out the number of rows in my result, but it prints NOTHING.

<?php

// Connection to DB

$mysqli = new mysqli("localhost", "user", "password", "database");
if($mysqli->connect_errno > 0){
    die('Unable to connect to database [' . $db->connect_error . ']');
}

// Retrieve all matching legislators
$query = 'SELECT * FROM contact_info WHERE branch = 'House' AND district = '10' ORDER BY district';

$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
 
   /* determine number of rows result set */
    $row_cnt = mysqli_num_rows($result);
	
	echo "There are ",$row_cnt," rows in the result.";
 
// Start building the table for showing results using "while" loop

echo "<table width='75%' border='0' cellpadding='10' cellspacing='0' bgcolor='#f0f0f0' style='margin-left:100px; margin-bottom:20px'>";
// Here is the "while" loop
while($row = $result->fetch_array(MYSQLI_ASSOC)) {
	// Print out the contents of each row into a table row
	echo "<tr valign='top'><td width='45%'><b>"; 
	echo "House District: </b><font color='red'>",$row['district'],"</font><br>";
	echo $row['first_name']," ",$row['last_name'],"<br>";
	echo $row['address'],"<br>";
	echo $row['csz'],"</td>";
	echo "<td width='55%'><b>County(ies): </b><font color='red'>",$row['county'],"</font><br>";
	echo "Capitol Phone: <font color='green'>",$row['cap_phone'],"</font><br>";
	echo "Office Phone: <font color='green'>",$row['bus_phone'],"</font><br>";
	echo "Home Phone: <font color='green'>",$row['home_phone'],"</font><br>";
	echo "Email: <a href='mailto:",$row['email'],"'>",$row['email'],"</a></td></tr>";
	} 
	echo "</table>"; 
?>

I have a feeling that I'm missing a thing or two, or something I have in there is not correct.  Any help will be so greatly appreciated.  Thanks!

what output are you getting from the page? and if it's a blank page, what does the 'view source' in your browser show?

 

next, do you have php's error_reporting set to E_ALL and display_errors set to ON in the php.ini on your development system so that php would help you by reporting and displaying all the errors it detects? you can put these settings into your main file, but if your main file contains a php syntax error, which is typically what causes a blank page, it won't help you because your code never runs in this case to affect the error_reporting/display_errors settings.

 

you also have an error in the database connection code error handling. there's no $db variable being used, so there's no $db->connect_error to use in the error handling.

 

edit: and based on examining the code closer and on the color highlighting, you do have a php syntax error due to putting single-quotes inside of an overall single-quoted string. having php's error_reporting/display_errors set as suggested, in the php.ini on your development system, will save you a TON of time when trying to learn, learn anything new, develop code, or debug code.

Edited by mac_gyver

You are better off not using a comma for concatenation and use a decimal point instead.

In some cases like returning from a function a comma won't work.

Return will only do a single expression while an echo will do a list.

Comma creates a list of expressions for echo, echo concatenates the list when it prints it on one line.

 

A decimal point is the concatenation operator, use that to avoid any future issues.

<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);

// Connection to DB

$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_errno > 0) {
    die('Unable to connect to database [' . $mysqli->connect_error . ']');
}

// Retrieve all matching legislators
$query = "SELECT * FROM contact_info WHERE branch = 'House' AND district = '10' ORDER BY district";

$result = $mysqli->query($query) or die($mysqli->error . __LINE__);

/* determine number of rows result set */
$row_cnt = mysqli_num_rows($result);

echo "There are $row_cnt rows in the result.";

// Start building the table for showing results using "while" loop

echo "<table width='75%' border='0' cellpadding='10' cellspacing='0' bgcolor='#f0f0f0' style='margin-left:100px; margin-bottom:20px'>";
// Here is the "while" loop
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
    // Print out the contents of each row into a table row
    echo "<tr valign='top'><td width='45%'><b>";
    echo "House District: </b><font color='red'>" . $row['district'] . "</font><br>";
    echo $row['first_name'] . " " . $row['last_name'] . "<br>";
    echo $row['address'] . "<br>";
    echo $row['csz'] . "</td>";
    echo "<td width='55%'><b>County(ies): </b><font color='red'>" . $row['county'] . "</font><br>";
    echo "Capitol Phone: <font color='green'>" . $row['cap_phone'] . "</font><br>";
    echo "Office Phone: <font color='green'>" . $row['bus_phone'] . "</font><br>";
    echo "Home Phone: <font color='green'>" . $row['home_phone'] . "</font><br>";
    echo "Email: <a href='mailto:" . $row['email'] . "'>" . $row['email'] . "</a></td></tr>";
}
echo "</table>";
?>  

That is a whole mess of echoing and escaping. Here ya go...

 

Option One

echo <<<EOT
<tr valign='top'>
   <td width='45%'><b>
      House District: </b><font color='red'>{$row['district']}</font><br>
      {row['first_name']} {$row['last_name']}<br>
      {row['address']}<br>
      {row['csz']}
   </td>
   <td width='55%'>
      <b>County(ies): </b><font color='red'>{$row['county']}</font><br>
      Capitol Phone: <font color='green'>{$row['cap_phone']}</font><br>
      Office Phone: <font color='green'>{$row['bus_phone']}</font><br>
      Home Phone: <font color='green'>{$row['home_phone']}</font><br>
      Email: <a href='mailto:{$row['email']}'>{$row['email']}</a>
   </td>
</tr>
EOT;

Option Two

<?php while ($row = $result->fetch_array(MYSQLI_ASSOC)): ?>
<tr valign='top'>
   <td width='45%'>
      <b>House District: </b>
      <font color='red'><?= $row['district'] ?></font><br>
      <?= $row['first_name'] ?> <?= $row['last_name'] ?><br>
      <?= $row['address'] ?><br>
      <?= $row['csz'] ?>
   </td>
   <td width='55%'>
      <b>County(ies): </b><font color='red'><?= $row['county'] ?></font><br>
      Capitol Phone: <font color='green'><?= $row['cap_phone'] ?></font><br>
      Office Phone: <font color='green'><?= $row['bus_phone'] ?></font><br>
      Home Phone: <font color='green'><?= $row['home_phone'] ?></font><br>
      Email: <a href='mailto:$row['email']'><?= $row['email'] ?></a>
   </td>
</tr>
<?php endwhile;?>
Edited by benanamen

What are you even trying to do, spock9458?

 

So your hoster has updated your PHP version, and your old code has “stopped working”. But instead of actually investing the problem, you've started to randomly insert new PHP features you found somewhere on the Internet, and now the code “doesn't work” in any PHP version. That helps you how exactly?

 

Do you just want to get your application online again? Then don't change things that never caused any problems in the first place. Identify the actual incompatibilities by analyzing the error log and reading the PHP 5.3 Migration Guide.

 

Or do you want to clean up your entire code and make it ready for the future? Then you need to actually understand the current state of PHP. Appending the letter “i” to your mysql_* functions doesn't cut it.

  • Why did you pick the MySQLi extension in the first place? Just because the name looked familiar? This extension is very cumbersome, it's naturally restricted to the MySQL database system, and a lot of users are very unhappy with it. So before you make a choice, you should consider the alternatives like PDO.
  • Both PDO and MySQLi are very different from the old MySQL extension, they're not a drop-in replacement. Dynamic queries are now implemented with prepared statements, and the manual error handling has been replaced with more intelligent approaches. So before you use the new extensions, learn them.
  • Your error handling is generally nonsensical. You've cluttered your entire code with die(mysql_error()) statements which print the error messages right on the user's screen. Why would you do that? Are your users supposed to somehow debug your database issues? Again: Learn and understand the features, don't just copy-and-paste stuff you found somewhere on the Internet.
  • You currently have no security whatsoever. You just insert all kinds of variables into all kinds of contexts. Never heard of cross-site scripting? SQL injections?
  • Your PHPSQLHTMLCSS mixture makes the code extremely hard to read. Consider using a template engine like Twig and putting the style declarations into separate CSS files.

I know this is a lot, and of course you cannot do this all at once. My point is: Get clear about what you want to do and what the tasks are. Then write the code. Right now, you have it backwards.

Congratulations! Your super-crappy host has updated PHP to a version that is no longer supported after last year. You need to find yourself a *real* host that supports at least 5.5, move your code there and do the updating required in your code. 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.