mysqli to get info from database - php

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";

Related

PHP /MYSQL Search Form Displaying Empty Results

Please Guys what must be wrong with this my code, I have tried to fetch the search data from my Database as follows
Students DATA As follow
Reg Number: Full Name: Faculty: Program: Level: Group.
on the HTML Search Page
<html>
<h2> Enter your matric number to connect to others studying your course </h2>
<form action="demo.php" method="post">
<b> ReG </b><input type="text" Name="find">
<input type="submit" value="Submit" />
</form>
</html>
PHP SIDE
<table border="1">
<tr>
<th>Student Full Name</th>
<th> Faculty</th>
<th> Program</th>
<th> Entry Year</th>
<th> Study Group</th>
<th> Group Members Contact</th>
<th> Group Leader Contacts</th>
</tr>
<?php
$conn=mysqli_connect("localhost", "root", "", "student");
// Check connection
if($conn=== false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$q = $_POST['find'];
if ($q == "")
{
echo "<p>You forgot to enter a search term!!!";
exit;
}
$sql = "SELECT * FROM study_circle WHERE matric LIKE $q ";
$result = mysqli_query($conn, $sql);
if ($result)
{
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>";
echo $row['full_name'];
echo "</td>";
echo "<td>";
echo $row['faculty'];
echo "</td>";
echo "<td>";
echo $row['program'];
echo "</td>";
echo "<td>";
echo $row['entry_year'];
echo "</td>";
echo "<td>";
echo $row['study_group'];
echo "</td>";
echo "<td>";
echo $row['group_members'];
echo "</td>";
echo "<td>";
echo $row['group_leader'];
echo "</td>";
echo "</tr>";
echo "<br/>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
Each time I try it it's bringing Empty results even though I have populated the database
Right now it's on my Localhost system, please anyone good person to help, am just new to Programming. I will be happy to fix this
Your code is misinterpreting what a false value means here:
if ($result)
{
//...
} else {
echo "0 results";
}
A false value in $result doesn't mean the search didn't find anything, it means the query failed with an error. To get the error, use mysqli_error:
if ($result)
{
//...
} else {
echo "There was an error: " . mysqli_error($conn);
}
In this case the error is probably a syntax error in your SQL code because you're directly concatenating user input to SQL code. This is also called a SQL injection vulnerability. There is some good information to get you started on correcting that here, including specifically how to use query parameters with the LIKE operator here. At its simplest you will want to use a prepared statement with a query parameter instead of using string interpolation like you do now.
Try adding wildcard characters to the start and end of the search string:
$sql = "SELECT * FROM study_circle WHERE matric LIKE '%$q%'";
You willl need to get the rowcount of the search data. That will gurantee if the record is there or not
Eg.
$rowcount = mysqli_num_rows($result);
and then perform if statement with it.
see code below.
<?php
$conn=mysqli_connect("localhost", "root", "", "student");
// Check connection
if($conn=== false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$q = $_POST['find'];
if ($q == "")
{
echo "<p>You forgot to enter a search term!!!";
exit;
}
$sql = "SELECT * FROM study_circle WHERE matric LIKE $q ";
$result = mysqli_query($conn, $sql);
$rowcount = mysqli_num_rows($result);
if( $rowcount ){
$row = mysql_fetch_array($result);
echo "<tr>";
echo "<td>";
echo $row['full_name'];
echo "</td>";
echo "<td>";
echo $row['faculty'];
echo "</td>";
echo "<td>";
echo $row['program'];
echo "</td>";
echo "<td>";
echo $row['entry_year'];
echo "</td>";
echo "<td>";
echo $row['study_group'];
echo "</td>";
echo "<td>";
echo $row['group_members'];
echo "</td>";
echo "<td>";
echo $row['group_leader'];
echo "</td>";
echo "</tr>";
echo "<br/>";
exit;
}else{
echo "0 results";
}
}
mysqli_close($conn);
?>

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 is breaking my code

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();
?>

Extracting MySQL data as table content

So I have a database from which I want to output data in the form of a table.
<table>
<tr>
<th class="tg-031e">Username</th>
<th class="tg-031e">Password</th>
</tr>
..
</table>
For now I get the data like this:
<?php include 'connect.php';
echo "<tr><td class='tg-031e'>";
$SQL = "SELECT `Username` FROM `Users` WHERE ID=1";
$exec = mysql_query($SQL, $connection);
while($row = mysql_fetch_array($exec)){
echo $row['Username'] . "</td>";
} ?>
However, in that case I need to echo out each column independently. How can I make PHP dynamically create an HTML table row with the information from the database whenever new data is present in the MySQL columns?
modify your php code like this:
<?php include 'connect.php';
$SQL = "SELECT `Username` FROM `Users` WHERE ID=1";
$exec = mysql_query($SQL, $connection);
echo "<table>";
echo "<tr>";
echo "<th class="tg-031e">Username</th>";
echo "<th class="tg-031e">Password</th>";
echo "</tr>";
while($row = mysql_fetch_array($exec)){
//add as many fields in record, i.e: username, password... etc.
echo "<tr>";
echo "<td class='tg-031e'>" . $row['Username'] . "</td>";
echo "<td class='tg-031e'>" . $row['Password'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
<?php include 'connect.php';
$SQL = "SELECT `Username`, Password FROM `Users`";
$exec = mysql_query($SQL, $connection);
echo "<table><tr><td class="tg-031e">Username</td><td class="tg-031e">Password</td></tr>";
while($row = mysql_fetch_array($exec))
{
echo "<tr><td class='tg-031e'>";
echo $row['Username'] . "</td>";
echo "<td class='tg-031e'>".$row['Password'] . "</td></tr>";
}
echo "</table>";
?>

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.

Categories