1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Failed to connect to MySQL

Discussion in 'PHP' started by pshaw, Apr 18, 2022.

  1. #1
    Hi, please someone advise what is wrong with this code.I copied the code from other programs that work. I changed the db and tale names.
    ==========================================================
    the code:

      <?php
    $mysqli = new mysqli("localhost","root","","bpdb");
    
    // Check connection
    if ($mysqli -> connect_errno) {
      echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
      exit();
    }
    
    
    //MySqli Select Query
    $results = $mysqli->query ("SELECT * FROM bptbl");
    
    echo "<center>";echo date('m/d/y');
    
    $ckdate = 'ckdate';
    $cktime = 'cktime';
    $bp = 'bp';
    $rate = 'rate';
    $issue = 'issue';
    ?>
    
    <!DOCTYPE html><html>
    <head>
    
    <STYLE TYPE="text/css">
    .blue {background-color: #ccffff;}
    .tan {background-color: #FFD4FF;}
    h1.centered-cell {text-align: center;font-size: 1.1em;}
    </STYLE>
    
    </head>
    <body><center><b><font size=+1>Vitals Report<br><br>
    
      <table border='1' cellpadding="4">
      <thead>
      <tr>
      <TH>Date</TH>
      <TH>Time</TH>
      <TH>Bp</TH>
      <TH>Rate</TH>
      <TH>Issue</TH>
      </tr>
      </thead>
      <tbody>
    
    <?php
    /* ---------------------------------------------- */
      while($row = mysqli_fetch_array($results))  {
    /* ---------------------------------------------- */
      echo "<tr>
      <td>{$row['ckdate']}</td>
      <td>{$row['cktime']}</td>
      <td>{$row['bp']}</td>
      <td>{$row['rate']}</td>
      <td>{$row['issue']}</td>
    </tr>
    </table";
      }
      ?>
      </tbody></font></b></center></body</html>
    PHP:
    ==========================================================
    error message:
    Vitals Report

    Date Time Bp Rate Issue
    {$row['ckdate']} {$row['cktime']} {$row['bp']} {$row['rate']} {$row['issue']}
     
    Last edited by a moderator: Apr 19, 2022
    pshaw, Apr 18, 2022 IP
  2. devialt

    devialt Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #2
    Could we see table 'bptbl' information in your database ?
     
    devialt, Apr 22, 2022 IP
  3. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #3
    If the block you have saying it's the error message is in fact what PHP is reporting, it's crashing there due to invalid code and it has nothing to do with your database. If I were to take a wild guess (I'm more of a PDO than mysqli guy) I'd say the spaces around the -> aren't helping and could be the error, but also ->errNo and ->error are functions, not variables so you have to have () after using them. You also seem to be trying to echo out the rows using associative (named) references, when you're fetching them as array, not assoc.

    Also that HTML is horrifyingly bad. Tags and attributes that haven't been valid in two and a half decades, willy-nilly use of upper and lower case, lack of SCOPE on your TH, center + font doing CAPTION's job, 24 year out of data FONT tag wrapping things inline-level tags can't even wrap (like a table), closing the TABLE on each loop before the TBODY is even closed... Hopping in and out of PHP for Christmas only knows what, output before the doctype throwing you into quirks client-side... You've got PROBLEMS.

    <!DOCTYPE html><html lang="en"><head>
    
    	<meta charset="utf-8">
    	<meta
    		name="viewport"
    		content="width=device-width,height=device-height,initial-scale=1"
    	>
    	
    	<link
    		rel="stylesheet"
    		href="screen.css"
    		media="screen"
    	>
    	
    	<title>
    		Vitals Test
    	</title>
    	
    </head><body>
    
    	<time>', date('m/d/y'),'</time>
    	
    	<table class="vitalsReport">
    		<caption>Vitals Report</caption>
    		<thead>
    			<tr>
    				<th scope="col">Date<</th>
    				<th scope="col">Time<</th>
    				<th scope="col">Bp<</th>
    				<th scope="col">Rate<</th>
    				<th scope="col">Issue<</th>
    			</tr>
    		</thead><tbody>';
    
    <?php
    
    	$mysqli = new mysqli('localhost', 'root', '', 'bpdb');
    
    	if ($mysqli->connect_errno()) throw new RuntimeException(
    		'Failed to connect to MySQL: ' . $mysqli->connect_error()
    	);
    
    	$result = $mysqli->query('SELECT * FROM bptbl');
    
    	while ($row = $result->fetch_assoc()) echo '
    			<tr>
    				<td>', $row['ckdate'], '</td>
    				<td>', $row['cktime'], '</td>
    				<td>', $row['bp'], '</td>
    				<td>', $row['rate'], '</td>
    				<td>', $row['issue'], '</td>
    			</tr>';
    			
    ?>
    
    		</tbody>
    	</table>
    	
    </body></html>
    Code (markup):
    EVERYTHING else you're trying to do there -- fonts and colours -- belongs in an external stylesheet, not in your HTML or PHP.

    Also, single quotes and comma delimits are your friend. Try to avoid double quotes when you can. They're garbage legibility and can also be slower. You also don't have to "flip" the quotes or escape them as much when writing HTML output.
     
    deathshadow, Apr 23, 2022 IP