How to fix specific PHP white screen error - php

I know there's a lot about debugging PHP but I just cant seem to figure out why this bit of code doesn't work:
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
// CONNECT TO MYSQL
$con = mysqli_connect("127.0.0.1","root","MySQLrootpass","cards");
if (mysqli_connect_errno()){
echo "<div style='text-align: center;'>";
echo "<h1>Could not connect to server, please try again later.</h1>";
echo "Failed to connect to MySQL: " . mysqli_connect_error();
echo "</div>";
}
$data = mysqli_query($con,"SELECT * FROM cards");
while($row = mysqli_fetch_array($data)){ ?>
<div id="card<?php echo $row["id"]; ?>" class="cardPost" style="background-image: url('Sprites/card.png');" onClick="sideData('card','<?php echo $row["id"]; ?>')">
<span class="cardTitle"><?php echo $row["name"]; ?></span>
<div class="elements">
<img class="elmPic" src="Sprites/elms/<?php echo $row["element"]; ?>Elm.png">
</div>
</div>
<?php}
// DISCONNECT FORM MYSQL
mysqli_close($con);
?>
I'm new to the whole PHP display error thing, so I don't know why there not displaying.
After running it through a syntax checker, it said that the } at the end of the while loop was unexpected, I don't understand why, because this code worked fine until I added the id and onClick attribute to the <div>.
Now when I remove them it still doesn't work.
Also, I know it is this while loop, because when I comment it, all works well.
I also have another on the page just like this one (with the id and onClick) that works just fine.
One more thing, when I remove a semicolon in this second while loop (which starts off hidden), all works well still.
I know this is a very specific situation, but any help would be very appreciated.

I'm new to the whole php display error thing, so I don't know why there not displaying
ini_set and error_reporting are functions: like any other functions, they are executed when your script runs. If your script has a parse error, it will never run in the first place, so your functions won't have a chance to do anything.
You can change these settings in your php.ini file instead, which will allow them to take effect before the script executes (and then you'll be able to see the error messages).
display_errors = on
error_reporting = E_ALL
After running it through a syntax checker, it said that the } at the end of the while loop was unexpected, I don't understand why
There should be whitespace between <?php and }.

As traq said, the problem is withespace between <?php and } (line 22). check this out:
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
// CONNECT TO MYSQL
$con = mysqli_connect("127.0.0.1","root","","ads");
if (mysqli_connect_errno()){
echo "<div style='text-align: center;'>";
echo "<h1>Could not connect to server, please try again later.</h1>";
echo "Failed to connect to MySQL: " . mysqli_connect_error();
echo "</div>";
}
$data = mysqli_query($con,"SELECT * FROM cards");
while($row = mysqli_fetch_array($data)){ ?>
<div id="card<?php echo $row["id"]; ?>" class="cardPost" style="background-image: url('Sprites/card.png');" onClick="sideData('card','<?php echo $row["id"]; ?>')">
<span class="cardTitle"><?php echo $row["name"]; ?></span>
<div class="elements">
<img class="elmPic" src="Sprites/elms/<?php echo $row["element"]; ?>Elm.png">
</div>
</div>
<?php }
// DISCONNECT FORM MYSQL
mysqli_close($con);
?>

Try
ini_set('display_errors','1'); // value in quotes
For special people. Visit this link .
http://php.net/manual/ru/function.ini-set.php
If not works then.
<?php
phpinfo();
/*
find where is php.ini
then tell admin to change if you dont have permissions.
*/
?>
Also you can try error_get_last

Related

image display, php mysql

I'm developing a project Personl and I have encountered a problem.
Details my of problem
Perform a query via a form, which calls me to another page where the query is displayed, everything is correct, it displays the information that I need, but when viewing the user's image, calling me by request the ID corresponding to this user, but not my image code is displayed below.
Code to display the image in html:
<img src="../../registro/imagen.php?matricula=<?php echo $_POST['matricula']; ?>" width="150px" height="150px" />
Code search the image by id to display:
<?php
$numero=$_REQUEST['matricula'];
$tabla="alumno";
include("../../Connections/colegio.php");
$conexion=#mysqli_connect($hostname_colegio,$username_colegio,$password_colegio,$database_colegio);
$sacar = "SELECT * FROM ".$tabla." WHERE (matricula=$numero)" ;
$resultado = mysqli_query($conexion,$sacar);
while ($registro = mysqli_fetch_assoc($resultado))echo mysqli_error( $conexion );{
$tipo_foto=$registro['matricula'];
header("Content-type: image/jpg");
echo $registro['matricula'];
}
mysqli_close($conexion);
?>
may terminate this issue, if it helps someone, I leave the correct code.
<?php
$numero=$_REQUEST['matricula'];
$consulta="select * from alumno WHERE (matricula = $numero)";
$busca_fotos=mysql_query($consulta) or die("Error en: $busca_fotos:" .mysql_error()) ;
while($ro=mysql_fetch_assoc($busca_fotos)){
$url=$ro['matricula'];
echo "
<img src=\"../../registro/fotos/".$url.".jpg\" width=\"150\" height=\"150\" alt=\"\" />
</a>
";
}
?>

How to get rid of notice and warning in php form?

I have a php form when I click the only the submit button without selecting any options I don't want the system to display :
Notice: Undefined variable: result in C:\XAMPP\htdocs\statistics\lecturer.php on line 83
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, null given in C:\XAMPP\htdocs\statistics\lecturer.php on line 83
So I get the notice and the warning for this line of code: while($unit=mysql_fetch_assoc($result)), especially for the $result.
lecturer.php
<?php
session_start();
include 'connect.php';
$years = array(
2005,
2006,
2007
);
$lecturers = array(
'lec1',
'lec2',
'lec3',
'lec4'
);
if(isset($_POST['submit'])){
$year = mysql_real_escape_string($_POST['year']);
$lecturer = mysql_real_escape_string($_POST['lecturer']);
/*checks if the user types the url of the page that he is not allowed to use, it leads him to the main page so to login*/
if(!isset($_SESSION['username'])){
header("location:../../statistics/main.htm");
}
$username=$_SESSION['username'];
if(!empty($lecturer) && !empty($year)){
if (in_array($lecturer, $lecturers) && in_array($year, $years)) {
$sql = mysql_query("SELECT unit_name,a1,a2,a3,l1,l2,l3,l4,l5,l6,l7,lavg,r1,r2,u1,u2,u3 FROM $lecturer WHERE year=$year)")or die(mysql_error());
$result = mysql_query($sql);
}
else{
echo "Please select a lecturer and a year.";
}
}
}
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="../../statistics/style.css">
</head>
<body>
<div id="container">
<table id="table" width="900" border="1" cellspacing="1">
<tr>
<td>Unit Name</td>
<td>A1 </td>
<td>A2 </td>
<td>A3 </td>
<td>L1 </td>
<td>L2 </td>
<td>L3 </td>
<td>L4 </td>
<td>L5 </td>
<td>L6 </td>
<td>L7 </td>
<td>LAVG </td>
<td>R1 </td>
<td>R2 </td>
<td>U1 </td>
<td>U2 </td>
<td>U3 </td>
</tr>
<?php
while($unit=mysql_fetch_assoc($result)){
echo "<tr>";
echo "<td>".$unit['unit_name']."</td>";
echo "<td>".$unit['a1']."</td>";
echo "<td>".$unit['a2']."</td>";
echo "<td>".$unit['a3']."</td>";
echo "<td>".$unit['l1']."</td>";
echo "<td>".$unit['l2']."</td>";
echo "<td>".$unit['l3']."</td>";
echo "<td>".$unit['l4']."</td>";
echo "<td>".$unit['l5']."</td>";
echo "<td>".$unit['l6']."</td>";
echo "<td>".$unit['l7']."</td>";
echo "<td>".$unit['lavg']."</td>";
echo "<td>".$unit['r1']."</td>";
echo "<td>".$unit['r2']."</td>";
echo "<td>".$unit['u1']."</td>";
echo "<td>".$unit['u2']."</td>";
echo "<td>".$unit['u3']."</td>";
echo "</tr>";
}
?>
</table>
</div>
</body>
</html>
So i assume your code are in production phase and hiding all errors from displaying. Add these on top of php block:
ini_set('error_reporting', 0); // type of error for displaying
ini_set('display_errors', 0); // 0 = hide errors; 1 = display errors
For testing phase, these codes below will show fatal error and hiding notice and warning:
ini_set('error_reporting', E_ALL & ~E_NOTICE & ~E_WARNING);
ini_set('display_errors', 1); // 0 = hide errors; 1 = display errors
The error message is telling you that you have tried to push ahead with the conditional section of your code without having all the values.
You are asking if the value is not empty which it is not but it is not empty with the value false because the post value was not set.
This is really helping you to write better code - you simply need to understand that there are missing conditions.
In your case take your line:
if(isset($_POST['submit'])){
and add a few additional constraints
if( (isset($_POST['submit'])) && (isset($_POST['year'])) && (isset($_POST['lecturer'])) ){
Now you have simple error checking and will ignore all input unless it is perfect.
However your end users will now complain that sometimes the form does not work. This is also helpful as it is telling you that you could do more with the conditionals
$noerrors = true;
if( !isset($_POST['year']) ){
echo "Did you forget to choose a year?";
$noerrors = false;
}
if( !isset($_POST['lecturer']) ){
echo "Did you forget to choose a lecturer?";
$noerrors = false;
}
if( isset($_POST['submit']) && $noerrors){
//...
The basic "rule" of development that you need to remember is to assume that users will misuse the form or other UI you made and then code for every possible abuse and misuse. Starting from the assumption that someone aims to break your system is often helpful.
Try the following:
The query:
$sql = mysql_query("SELECT `unit_name`, `a1`, `a2`, `a3`, `l1`, `l2`, `l3`, `l4`, `l5`, `l6`, `l7`, `lavg`, `r1`, `r2`, `u1`, `u2`, `u3` FROM `$lecturer` WHERE `year` = $year)") or die (mysql_error());
The backticks will prevent an error know as mysql reserved words
For the while loop do the following:
if(!empty($result)) {
while($unit = mysql_fetch_assoc($result)){
echo "<tr>";
echo "<td>".$unit['unit_name']."</td>";
echo "<td>".$unit['a1']."</td>";
echo "<td>".$unit['a2']."</td>";
echo "<td>".$unit['a3']."</td>";
echo "<td>".$unit['l1']."</td>";
echo "<td>".$unit['l2']."</td>";
echo "<td>".$unit['l3']."</td>";
echo "<td>".$unit['l4']."</td>";
echo "<td>".$unit['l5']."</td>";
echo "<td>".$unit['l6']."</td>";
echo "<td>".$unit['l7']."</td>";
echo "<td>".$unit['lavg']."</td>";
echo "<td>".$unit['r1']."</td>";
echo "<td>".$unit['r2']."</td>";
echo "<td>".$unit['u1']."</td>";
echo "<td>".$unit['u2']."</td>";
echo "<td>".$unit['u3']."</td>";
echo "</tr>";
}
}
If you still get the error / warning echo the $result just before the if statement. and let me know what you get. While trying this remove the 2 lines from #EngCy.
You should also start using mysqli_* or pdo as mysql_* is depracted and will be removed
Update
I looked at the code again and noticed a other error:
$sql = mysql_query("SELECT unit_name,a1,a2,a3,l1,l2,l3,l4,l5,l6,l7,lavg,r1,r2,u1,u2,u3 FROM $lecturer WHERE year=$year)")or die(mysql_error());
$result = mysql_query($sql);
You are basically doing the same here. You first do a query to and set the result to $sql. And when that is done you query $sql this wont work. So you can use $sql in your while loop or say $result = $sql;

Using PHP/MYSQL book but get no output from PHP when trying to access the database

I'm currently working my way through "PHP and MySQL Web Development." I've successfully created databases and been able to make tables and use the database. I've also successfully completed all the chapters on PHP and have had no problems with PHP not working up to this point. The goal of this page is return search results from a database. It's a pretty simple thing to do but for some reason nothing is being output from the script to the page. I'm getting no errors or anything. It's just blank with the title at the top. Can anyone please help me out with this? Thank you.
Here is the PHP code:
<html>
<head>
<title>Book-O-Rama Search Results</title>
</head>
<body>
<h1>Book-O-Rama Search Results</h1>
<?php
// create short variable names
$searchtype=$_POST['searchtype'];
$searchterm=trim($_POST['searchterm']);
if (!$searchtype || !$searchterm) {
echo 'You have not entered search details. Please go back and try again.';
exit;
}
if (!get_magic_quotes_gpc()){
$searchtype = addslashes($searchtype);
$searchterm = addslashes($searchterm);
}
# $db = new mysqli('localhost', 'bookorama', 'bookorama123', 'books');
if (mysqli_connect_errno()) {
echo 'Error: Could not connect to database. Please try again later.';
exit;
}
$query = "select * from books where ".$searchtype." like '%".$searchterm."%'";
$result = $db->query($query);
$num_results = $result->num_rows;
echo "<p>Number of books found: ".$num_results."</p>";
for ($i=0; $i <$num_results; $i++) {
$row = $result->fetch_assoc();
echo "<p><strong>".($i+1).". Title: ";
echo htmlspecialchars(stripslashes($row['title']));
echo "</strong><br />Author: ";
echo stripslashes($row['author']);
echo "<br />ISBN: ";
echo stripslashes($row['isbn']);
echo "<br />Price: ";
echo stripslashes($row['price']);
echo "</p>";
}
$result->free();
$db->close();
?>
</body>
</html>
You're supressing the errors from the line:
# $db = new mysqli('localhost', 'bookorama', 'bookorama123', 'books');
Thats what the # sign does, remove the # sign and verify that the connection works properly, it might be that your script fails there.
You shouldn't use that, it's not considered good practice as far as I know.
It worked for me! no white page! If you work with a editor including ftp sometimes saving the file failes. than you get a blank file. in that case safe your code and reopen the file.
as for the sql injection try this:
$searchtypes = array('type1','type2');
if (!in_array($searchtype,$searchtypes) || $searchterm=='') {
echo 'You have not entered search details. Please go back and try again.';
exit;
}
You need to create another php page that sends a post request. Here is a sample one:
Take a look at the fiddle:
<html>
<head>
<title>Book-O-Rama Search </title>
</head>
<body>
<h1>Book-O-Rama Search</h1>
<form id='uploadform' method='post' enctype='multipart/form-data' action='link to your search action php page'>
<legend>Submit form</legend><br/>
<div class='form-inputs'>
SearchType <input name='searchtype' id='searchtype'/><br>
SearchTerm <input name='searchterm' id='searchterm'/><br>
<input type="submit" value= "Search" />
</div>
</form>
</body>
</html>

positioning data on your webpage pulled from a mysql database

I am trying to position the data pulled from my mysql database using the following php code on my webpage;
<?php
require "connect.php";
$query = "select * from item LIMIT 0, 1";
$result = #mysql_query($query, $connection)
or die ("Unable to perform query<br>$query");
?>
<?php
while($row= mysql_fetch_array($result))
{
?
//call each item from the database, and nest in div or html table
<?=$row['item']?>
<?=$row['description']?>
<?=$row['brand']?>
When I view the webpage it creates the row for each item however each field is empty?
The conection to the database is fine as I ran another piece of code which simply displayed all the info in 1 block. However this is not what I want I want to be able to position each item where I want it on the site.
I wrote this when I had php version 4.1 running on IIS I am now running the latest version of php 5 and after doing some reading it says there are chnages in the syntax including global variables disabled by default so not sure if this is the issue?
try:
<?php echo $row['item'] ?>
<?php echo $row['description'] ?>
<?php echo $row['brand'] ?>
I believe the <?= ?> syntax is normally disabled by default.
The following will also show a dump of you $row array, so you can make sure that it actually contains data
<?php print_r($row); ?>
Good luck!
Sorttag maybe no enabled, try :
<?php echo $row['item']; ?>
<?php echo $row['description']; ?>
<?php echo $row['brand']; ?>

error outputting html with javascript

I have this php code, with which I am trying to generate a popup window that will contain the contents of a html file, however after adding in the script tags, no html is displayed. I tried echoing out $row2, but the word array is printed to the screen and nothing else.
<?php
session_start();
if (isset($_GET["cmd"]))
$cmd = $_GET["cmd"];
else
die("You should have a 'cmd' parameter in your URL");
$pk = $_GET["pk"];
$con = mysql_connect("localhost","root","geheim");
if(!$con)
{
die('Connection failed because of' .mysql_error());
}
mysql_select_db("ebay",$con);
if($cmd=="GetAuctionData")
{
$sql="SELECT * FROM Auctions WHERE ARTICLE_NO ='$pk'";
$sql2="SELECT ARTICLE_DESC FROM Auctions WHERE ARTICLE_NO ='$pk'";
$htmlset = mysql_query($sql2);
$row2 = mysql_fetch_array($htmlset);
echo $row2;
echo '<script>
function makewindows(){
child1 = window.open ("about:blank");
child1.document.write('.$row2["ARTICLE_DESC"].');
child1.document.close();
}
</script>';
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result))
{
echo "<div id='leftlayer'>
<strong>Article Number</strong> ".$row['ARTICLE_NO']."
<p><strong>Article Name</strong></p> ".$row['ARTICLE_NAME']."
<p><strong>Subtitle</strong></p> ".$row['SUBTITLE']."
<p><strong>Username</strong></p> ".$row['USERNAME']."
<p><strong>Total Selling</strong></p> ".$row['QUANT_TOTAL']."
<p><strong>Total Sold</strong></p> ".$row['QUANT_SOLD']."
<p><strong>Category</strong></p> ".$row['CATEGORY']."
<p><strong>Highest Bidder</strong></p> ".$row['BEST_BIDDER_ID']."
</div>
<div class='leftlayer2'>
<strong>Current Bid</strong> ".$row['CURRENT_BID']."
<p><strong>Start Price</strong></p> ".$row['START_PRICE']."
<p><strong>Buyitnow Price</strong></p> ".$row['BUYITNOW_PRICE']."
<p><strong>Bid Count</strong></p> ".$row['BID_COUNT']."
<p><strong>Start Date</strong></p> ".$row['ACCESSSTARTS']."
<p><strong>End Date</strong></p> ".$row['ACCESSENDS']."
<p><strong>Original End</strong></p> ".$row['ACCESSORIGIN_END']."
<p><strong>Auction Type</strong></p> ".$row['AUCTION_TYPE']."
</div>
<div class='leftlayer2'>
<strong>Private Auction</strong></p> ".$row['PRIVATE_AUCTION']."
<p><strong>Paypal Accepted</strong></p> ".$row['PAYPAL_ACCEPT']."
<p><strong>Auction Watched</strong></p> ".$row['WATCH']."
<p><strong>Finished</strong></p> ".$row['FINISHED']."
<p><strong>Country</strong></p> ".$row['COUNTRYCODE']."
<p><strong>Location</strong></p> ".$row['LOCATION']."
<p><strong>Conditions</strong></p> ".$row['CONDITIONS']."
</div>
<div class='leftlayer2'>
<strong>Auction Revised</strong></p> ".$row['REVISED']."
<p><strong>Cancelled</strong></p> ".$row['PRE_TERMINATED']."
<p><strong>Shipping to</strong></p> ".$row['SHIPPING_TO']."
<p><strong>Fee Insertion</strong></p> ".$row['FEE_INSERTION']."
<p><strong>Fee Final</strong></p> ".$row['FEE_FINAL']."
<p><strong>Fee Listing</strong></p> ".$row['FEE_LISTING']."
<p><a href='#' onclick='makewindows(); return false;'>Click for full description </a></p>
</div>";
$lastImg = $row['PIC_URL'];
echo "<div id='rightlayer'>Picture Picture
<img src=".$lastImg.">
</div>";
}
}
mysql_close($con);
?>
edit: I have fixed the errors that Roborg pointed out, however the script will still not load and does not give a precise error.
i have updated the code above
As well as the missing </script>,
child1.document.write('.$row2["ARTICLE_DESC"].')
should be
child1.document.write(' . json_encode($row2["ARTICLE_DESC"]) . ');
The json_encode() function will take care of any quoting for you.
Edit:
<a href='#' onclick=makewindows()> should be <a href='#' onclick='makewindows(); return false;'> - You should have quotes there, and the return false will stop you getting taken to "#" when you click the link.
Also, from memory I'm not sure you can open about:blank and then write to it - I think it sees that as cross-domain scripting. You might be better off creating a minimal "blank.html" file on your server and using that.
You have to print_r an array, like print_r($row2);
In this line:
$row2 = mysql_fetch_array($htmlset);
You are setting $row2 to be an array representing an entire row of the result from your query. Even if the "row" is just one field, it's still an array. If you want to get just the value of the first field, you can use mysql_result.
The parameters are: resource $result , int $row [, mixed $field ] so an example of the usage would be:
// get the first field of the first row
$fieldVal = mysql_result($htmlset, 0);
// get the third field
$fieldVal = mysql_result($htmlset, 0, 2);
// get the first field of the 2nd row
$fieldVal = mysql_result($htmlset, 1);
Your <script> tag is never closed and your JavaScript instructions are not ended with semicolons. It might be the source of the problem.

Categories