Warning: mysql_fetch_row() expects parameter 1 - php

I'm a new learner in PHP. I don't know what's my error with my code. Please help me fix this. :( Warning: mysql_fetch_row() expects parameter 1 to be resource, object given in C:\xampp\htdocs\project\employees.php on line 35.
connection.php
<?php
//print_r($_POST);
// DB Credentials
$servername = "localhost";
$dbusername = "root";
$dbpassword = "";
$dbname = "project101";
// Create connection
$con = mysqli_connect($servername, $dbusername, $dbpassword, $dbname);
// Check connection
if ( $con->connect_error ) {
die("Connection failed: " . mysqli_connect_error());
}
?>
Employee.php
include ('connection.php');
if(empty($_SESSION)) // if the session not yet started
session_start();
if(!isset($_SESSION['username'])) { //if not yet logged in
header("Location: index.php");// send to login page
exit;
}
?>
<html>
<head>
<title>Employee</title>
</head>
<body>
<center>
<div id="body">
<div id="content">
<table align="center">
<tr>
<th colspan="5">add data here.</th>
</tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th colspan="2">Action</th>
</tr>
<?php
$result2 = $con->query("Select * from `employee`");
while($row=mysql_fetch_row($result2))
{
?>
<tr>
<td><?php echo $row[1]; ?></td>
<td><?php echo $row[2]; ?></td>
<td><?php echo $row[3]; ?></td>
<td align="center"><img src="b_edit.png" align="EDIT" /></td>
<td align="center"><img src="b_drop.png" align="DELETE" /></td>
</tr>
<?php
}
?>
</table>
</div>
</div>
</center>
</body>
</html>

As apparent by your commented additional code, you instanciate a mysqli_ object, but try to use a mysql_ function on it.
those two don't work together. use mysqli_ functions with it instead.

Use the following code
include ('connection.php');
if(empty($_SESSION)) // if the session not yet started
session_start();
if(!isset($_SESSION['username'])) { //if not yet logged in
header("Location: index.php");// send to login page
exit;
}
?>
<html>
<head>
<title>Employee</title>
</head>
<body>
<center>
<div id="body">
<div id="content">
<table align="center">
<tr>
<th colspan="5">add data here.</th>
</tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th colspan="2">Action</th>
</tr>
<?php
$result2 = $con->query("Select * from `employee`");
while($row=mysqli_fetch_row($result2))
{
?>
<tr>
<td><?php echo $row[1]; ?></td>
<td><?php echo $row[2]; ?></td>
<td><?php echo $row[3]; ?></td>
<td align="center"><img src="b_edit.png" align="EDIT" /></td>
<td align="center"><img src="b_drop.png" align="DELETE" /></td>
</tr>
<?php
}
?>
</table>
</div>
</div>
</center>
</body>
</html>

Related

Image is uploaded but not displayed in php

I had this project wherein, I am supposed to insert and select pictures, as in upload and view them. Initially, when I tried a code, it worked but the image didn't display. After many trials, I just copied a code from a mate which worked for him but when I tried, it didn't. Why? I have posted everything below-
Image upload code
<head>
<title>Retrieve Image</title>
</head>
<body>
<h1>Retrieving Image from database using PHP</h1>
<table border="1px" >
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Image</th>
</tr>
</thead>
<?php
$connection = mysqli_connect("localhost","root","");
$db = mysqli_select_db($connection,"olap");
$query = "SELECT * FROM `blobclob`";
$query_run = mysqli_query($connection,$query);
while($row = mysqli_fetch_array($query_run))
{
?>
<tr>
<td> <?php echo $row['id']; ?></td>
<td> <?php echo $row['name']; ?></td>
<td> <?php echo '<img
src="data:image;base64,'.base64_encode($row['image']).' " alt="FlowerImage"
style="width:100px; height:100px;">'; ?></td>
</tr>
<?php
}
?>
</table>
</body>
</html>
Image View Code
<html>
<head>
<title>Retrieve Image</title>
</head>
<body>
<h1>Retrieving Image from database using PHP</h1>
<table border="1px" >
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Image</th>
</tr>
</thead>
<?php
$connection = mysqli_connect("localhost","root","");
$db = mysqli_select_db($connection,"olap_exp");
$query = "SELECT * FROM `images`";
$query_run = mysqli_query($connection,$query);
while($row = mysqli_fetch_array($query_run))
{
?>
<tr>
<td> <?php echo $row['id']; ?></td>
<td> <?php echo $row['name']; ?></td>
<td> <?php echo '<img
src="data:image;base64,'.base64_encode($row['image']).' " alt="FlowerImage"
style="width:100px; height:100px;">'; ?></td>
</tr>
<?php
}
?>
</table>
</body>
</html>
Output
Database
it should be something like this
echo '<img src="data:image/jpeg;base64,'.base64_encode($row['image']).'"/>';
it should be **src="data:image/jpeg;base64 ... ** instead of src="data:image..

Print all table data from mysql into a html table

I've been trying to print all users into a html table. The objective is that the site administrator can edit the information.
I've done the following code so far, but I can't understand how I will print User1_Name, User1_Email and password into a table row. I want this for every user on the database.
<body>
<div class="edituser">
<h1> Edit Users </h1>
<table>
<tr>
<th>User Name</th>
<th>Email</th>
<th>Password</th>
</tr>
<?php include("include/headerhomeadmin.php");?>
<?php while($row = mysqli_fetch_assoc($result)) {
/*$line[]=$row;*/
echo
$userusername =$row['username'];
$useremail =$row['email'];
$userpassword=$row['password'];
?>
<td><?php echo implode(" ", $line)?></td>
<?php } ?>
</table>
</div>
Can someone help me?
UPDATE:
My code is now printing the first username, the email from the second user and the password from de third user.
The code is the following:
<body>
<div class="edituser">
<h1> Editar Utilizadores </h1>
<table>
<tr>
<th>Nome de Utilizador</th>
<th>Email</th>
<th>Password</th>
</tr>
<tr>
<?php while($row = mysqli_fetch_assoc($result)) {
$userusername =$row['username'];
?>
<td> <?php echo $userusername ?> </td>
<?php while($row = mysqli_fetch_assoc($result)) {
$useremail =$row['email'];
?>
<td> <?php echo $useremail ?> </td>
<?php while($row = mysqli_fetch_assoc($result)) {
$userpassword=$row['password'];
?>
<td> <?php echo $userpassword ?> </td>
<?php }
}
}?>
How can I fix this?
if ($result->num_rows > 0) {?>
<table border=1>
<tr>
<th>Name</th>
<th>Email</th>
<th>Password</th>
</tr>
<?php while($row = mysqli_fetch_assoc($result)) { ?>
<tr>
<td><?php echo $row['username']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['password']; ?></td>
</tr>
<?php }?>
</table>
<?php
}
else{
echo "0 results";
}
?>

The First Data Do Not Show When Getting Data From MySQL Table to PHP

I am trying to fetch data from an sql database into a php table. Yet the problem is that the first data from my database doesn't appear into the table. Did i commit a mistake ? The following is my code....
<?php
include "koneksi.php";
$sql = mysqli_query ($link,
"SELECT * FROM absen");
$data = mysqli_fetch_array($sql);
?>
<html>
<head>
<title>Data Mahasiswa</title>
</head>
<body>
<p><h2><b><center>DATA MAHASISWA</center></b></h2></p>
<table border="2" style="1000px;" align="center">
<tr bgcolor="blue">
<th>No</th>
<th>Nama</th>
<th>NIM</th>
<th>Jenis Kelamin</th>
</tr>
<?php
while($data)
while($data = mysqli_fetch_array($sql)){
?>
<tr>
<td><?php echo $data['no']; ?></td>
<td><?php echo $data['nama']; ?></</td>
<td><?php echo $data['nim']; ?></</td>
<td><?php echo $data['jenis_kelamin']; ?></</td>
</tr>
<?php } ?>
</table>
<center><b><h3><a href="Website.html"><img src="Capture.jpg" width="100px">
</a></h3></b></center>
</p>
</body>
</html>
You are doing mysqli_fetch_array() twice. Fixed code:
<?php
include "koneksi.php";
$sql = mysqli_query ($link,
"SELECT * FROM absen");
?>
<html>
<head>
<title>Data Mahasiswa</title>
</head>
<body>
<p><h2><b><center>DATA MAHASISWA</center></b></h2></p>
<table border="2" style="1000px;" align="center">
<tr bgcolor="blue">
<th>No</th>
<th>Nama</th>
<th>NIM</th>
<th>Jenis Kelamin</th>
</tr>
<?php
while($data = mysqli_fetch_array($sql)){
?>
<tr>
<td><?php echo $data['no']; ?></td>
<td><?php echo $data['nama']; ?></</td>
<td><?php echo $data['nim']; ?></</td>
<td><?php echo $data['jenis_kelamin']; ?></</td>
</tr>
<?php } ?>
</table>
<center><b><h3><a href="Website.html"><img src="Capture.jpg" width="100px">
</a></h3></b></center>
</p>
</body>
</html>

Use Select option as search with dynamic fields

I am making a site linked with a database. It sorts transactions for budgeting reasons. For the store search section, I have a select drop down menu. I have linked up the data so that the drop down only shows stores that are in the database and automatically adds them as they change dynamically.
My issue is that I need a way to actually use the select options as search terms. Here is the initial page.
<!DOCTYPE html>
<html>
<head>
<title>Output 1</title>
</head>
<body>
<h1>Required Output 1</h1>
<h2>Transaction Search</h2>
<form action="output1out.php" method="get">
Search Store:<br/>
<input type="text" name="StoreName">
<br/>
<input name="Add Merchant" type="submit" value="Search">
</form>
<?php
require_once 'login.php';
$connection = mysqli_connect($db_hostname, $db_username,$db_password, $db_database);
if(mysqli_connect_error()){
die("Database Connection Failed: ".mysqli_connect_error()." (".mysqli_connect_errno().")");
};
$query = "SELECT * from PURCHASE";
//echo $query;
$result = mysqli_query($connection,$query);
if(!$result) {
die("Database Query Failed!");
};
$distinct = "SELECT DISTINCT StoreName FROM PURCHASE";
$distinctResult = mysqli_query($connection,$distinct);
if(!$result) {
die("Database Query Failed!");
};
echo '<select name="search_string" />'."\n";
while ($row = mysqli_fetch_assoc($distinctResult)){
echo '<option value="'.$row["StoreID"].'">';
echo $row["StoreName"];
echo '</option>'."\n";
};
echo '</select>'."\n";
mysqli_free_result($result);
mysqli_close($connection);
?>
Here is the output page.
<?php
$transaction = $_REQUEST["StoreName"];
require_once 'login.php';
$connection = mysqli_connect($db_hostname, $db_username,$db_password, $db_database);
$sql = "SELECT * FROM PURCHASE WHERE StoreName LIKE '%".$transaction."%'";
$result = $connection->query($sql);
?>
Purchases Made From <?php echo $transaction ?>
<table border="2" style="width:100%">
<tr>
<th width="15%">Item Name</th>
<th width="15%">Item Price</th>
<th width="15%">Purchase Time</th>
<th width="15%">Purchase Date</th>
<th width="15%">Category</th>
<th width="15%">Rating</th>
</tr>
</table>
<?php
if($result->num_rows > 0){
// output data of each row
while($rows = $result->fetch_assoc()){ ?>
<table border="2" style="width:100%">
<tr>
<td width="15%"><?php echo $rows['ItemName']; ?></td>
<td width="15%"><?php echo $rows['ItemPrice']; ?></td>
<td width="15%"><?php echo $rows['PurchaseTime']; ?></td>
<td width="15%"><?php echo $rows['PurchaseDate']; ?></td>
<td width="15%"><?php echo $rows['Category']; ?></td>
<td width="15%"><?php echo $rows['Rating']; ?></td>
</tr>
<?php
}
}
?>
I can get regular typing search to work but I can't think of a way to search using the same method. Is it possible?

I'm trying to display data from MySQL onto a PHP page that contains a table but I can't get the info to display inside the table

I'm trying to display rows from a mysql database in a table but I can't get the PHP to display in the html. This is my code:
<?php
$connection = mysql_connect("localhost","root","")
or die("no connection");
$db_select=mysql_select_db("smqr",$connection)
or die("no connection to db");
$query= ("SELECT * FROM seafood");
$result=mysql_query($query)or die(mysql_error());
while
($row =mysql_fetch_array($result)):
$recipe=$row['recipe'];
$usrtext=$row['usrtext'];
$usrtxt=$row['usrtxt'];
endwhile;
?>
<body bgcolor="#ffccff">
<table align="center" width="780" height="100%" bgcolor="lightgrey" border="1">
<thead>
</thead>
<tbody>
<tr>
<th height="220">
<img src="seafoods.jpg" width="100%" height="220" /></th></tr>
<tr>
<th>Recipe Name <p>
<? echo "$recipe" ?></p></th></tr>
<tr>
<th>Ingrediants and Measurements
<p><? echo $usrtext ?></p></th></tr>
<tr>
<th>Instructions
<p> <? echo $usrtxt ?></p></th>
</tr>
I thought I had to echo the table in the while loop and I tried echo table but it didn't work so I tried to add PHP in the html which is what I have posted. When I echo $recipe before endwhile it will display the info but I need it in the table.
try this
<table align="center" width="780" height="100%" bgcolor="lightgrey" border="1">
<tr>
<td>Recipe Name </td>
<td>Ingrediants and Measurements</td>
<td>Instructions</td>
</tr>
<?php
$connection = mysql_connect("localhost","root","")or die("no connection");
$db_select=mysql_select_db("smqr",$connection) or die("no connection to db");
$query= ("SELECT * FROM seafood");
$result=mysql_query($query)or die(mysql_error());
while($row=mysql_fetch_array($result))
{
$recipe=$row['recipe'];
$usrtext=$row['usrtext'];
$usrtxt=$row['usrtxt'];
?>
<tr>
<td><?=$recipe?></td>
<td><?=$usrtext?></td>
<td><?=$usrtxt?></td>
</tr>
<?php
}// End while
?>
</table>
Check if php short tags is enabled in your php settings file. if not you can't use <? you must use <?php
example:
<?php echo $usrtext ?>
Also The while loop ended before the display table. end the while loop after display that is at the end.
Rewrite your code like this by adding
<?php ?> instead of <? ?>
<?php
$connection = mysql_connect("localhost","root","")
or die("no connection");
$db_select=mysql_select_db("test",$connection)
or die("no connection to db");
$query= ("SELECT * FROM profile");
$result=mysql_query($query)or die(mysql_error());
while
($row =mysql_fetch_array($result)):
$recipe=$row['recipe'];
$usrtext=$row['usrtext'];
$usrtxt=$row['usrtxt'];
endwhile;
?>
<body bgcolor="#ffccff">
<table align="center" width="780" height="100%" bgcolor="lightgrey" border="1">
<thead>
</thead>
<tbody>
<tr>
<th height="220">
<img src="seafoods.jpg" width="100%" height="220" /></th></tr>
<tr>
<th>Recipe Name <p>
<?php echo "$recipe" ?></p></th></tr>
<tr>
<th>Ingrediants and Measurements
<p><?php echo $usrtext ?></p></th></tr>
<tr>
<th>Instructions
<p> <?php echo $usrtxt ?></p></th>
</tr>
$recipe, $usrtext and $usrtxt are all defined within the local scope of your while loop so they won't exist after "endwhile;" anymore
<?
//define variables outside the while loop
$recipe;
$usrtext;
$usrtxt;
while($row =mysql_fetch_array($result)):
$recipe=$row['recipe'];
$usrtext=$row['usrtext'];
$usrtxt=$row['usrtxt'];
endwhile;
?>
<body bgcolor="#ffccff">
<table align="center" width="780" height="100%" bgcolor="lightgrey" border="1">
<thead>
</thead>
<tbody>
<tr>
<th height="220">
<img src="seafoods.jpg" width="100%" height="220" />
</th>
</tr>
<tr>
<th>
Recipe Name
<p> <? echo $recipe ?></p>
</th>
</tr>
<tr>
<th>
Ingrediants and Measurements
<p><? echo $usrtext ?></p>
</th>
</tr>
<tr>
<th>
Instructions
<p> <? echo $usrtxt ?></p>
</th>
</tr>
</table>
</body>
You can do it like this.
...
while($row = mysql_fetch_array($result)) {
?>
<table align="center" width="780" height="100%" bgcolor="lightgrey" border="1">
<thead>
</thead>
<tbody>
<tr>
<th height="220">
<img src="seafoods.jpg" width="100%" height="220" />
</th>
</tr>
<tr>
<th>Recipe Name <p>
<?php echo $row['recipe']; ?></p>
</th>
</tr>
<tr>
<th>Ingrediants and Measurements
<p><?php echo $row['usrtxt']; ?></p>
</th>
</tr>
<tr>
<th>Instructions
<p><?php echo $row['usrtxt']; ?></p>
</th>
</tr>
</tbody>
</table>
<?php } ?>
If your query result returns more than one row, then most probably you are looking for something like this:
<?php
$connection = mysql_connect("localhost", "root", "") or die("no connection");
$db_select = mysql_select_db("smqr", $connection) or die("no connection to db");
$query = "SELECT * FROM seafood";
$result = mysql_query($query) or die(mysql_error());
?>
<body bgcolor="#ffccff">
<table align="center" width="780" bgcolor="lightgrey" border="1">
<thead>
<tr>
<th>Recipe Name</th>
<th>Ingrediants and Measurements</th>
<th>Instructions</th>
</tr>
</thead>
<tbody>
<?php while ($row = mysql_fetch_array($result)): ?>
<tr>
<td><?php echo $row['recipe']; ?></td>
<td><?php echo $row['usrtext']; ?></td>
<td><?php echo $row['usrtxt']; ?></td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
</body>
Note: If the above code works for you, carefully follow what changes I made on your code. Hope this will help you learn writing better code. :)
You code contains some bad stuff, first of all, you shouldn't use <? ?>, I don't even know if that's valid somewhere. Use either <?= ?> or <?php echo ?> (but use <?php instead, because it's more compatible.
Secondary, it would be a good idea to use PDO library istead of MySQL, it's safer, easyer, more flexible and more compatible, use this tutorial.
Now, you can try this:
<?php
$connection = mysql_connect("localhost","root","")
or die("no connection");
$db_select=mysql_select_db("smqr",$connection)
or die("no connection to db");
$query= ("SELECT * FROM seafood");
$result=mysql_query($query)or die(mysql_error());
?>
<table>
<?php
while($row=mysql_fetch_array($result)): // everything between "while():" and "endwhile;" will be outputted in a cycle
?>
<tr>
<td>
<?php echo $row['recipe']; ?>
</td>
<td>
<?php echo $row['usrtext']; ?>
</td>
<td>
<?php echo $row['usrtxt']; ?>
</td>
</tr>
<?php endwhile; ?>
</table>
this is called inline coding

Categories