I'm writing a forum, and my PHP code shows all the records for the selected column on a simple row.
Code:
<td bgcolor="#FAB1CA">
<?php
$link = mysqli_connect("xxxx", "xxx", "xxxx", "xxxxxx");
if ($link === false) {
die("ERROR: Could not connect. " . mysqli_connect_error());
}
if (isset($_SESSION['logged'])) {
$sql = "Select ID from forum_question ORDER by id DESC";
$result = mysqli_query($link, $sql);
while ($row = mysqli_fetch_assoc($result)) {
$show = $row['ID'];
print_r($show);
}
}
mysqli_close($link);
?>
</td>
<td bgcolor="#FAB1CA">
<?php
$link = mysqli_connect("xxxx", "xxx", "xxxx", "xxxxxx");
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
if(isset($_SESSION['logged'])){
$sql= "Select ID from forum_question ORDER by id DESC";
$result = mysqli_query($link, $sql);
while($row = mysqli_fetch_assoc($result)){
$show = $row['ID'];
print_r($show);
echo"<br />";
}
}
mysqli_close($link);
?>
</td>
please echo a "br"-tag. hope your problem will be fixed.
Related
I have this code, i try to call my data from table database mysql , but didn't see any result. always go to else , not go to the process. what would i do?
<?php
require('connectDB.php');
$nama = $_GET['nama'];
echo $nama;
$query = "SELECT * FROM pesan
WHERE nama = '%" . mysqli_real_escape_string($connection, $nama) . "%'
";
$results = mysqli_query($connection, $query);
$baris = mysqli_num_rows($results);
if (!$results) {
die('Invalid query: ' . mysql_error());
}
if ( $baris > 0) {
while($row = mysqli_fetch_assoc($results)) {
?>
<h3>Nama Mobil : <?php echo $row['mobil'] ?></h3>
<h3>ID Pembelian : <?php echo $row['id']; ?></h3>
<h3>Nama anda : <?php echo $row['nama']; ?></h3>
<h3>Alamat : <?php echo $row['alamat']; ?></h3>
<h3>Tanggal Masuk : <?php echo $row['tgl_masuk']; ?></h3>
<?php
}
}else{
echo "error";
}
?>
What wrong with my code?
Thanks!
im sorry , this is my ConnectDB.php , i include in my html.
<?php
$connection = mysqli_connect('localhost', 'root', '', 'dealermobil');
if (!$connection){
die("Database Connection Failed" . mysqli_error());
}
// $db = new PDO ('mysql:host=localhost;dbname=db_login;charset=utf8mb4','root','');
?>
Your connection check and result check is incorrect.
$connection = mysqli_connect('localhost', 'root', '', 'dealermobil');
if (!$connection){
die("Database Connection Failed" . mysqli_connect_error());
}
Also
$results = mysqli_query($connection, $query);
$baris = mysqli_num_rows($results);
if (!$results) {
die('Invalid query: ' . mysqli_error($connection));
}
use this you are using = it should be LIKE when you are trying to search a field in database.
$query = "SELECT * FROM `pesan` WHERE `nama` LIKE '%". mysqli_real_escape_string($connection, $nama) ."%'";
I'm currently struggling with my code. I want to delete certain row from my table, but I can't figure out what's wrong.
This is my .php for delete function:
<?php
$connect = mysqli_connect("localhost", "root", "", "produktai") or die (mysql_error());
mysqli_select_db($connect,'dazai');
if (isset($_GET['recordID']))
{
$id = $_GET['recordID'];
$query = "DELETE FROM dazai WHERE id = '$id'";
header("refresh:0; url=Dazai.php");
}
else
{
echo "Not Delete";
}
?>
That's my main .php:
<?php
session_start();
if(!isset($_SESSION['uid']))
{
header("Location:signup.php");
}
$connect = mysqli_connect("localhost", "root", "", "produktai");
$query = "SELECT * FROM dazai ORDER BY id ASC";
$result = mysqli_query($connect, $query);
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
?>
<tr>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['pavad'];?></td>
<td><?php echo $row['Gamintojas'];?></td>
<td><?php echo $row['Spalva'];?></td>
<td><?php echo $row['Kiekis'];?></td>
<td><?php echo $row['Blizgumas'];?></td>
<td><?php echo $row['Kaina'];?>€</td>
<td><?php echo $row['Kategorija'];?></td>
<td><?php echo $row['sandely'];?></td>
<td>X
</tr>
<?php
}
}
?>
You probably don't want this...
mysqli_select_db($connect,'dazai');
as it is changing the database you are connected to.
$query = "DELETE FROM dazai WHERE id = '$id'";
$result = mysqli_query($connect, $query);
You miss to run the query and connect to bd in your delete.php file
Try this:
$connect = mysqli_connect("localhost", "root", "", "produktai");
if (isset($_GET['recordID'])) {
$id = $_GET['recordID'];
$query = "DELETE FROM dazai WHERE id = $id";
mysqli_query($connect, $query);
header ("refresh:0; url=Dazai.php");
}
else {
echo "Not Delete";
}
?>
I don't know why but it disregards the first row of the table.
Please help:
$row = 0;
$con = mysqli_connect("localhost", "root", "root", "db");
$query = "select * from user_accounts";
$result = mysqli_query($con, $query);
$row = mysqli_fetch_assoc($result);
while($row = $result->fetch_array())
{
echo $row['ID'] . " " . $row['Username'];
echo "<br />";
}
Two things, you're declaring an un-needed variable ($row = 0;)and you're fetching twice, which you shouldn't do:
$con = mysqli_connect("localhost", "root", "root", "db");
$query = "select * from user_accounts";
$result = mysqli_query($con, $query);
while($row = mysqli_fetch_assoc($result))
{
echo $row['ID'] . " " . $row['Username'];
echo "<br />";
}
$row = mysqli_fetch_assoc($result);
while($row = $result->fetch_array())
So, you fetch one, ignore it and then continue fetching
code:
<?php
session_start();
if ( isset($_GET['user']) && isset($_GET['pass']) )
{
$sql = "SELECT * FROM `users` WHERE `name` = '" . $_GET['user'] . "' AND `password` = '" . $_GET['pass'] . "';";
echo("query: $sql <br />");
$db = mysqli_connect("localhost", "root", "<password here>", "1596");
if (mysqli_connect_errno($db)) { die("err"); }
$result = mysqli_query($db, $sql);
echo($query);
$row = mysqli_fetch_aray($result);
echo($row);
if ($row['name'] == $_GET['user'])
{
$_SESSION['uid'] = $row['name'];
$_SESSION['level'] = $row['level'];
echo("logged in as " . $_SESSION['uid']);
}
}
else
{
die("Error, not enough parameters");
}
?>
If I run that query on server, it is fine.. there is no connect error, so wondering where I went wrong
$db = mysqli_connect("localhost", "root", "<password here>", "1596");
if (mysqli_connect_errno($db)) { die("err"); }
$result = mysqli_query($db, $sql); // line corrected
$row = mysqli_fetch_array($result); // line corrected
The table Users contains data but still it shows Records Not Found
<?php
$conn = mysql_connect("localhost", "root", "pass", "Assign1");
$records = mysql_query($conn, "select * from Users");
if(!$records)
{
echo "No Records Found";
exit();
}
while($row = mysql_fetch_array($records))
{
echo $row['name'] . " " . $row['pwd'];
echo "<br />";
}
mysql_close($conn);
?>
You have the parameters to mysql_query reversed. It should be:
$records = mysql_query("select * from Users", $conn);
Your other issue is with the if statement. You're checking if on a query, not on a result set.
Also, I'm sure you probably know but mysql libraries are deprecated and are being removed. You should really learn to use mysqli functions as they will be far more useful to you in the future.
Link to MySQLi documentation - It's really no harder than mysql libraries.
To re-implement in correct libraries:
<?php
$mysqli = new mysqli("localhost", "user", "pass", "database");
$query = $mysqli->query("SELECT * FROM users");
$results = $query->fetch_assoc();
if($results) {
foreach($results as $row) {
echo $row['name'] . " " . $row['pwd'] . "<br/>";
}
} else {
echo "No results found.";
}
?>
Hopefully I didn't just do your whole assignment for you, but it'd probably be worth it to get one more person using mysqli properly.
You have a wrong usage of mysql_query function
use it like this:
<?php
$conn = mysql_connect("localhost", "root", "pass","Assign1");
$result = mysql_query("select * from Users", $conn);
if(!$records)
{
echo "No Records Found";
exit();
}
while($row = mysql_fetch_array($result))
{
echo $row['name'] . " " . $row['pwd'];
echo "<br />";
}
mysql_close($conn);
?>
Lets resolve this issue first.The error it was actually showing is no database selected you have to select the database that needs the code
mysql_select_db("Assign1",$conn);
Hope this code will perfectly sole your issue .Try it once .........
<?php
$conn = mysql_connect("localhost", "root", "pass");
mysql_select_db("Assign1",$conn);
$result = mysql_query("select * from users", $conn);
if(!$result)
{
echo "No Records Found";
exit();
}
while($row = mysql_fetch_array($result))
{
echo $row[0]['name'];
echo "<br />";
}
mysql_close($conn);
?>
here you go
<?php
$conn = mysql_connect("localhost", "root", "pass", "Assign1");
mysql_select_db(' ----your-database-here---', $conn ) ;
$records = mysql_query($conn, "select * from Users");
if(mysql_num_rows($records) > 0 )
{
while($row = mysql_fetch_array($records))
{
echo $row['name'] . " " . $row['pwd'];
echo "<br />";
}
}else
{
echo "No Records Found";
exit();
}
mysql_close($conn);
?>