how to get my data shown in my dynamic URL with PHP - php

How should my test.php get data from a database? My database name is "example" and the things i want to get are test1, test2, test3.
<html>
<a href="test.php?ID=<?php echo $kerko_rezultatin['id_object'];?>">
</a>
</html

First you need to create a connection to you database:
$conn = mysqli_connect("localhost", "username", "pass",
"db_name");
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
Then you need to create a query to select data from your database:
$sql = "SELECT *
FROM table
WHERE 1";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($kerko_rezultatin = mysqli_fetch_assoc($result)) {
echo "<a href='test.php?ID=".$kerko_rezultatin['id_object']."'> </a>"
;}

Related

How to put data from database in a select box

I have a select box to gather the id numbers from the database, it's showing how much is in the database but the options are blank.
This is the code I have. I just want to be able to bring id number from my database in this select box.
php
<?php
$mysqli = new mysqli("localhost", "root", "", "volunteer");
/* check connection */
if (mysqli_connect_errno())
{
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT * FROM registrations";
$result = mysqli_query($mysqli,$query);
while($rows = mysqli_fetch_assoc($result))
{
echo "<option value=" . $rows['idnumber'] . "></option>";
}
?>

How to echo users from table those are logged in?

I am trying to make a list that echos only members who are currently signed in.
Been googling for an answer all day and no luck.
I have an ENUM row for each column that represents when a user is logged on or logged off as 0 or 1. But how would I go about populating a list of users which only have their ENUM value set to 1?
This is what I got:
<?php
// Create connection
include_once '/phpscripts/credentials.php';
$conn = new mysqli($db_hostname, $db_username, $db_password, $db_database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}else{
$sql="SELECT * FROM users";
$INFO=mysqli_query($conn, $sql);
While($accounts=mysqli_fetch_assoc($INFO)){
$sql = "SELECT * FROM users WHERE loggedin = '1'";
echo $accounts['username'];
echo "<br>";
}//end while
}
?>
I have modified your code. Try the code below
<?php
// Create connection
include_once '/phpscripts/credentials.php';
$conn = new mysqli($db_hostname, $db_username, $db_password, $db_database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}else{
$sql = "SELECT * FROM users WHERE loggedin = '1'";
$INFO=mysqli_query($conn, $sql);
if (mysqli_num_rows($INFO) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($INFO)) {
echo "Username: " . $row["username"]. "<br>";
}
} else {
echo "0 results";
}
}
?>

Sorting my guestbook posts on time/date (php/mySQL)

I need the posts on my guestbook to be sorted by time ascending.
I have been told that I need to add:
ORDER by datetime
in my code. But I dont know what the correct way to enter this line is.
Here is my code:
<?php
$host = "ZZZ"; // Host name
$username = "ZZZ"; // Mysql username
$password = "ZZZ"; // Mysql password
$db_name = "ZZZ"; // Database name
$tbl_name = "ZZZ"; // Table name
// Create connection
$conn = mysqli_connect($host, $username, $password, $db_name);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM ". $tbl_name ." ";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "
<b> Name: ". $row["name"]."<br>
Date Added: : ". date('d-m-Y H:i', $row["datetime"]) ."</b><br><br>
Comment: ". $row["comment"]."<br>
<br>
";
}
} else {
echo "0 results";
}
mysql_close(); //close database
?>
$sql = "SELECT * FROM " . $tbl_name . " ORDER BY datetime ASC";
Also you have mysql_close and your other functions are mysqli

Mysqli num row returned is not working

I'm wondering why it is not working for mysqli eventhou mysql_num_row is working.
if (mysql_num_rows($rows) > 0) {
echo "<p>That name has been taken </p>";
}
That is mysql. But, Im trying convert it to mysqli.
if (mysqli_num_rows($rows) > 0) {
echo "<p>That name has been taken </p>";
}
It supposed to be displayed on the screen but it's not. And there is nothing error message displayed. Or am I missing something? Any ideas?
First, a quick tutorial on some of the differences between mysql_* and mysqli_* functions.
In mysql_* you would have 3 parameters for your DB connection, then have a seperate line for your DB selection.
For example:
$db = mysql_connect("host","username", "password");
$db_selected = mysql_select_db('db_name', $db);
if (!$db_selected) {
die ('Can\'t use this : ' . mysql_error());
}
Your query would come first, followed by your DB connection.
For example:
mysql_query($query,$db);
But in mysqli_* things have changed including the parameters location. You now put all 4 parameters, for example (if you haven't done so yet):
$db = new mysqli("host","username", "password", "db_name");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
then the DB connection would come first, followed by the query instead of the other way around:
mysqli_query($db,$query);
A sample query:
$email = mysqli_real_escape_string($db,$_POST['email']);
$query = mysqli_query($db, "SELECT * FROM table_name WHERE email='".$email."'");
if(mysqli_num_rows($query) > 0){
echo "email already exists";
}else{
$sql="INSERT INTO table_name (email) VALUES ('$email')";
if (!mysqli_query($db,$sql))
{
die('Error: ' . mysqli_error($db));
}
}
You could try this code:
$connect = new mysqli("localhost", "user", "password", "database");
$query = "SELECT name FROM table";
$statement= mysqli_prepare($connect, $query)
mysqli_stmt_execute($statement);
mysqli_stmt_store_result($statement);
if (mysqli_stmt_num_rows($statement) > 0) {
echo "<p>That name has been taken </p>";
}
mysqli_stmt_close($statement);
mysqli_close($connect);

MySqli Select is not working

I couldn't figure out why it has some problem like this. The error displayed mysqli_fetch_assoc() expects parameter 1 to be mysqli_result.
My code is like this. I don't know which one is wrong.
Have a look at my con.php.
<?php
$conn = mysqli_connect('localhost', 'username', 'password');
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
?>
and this is my page.php
<?php
include 'con.php';
$link = mysqli_connect('localhost', 'username', 'password', 'username') or die("Error " . mysqli_error($link));
$query = " SELECT thread_id, thread_name, thread_date
FROM forum_thread
ORDER BY thread_date";
$result = mysqli_query($link,$query);
while ($row = mysqli_fetch_assoc($result)) {
$thread_id = $row ['thread_id'];
$thread_name = $row['thread_name'];
$thread_date = $row['thread_date'];
echo "$thread_id, $thread_name, $thread_date";
Any ideas? Appreciate any answers from you. Cheers!
I think I figured it out:
Change $result = mysqli_query($link,$query);
to $result = mysqli_query($conn,$query); since $conn is your DB connection, not $link
as per what you posted above
<?php
$conn = mysqli_connect('localhost', 'username', 'password', 'database_name');
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
?>
and remove:
$link = mysqli_connect('localhost', 'username', 'password', 'username') or die("Error " . mysqli_error($link));
since you're already loading your DB con with include 'con.php';
Or this way:
<?php
$conn = mysqli_connect('localhost', 'username', 'password', 'database_name') or die("Error " . mysqli_error($conn));
$query = " SELECT thread_id, thread_name, thread_date
FROM forum_thread
ORDER BY thread_date";
$result = mysqli_query($conn,$query);
while ($row = mysqli_fetch_assoc($result)) {
$thread_id = $row ['thread_id'];
$thread_name = $row['thread_name'];
$thread_date = $row['thread_date'];
echo "$thread_id, $thread_name, $thread_date";
The error message indicates the query did fail for some reason. Instead of just using $result test to see if it's valid and if not output the error to the screen or a log file:
if ($result) {
// continue processing $result
}
else {
echo mysqli_error($link);
}
Edit:
The 4th parameter in the mysqli_connect should be the name of the database, "abc" or whatever.
Try to use following code
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli- >connect_error;
}
Here when you try to get connection object itself you can identify the problem if something goes wrong.

Categories