can't get the data from database PHP - php

i created a function to get data from the database but it's not working i don't know what's wrong
here is the code :
<?php
function Connect_Show($server_name,$server_user,$server_pass,$db,$db_table){
$connect = mysql_connect($server_name,$server_user,$server_pass) or die ('error , can not connect to database ') ;
$select = mysql_Select_db($db) or die (' error , can not select the database ');
$query = mysql_query("SELECT * FROM $db_table order by id ")
while ($rows = mysql_fetch_assoc($query)) {
echo $rows['id'];
echo $rows['name'];
echo $rows['emails'];
echo $rows['password'];
}}
echo Connect_Show("localhost","root","root","learn","users");
?>

Instead of
echo Connect_Show("localhost","root","root","learn","users");
Just use this:
Connect_Show("localhost","root","root","learn","users");
Connect_Show is a function, with echo commands in it, its not returning anything.

Related

How I See my Result in Select from Where MYSQL using PHP?

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) ."%'";

PHP sorting out results from a mysqli_fetch_row array

I have a simple query to pull all the data from a table but I only need display the value once no matter how many times its in the table. Right now I get the following results:
Material in Que
9231500
9231500
9231500
Grp1298
Grp1298
6251752
6251752
What my desired result is to get:
Material in Que
9231500
6251752
Grp1298
How Do I sort out repeating numbers or Text from my query results?
For the array with column 3 from my table $row[2]
<?php session_start(); ?>
<html>
<head>
<basefont face="Arial">
<title>Material in Testing que</title>
</head>
<body>
<?php
// set database server access variables:
include('db.php');
// open connection
$connection = mysqli_connect($host, $user, $pass, $db);
if (mysqli_connect_errno($connection)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// create query
$query = "SELECT * FROM testingqa1160";
$result = mysqli_query($connection, $query);
if (mysqli_num_rows($result) > 0) {
echo "Material in Testing Que";
echo "<br>";
while($row = mysqli_fetch_row($result)) {
echo $row[2];
echo "<br>";
echo " ";
}
}
else {
// print status message
echo "<center>";
echo " Que Empty </font>";
echo "</center>";
}
mysqli_free_result($result);
// close connection
mysqli_close($connection);
?>
</body>
</html>
Use MySQL's DISTINCT in your query:
$query = "SELECT DISTINCT colname FROM testingqa1160";
you can us group by clause
$query = "SELECT * FROM testingqa1160 GROUP BY colname";
$result = mysqli_query($connection, $query);

Need to display a MySQL table on a page using PHP

I'm trying to get a table to display and keep getting an error. Could someone out there with better coding skills help me?
Here is the code:
<?php
// connect to the database
$host = "###";
$username = '###';
$pass = '###';
mysql_connect($host,$username,$pass) or die(mysql_error());
mysql_select_db("Employees") or die(mysql_error());
// select everything from the table
$query = "SELECT * FROM Employees";
$result = mysql_query($query') or die(mysql_error());
echo "<table>";
echo "<tr>";
while( ($row = mysql_fetch_array($result)))
{
echo "<td>".$row['employeeid']."</td>";
echo "<td>".$row['firstname']."</td>";
echo "<td>".$row['lastname']."</td>";
echo "<td>".$row['department']."</td>";
}
echo "</tr>";
echo "</table>";
// disconnect from the database
mysql_close();
?>
When the page runs it yields the following error:
Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /home/content/35/4683335/html/crosshill/display.php on line 36
Line 34 is: echo "<td>".$row['employeeid']."</td>";
I suggest that you need check 3 points:
You could use or die(mysql_error()) in dev environment to check if you have an error (with sql functions related).
Connection
mysql_connect($host,$username,$pass) or die(mysql_error());
Select db
mysql_select_db("Employees") or die(mysql_error());
Query (I see that you query are correct, but probably you table name are wrong, remember that names are case sensitive)
$result = mysql_query($query) or die(mysql_error());
I noticed your database is called Employees as well as your table, is this a mistake or are they both the same name?
You also have syntax error here:
$query = "SELECT * FROM Employees";
$result = mysql_query($query') or die(mysql_error());
Remove the ' so it is like this:
$result = mysql_query($query) or die(mysql_error());
Try this:
$query = "SELECT * FROM Employees";
$result = mysql_query($query) or die(mysql_error());
if(mysql_num_rows($result)< 1){
echo 'no rows founds';
} else {
echo "<table>";
echo "<tr>";
while( ($row = mysql_fetch_assoc($result)))
{
echo "<td>".$row['employeeid']."</td>";
echo "<td>".$row['firstname']."</td>";
echo "<td>".$row['lastname']."</td>";
echo "<td>".$row['department']."</td>";
}
echo "</tr>";
echo "</table>";
}
Side note:
As suggested by others you should switch to either mysqli_* or perhaps pdo.

display all data from my User table in my database

My question is how to display all data from my users table in my database?
I have this.
$loop = mysql_query(“SHOW users FROM $dbname”) or die (‘cannot select tables’);
You want to SELECT the users, not SHOW them.
Basic SQL loop example:
$sql = mysql_query("SELECT * FROM `users`");
while ($row = mysql_fetch_object($sql)) {
echo $row->id . ' ' . $row->nickname . '<br />';
}
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
$result = mysql_query(“SELECT * FROM Users”, $link) or die (‘cannot select tables’);
while ($row = mysql_fetch_assoc($result)) {
echo $row['firstname'];
echo $row['lastname'];
echo $row['address'];
echo $row['age'];
}
mysql_free_result($result);
$loop = mysql_query('SELECT * FROM `users`') or die();
You don't want your database name to be in the query, and you want to be using SELECT
If you haven't connected to the database earlier then you need to add this before your query:
mysql_connect($mysql_host, $mysql_user, $user_password);

how to write text in textbox from database in php

I have a form where I am getting values from previous page in $GET. I want to fetch around 3 different values from database and display them in textbox. How will I do it? Following is my code for getting data from database.
<?php
require_once('config.php');
$id = $_GET['id'];
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database");
}
$query = "select question,price,sequence from questions where status = 1 and qid =".$id;
//echo $query;
$result=mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
$num_rows = mysql_num_rows($result);
if($num_rows==0) {
echo '<center><font color="red"><b>No record found!!</b></font></center>';
}
else {
$row = mysql_fetch_array($result);
echo $row['question'];
echo $row['sequence'];
echo $row['price'];
}
?>
Thanks
Pankaj
<textarea><?php echo htmlentites($row['question']);?></textarea>
or
<input type="text" value="<?php echo htmlentites($row['question']);?>" />
Depending on your fancy.

Categories