PHP is breaking my code - php

I'm trying to deploy a site but I get errors when the PHP/MySql code goes on the server. It worked fine in development but not in production.
My code gets down to here, with no problems:
<div class="content">
<section class="col1">
<h1>Read reviews or write us one</h1>
</section>
<section class="col2">
<button type="button" class="read">Read Reviews</button>
<?php include ('display.php'); ?>
Then when I check the source code, the HTML just stops there. I'm guessing there's something wrong with my PHP in the display.php file, which looks like this:
<?php
$con = mysql_connect('localhost','communi3_root',"password");
mysql_select_db('communi3_cfds','communi3#localhost',"password") or die(mysql_error());
$query = "select * from reviews";
$result = mysql_query($query)or die(mysql_error());
$row = mysql_fetch_array($result);
echo "<table class='displayReviews' border='1' style='width:100%;'>";
echo "<tr stlye='display:block;margin:0em auto;'><th>Name</th><th>Review</th><th>Date</th></tr>";
while ($row = mysql_fetch_array($result))
{
echo "<tr><td>";
echo $row['monicker'];
echo "</td><td>";
echo $row['review'];
echo "</td><td>";
echo $row['date'];
echo "</td></tr>";
}
echo "</table>";
mysql_close();
?>

after removing some error
<?php
$con = mysql_connect('localhost','communi3_root',"password");
mysql_select_db('communi3_cfds',$con) or die(mysql_error());
$query = "select * from reviews";
$result = mysql_query($query)or die(mysql_error());
$row = mysql_fetch_array($result);
echo "<table class='displayReviews' border='1' style='width:100%;'>";
echo "<tr stlye='display:block;margin:0em auto;'><th>Name</th><th>Review</th><th>Date</th></tr>";
while ($row = mysql_fetch_array($result))
{
echo "<tr><td>";
echo $row['monicker'];
echo "</td><td>";
echo $row['review'];
echo "</td><td>";
echo $row['date'];
echo "</td></tr>";
}
echo "</table>";
mysql_close();
?>

Related

How to retrieve data from the first row from database in php code

I wrote this code to retrieve some data from data base But the code do not display the first value in the table which is started from second row.
How I can make this code retrieve the data from first row.
<html>
<head>
<title>hello</title>
</head>
<body>
<?php
$con = mysqli_connect('localhost', 'root', '');
mysqli_select_db($con,"uoh");
$q = " SELECT * FROM student_record WHERE id =201102887;";
$result = mysqli_query($con , $q ) ;
if($row = mysqli_fetch_array($result)) {
echo "<table border=\"1\" style=\"width:500\">";
echo "<tr>";
echo "<th>courses</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row["grade"]. "</td>";
echo "</tr>";
}
echo "</table>";
}
?>
</body>
</html>
Change
if($row = mysqli_fetch_array($result)) {
to
if(mysqli_num_rows($result) > 0) {
Updated Code
<html>
<head>
<title>hello</title>
</head>
<body>
<?php
$con = mysqli_connect('localhost', 'root', '');
mysqli_select_db($con,"uoh");
$q = " SELECT * FROM student_record WHERE id =201102887;";
$result = mysqli_query($con , $q ) ;
if(mysqli_num_rows($result)>0) {
echo "<table border=\"1\" style=\"width:500\">";
echo "<tr>";
echo "<th>courses</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row["grade"]. "</td>";
echo "</tr>";
}
echo "</table>";
}
?>
</body>
</html>
as far as i know when you call this twice,
if($row = mysqli_fetch_array($result)) {
then first row will skip when you call it again in loop, what point you create if($row = mysqli_fetch_array($result)) { ? if you just want check query return you can just use if($result) {
You are calling mysqli_fetch_array two times second call is going to the second row. Try like this
<html>
<head>
<title>hello</title>
</head>
<body>
<?php
$con = mysqli_connect('localhost', 'root', '');
mysqli_select_db($con,"uoh");
$q = " SELECT * FROM student_record WHERE id =201102887;";
$result = mysqli_query($con , $q ) ;
$rows = array();
while($row = mysqli_fetch_array($result))
{
$rows[] = $row;
}
if(count($rows) > 0) {
echo "<table border=\"1\" style=\"width:500\">";
echo "<tr>";
echo "<th>courses</th>";
echo "</tr>";
foreach($rows as $row)
{
echo "<tr>";
echo "<td>" . $row["grade"]. "</td>";
echo "</tr>";
}
echo "</table>";
}
?>
</body>
</html>

PHP Button href not working

For a school project I am trying to create a shoppingcart with php only with MySQLi. For this I have a catalogue called index.php. In this is a table with the product and after every product there is a button which should add the item to the shoppingcart.
The only problem is that I cannot get the link working properly.
<?php
session_start();
include 'connect.php';
$qry = "select * from products";
$result = mysqli_query($connect, $qry);
echo "<table class='catalogue'>";
echo "<tr><th>ID</th><th>Code</th><th>Name</th><th>Description</th><th>Image</th></th><th>Price</th><th>Buy</th></tr>";
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo "<tr><td>";
echo $row['id'];
echo "</td><td>";
echo $row['product_code'];
echo "</td><td>";
echo $row['product_name'];
echo "</td><td>";
echo $row['product_desc'];
echo "</td><td>";
echo $row['product_img_name'];
echo "</td><td>";
echo $row['price'];
echo "</td><td>";
echo "<input type='submit' value='Add' href='cart.php?id=['id']'/>";
echo "</td></tr>";
}
echo "</table>";
?>
The cart.php looks like this.
<?php
session_start();
require 'connect.php';
require 'item.php';
$result = mysqli_query($connect, 'select * from products where id='.$_GET['id']);
$product = mysqli_fetch_object($result);
if(isset($_GET['id'])){
$item = new Item();
$item->id = $product->id;
$item->name = $product->product_name;
$item->price = $product->price;
$item->quantity = 1;
$_SESSION['cart'][] = $item;
}
echo "<table class='cart'>";
echo "<tr><th>ID</th><th>Name</th><th>Price</th><th>Quantity</th><th>Sub Total</th></tr>";
$cart = unserialize(serialize($_SESSION['cart']));
for($i=0; $i<count($cart); $i++){
echo "<tr><td>";
echo $cart[$i]->id;
echo "</td><td>";
echo $cart[$i]->product_name;
echo "</td><td>";
echo $cart[$i]->price;
echo "</td><td>";
echo $cart[$i]->quantity;
echo "</td><td>";
echo $cart[$i]->price * $cart[$i]->quantity;
echo "</td></tr>";
}
?>
Please forgive any other mistakes you might see, I am rather new to PHP.
Buttons don't have hrefs, anchors(<a>) do, so using an anchor it would be
echo "<a href='cart.php?id=$row[id]'/>Add</a>";
you could always style it like a button if you want it to look like one.

How do I stop my sql query from displaying the results twice?

How do I stop the results from getting displayed twice on the web page? What have I done wrong? Yes this is for a game but it helps me learn SQL and PHP. Please be genital it's my first time.
There is one database with two tables. The database is game_tittle the two tables are player_data and the second is character_data.
<?php
include('db_conn.php');
#for connecting to SQL
$sqlget = "SELECT player_data.PlayerName, Character_data.Duration, character_data.KillsZ, character_data.KillsB, character_data.KillsH, character_data.HeadshotsZ, character_data.Humanity, character_data.Generation, character_data.InstanceID FROM player_data, character_data";
$sqldata = mysqli_query($dbcon, $sqlget) or die('error getting data');
echo "<center><table>";
echo "<tr> <th>Player Name</th><th>play time</th><th>Total Kills</th><th>Bandit kills</th><th>Hero Kills</th><th>Head shots</th><th>Humanity</th><th>Alive count</th><th>Instance ID</tr>";
while ($row = mysqli_fetch_array($sqldata)) {
echo "<tr><td>";
echo $row['PlayerName'];
echo "</td><td>";
echo $row['Duration'];
echo "</td><td>";
echo $row['KillsZ'];
echo "</td><td>";
echo $row['KillsB'];
echo "</td><td>";
echo $row['KillsH'];
echo "</td><td>";
echo $row['HeadshotsZ'];
echo "</td><td>";
echo $row['Humanity'];
echo "</td><td>";
echo $row['Generation'];
echo "</td><td>";
echo $row['InstanceID'];
echo "</td></tr>";
echo "</center>";
}
echo "</table>";
#end of table
?>
Got it to work this way.
<?php
include('db_conn.php');
$sqlget = "SELECT player_data.PlayerUID, character_data.PlayerUID, player_data.PlayerName, character_data.HeadshotsZ, character_data.KillsZ, character_data.KillsH, character_data.KillsB, character_data.Alive, character_data.Generation, character_data.Humanity, character_data.InstanceID FROM player_data INNER JOIN character_data ON player_data.PlayerUID = character_data.PlayerUID";
$sqldata = mysqli_query($dbcon, $sqlget) or die('error getting data');
echo "<center><table>";
echo "<tr> <th>Player Name</th><th>Head shots</th><th>Total Kills</th><th>Hero kills</th><th>Bandit Kills</th><th>Live or dead</th><th>Lifes</th><th>Humanity</th><th>Instance ID</tr>";
while ($row = mysqli_fetch_assoc($sqldata)) {
echo "<tr><td>";
echo $row['PlayerName'];
echo "</td><td>";
echo $row['HeadshotsZ'];
echo "</td><td>";
echo $row['KillsZ'];
echo "</td><td>";
echo $row['KillsH'];
echo "</td><td>";
echo $row['KillsB'];
echo "</td><td>";
echo $row['Alive'];
echo "</td><td>";
echo $row['Generation'];
echo "</td><td>";
echo $row['Humanity'];
echo "</td><td>";
echo $row['InstanceID'];
echo "</td></tr>";
echo "</center>";
}
echo "</table>";
#end of table
?>
Bug in your SQL-query: You forgot to join the tables with a "where"-clause.

mysqli to get info from database

Trying to get info from the database into a table. Below is the code I am using but it is not populating the table. The site comes up but no info from the database. Please help, I am very new to this php thing and have no idea what I am doing apart from Google!
<body>
<?php include("header.php"); ?>
<?php
$con=mysqli_connect("localhost","username","password","database");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query = "SELECT * FROM 'teacher'";
$result = mysqli_query($con, $query);
echo "<div align=\"center\">";
echo "<table width=\"100%\">";
echo "<tr>";
echo "<th>First Name</th>";
echo "<th>Middle Name</th>";
echo "<th>Last Name</th>";
echo "</tr>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "</td><td>";
echo $row['first_name'];
echo "</td><td>";
echo $row['middle_name'];
echo "</td><td>";
echo $row['last_name'];
echo "</td></tr>";
}
echo "</table>";
mysqli_free_result($result);
mysqli_close($con);
?>
</div>
</body>
Your query is wrong
$query = "SELECT * FROM 'teacher'";
should be
$query = "SELECT * FROM `teacher`";
"SELECT * FROM teacher"
Just remove your quotation marks.
Try this
$query = "SELECT * FROM teacher";

How to style PHP echo output with span

I'm trying to style the output of each echo.
Ideally I'd like to use <span class=""> </span> for each echo, but I'm not too sure how to achieve this.
$result = mysql_query("SELECT * FROM Blog");
while($row = mysql_fetch_array($result))
{
echo $row['Date'];
echo $row['Title'];
echo $row['Message'];
echo "<img src='".$row['Image']."'/>";
}
mysql_close($con);
$result = mysql_query("SELECT * FROM Blog");
while($row = mysql_fetch_array($result))
{
echo "<span class=\"myclass\">$row['Date']</span>";
echo "<span class=\"myclass\">$row['Title']</span>";
echo "<span class=\"myclass\">$row['Message']</span>";
echo "<img src='".$row['Image']."'/>";
}
mysql_close($con);
or, much nicer, in a table:
$result = mysql_query("SELECT * FROM Blog");
echo "<table>"
while($row = mysql_fetch_array($result)) {
echo "<tr>"
echo "<td>$row['Date']</td>";
echo "<td>$row['Title']</td>";
echo "<td>$row['Message']</td>";
echo "<td><img src='".$row['Image']."'/></td>";
echo "</tr>"
}
echo "</table>"
mysql_close($con);
You then can style each row and column with a class.
Try this:
$prepend = "<span class=''>";
$append = "</span>";
$result = mysql_query("SELECT * FROM Blog");
while($row = mysql_fetch_array($result))
{
echo $prepend.$row['Date'].$append;
echo $prepend.$row['Title'].$append;
echo $prepend. $row['Message'].$append;
echo $prepend."<img src='".$row['Image']."'/>".$append;
}
mysql_close($con);
I'd create a function that does this:
function decorated_echo($text) {
echo '<span class="myclass">' . $text . '</span>';
}
This way, you don't have to repeat this everytime you want this behaviour.
You are guessing right, just add required html in the echo:
echo '<span class="yourclass"><img src="'.$row['Image'].'" /></span>';
or you can just put inline style if no css file is loaded:
echo '<span style="color:red;"><img src="'.$row['Image'].'" /></span>';

Categories