I am able to grab the current poll_id fine and get it to echo out the correct value.
I'm also trying to echo the value from the next row in the table at the moment just the number but it's so I can insert a link to the next poll.
I keep getting the Next Poll Not Available, although I know that there is a "next" poll.
What am I doing wrong?
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT poll_id, poll_question FROM poll_main LIMIT 5, 1";
$nxt = "SELECT poll_id FROM poll_main WHERE ID > ? LIMIT 1";
$result = $conn->query($sql);
$nxtres = $conn->query($nxt);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<input type='hidden' id='poll_id' value='" . $row["poll_id"]. "'/>";
}
} else {
echo "Current Poll Not Available";
}
if ($nxtres->num_rows > 0) {
while($row = $nextres->fetch_assoc()) {
echo $row["poll_id"];
}
} else {
echo "Next Poll Not Available";
}
$conn->close();
Related
So I have a SQL column that stores data like this.
"[1338,0,8523]"
I was wondering if I can then garb each one individually and because the value is minutes, if I could times it by 60 to get the hours and then display it, I've used the below for my other results but I'm stuck on this one.
<?php
$servername = "localhost";
$username = "Time";
$password = "fakepassword";
$dbname = "time";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed to Timebase Highscores: " . $conn->connect_error);
}
$sql = "SELECT name, time FROM players ORDER BY time DESC LIMIT 5";
$result = $conn->query($sql);
echo "Top 5 Life Wasters". "<br>";
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo $row["name"]. " - $" . $row["time"] . "<br>";
}
} else {
echo "Error fetching any players from database.";
}
$conn->close();
?>
i wrote this code but there is some mistake its only shows the first echo even if the column is not empty.
i wnat the code to show echo "1" if the column subuser1 is empty
else to show echo "2" if the column subuser1 is not empty.
<?php
include_once 'dbconnect.php';
// Create connection
$conn = new mysqli(DBHOST,DBUSER,DBPASS,DBNAME);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql= ("SELECT subuser1 FROM users WHERE id=".$_SESSION['user'] );
$result = $conn->query($sql);
$subuser1 = $row["subuser1"];
if ($result->num_rows > 0) {
if (empty($subuser1)) {
echo "subuser1 is either 0, empty, or not set at all";
}
} else {
echo " subuser1 not empty";
I solve it like that it works perfect if anyone need it.
<?php
$userR= 0 ;
include_once 'dbconnect.php';
// Create connection
$conn = new mysqli(DBHOST,DBUSER,DBPASS,DBNAME);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql= ("SELECT * FROM users WHERE id=".$_SESSION['user'] );
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
if ($row["subuser1"] == NULL ){
echo ""; }
else {
echo " bla bla bla "
}
}
?>
Basically what I want is to assign the value of a field in my database to an variable. Can it be done in an effective way?
I was thinking something like:
$sql2 = mysql_query("SELECT * FROM rom WHERE idrom = 101");
while ($row = mysql_fetch_array($sql2)) {
$rom1 = $row['idrom'];
$status = $row['status'];
echo $rom1;
echo $status;
}
But this doesn't echo anything.
Edit:
I have gotten a bit longer on the way, now I am looking for a simpler way to assign the values to variables. As we speak I only need 4 values, but this still doesn't look like a very good way to accomplish what I want. Any better suggestions?
Heres what I got now:
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM rom WHERE idrom = 101";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$room101 = $row["idrom"];
$status101 = $row["status"];
echo "This is roomnumber ". $room101 . "!<br >";
echo "And the status of roomnumber ". $room101 ." is ". $status101 ."<br><br>";
}
} else {
echo "0 results";
}
$sql2 = "SELECT * FROM rom WHERE idrom = 102";
$result2 = $conn->query($sql2);
if ($result2->num_rows > 0) {
// output data of each row
while($row = $result2->fetch_assoc()) {
$room102 = $row["idrom"];
$status102 = $row["status"];
echo "This is roomnumber ". $room102 . "!<br >";
echo "And the status of roomnumber ". $room102 ." is ". $status102 ."<br><br>";
}
} else {
echo "0 results";
}
You can use bind_result to do that.
Please try this simple example, hope it run well :
$mysqli = mysqli_connect('host', 'user', 'pass','dbase')or die('Could not connect: ' . mysqli_error());
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
// Because you provide an Id in where clause, you can use it for the new variable
$output = array();
$id = 101;
$sql = "select idrom,status from rom where idrom=?";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param('i',$id);
$stmt->execute();
if ($stmt->errno == 0) {
$stmt->store_result();
// $stmt->bind_result($idrom,$status); // way 1
$stmt->bind_result($output[$id],$output["status".$id]); // way 2
while ($stmt->fetch()) {
echo $output[$id]." -> ".$output["status".$id]."<br />"; // So, your output variable will be like $output[101] for way 2
// or you defined it here like :
// $output[$idrom] = $idrom;
// $output["status".$idrom] = $status; // For way 1
}
} else {
return "Error: " . $sql . "<br>" . $stmt->error;
}
$stmt->close();
$mysqli->close();
I am looking for a way to display an error message if there is nothing listed in the table.
I have a photos table.
If this tables is empty, id like to echo something.
else, show the pictures.
inside of that table I have
id, name, url
id = id
name = name of image
url = url of image.
If there are no rows, we have an error.
$query1 = mysql_query("SELECT COUNT(*) FROM photos;");
mysql_fetch_array($query1);
if(empty($query1)) {
echo "nothing";
} else {
echo "good";
}
Try this,
$query = "SELECT * FROM photos";
$result= mysql_query($query);
$length= mysql_num_rows($result);
if($length>0)
{
while($rows = mysql_fetch_array($result))
{
echo $rows['name'];
echo "<img src='$rows[url]' />";
}
}
else
{
echo "Nothing to display";
}
Hope this will work
What about something like...
$sql = "SELECT COUNT(*) AS amountPhotos FROM photos";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
if ($row["amountPhotos"] == 0) {
echo "There are no photos in the photo table.";
}
or
$sql = "SELECT * FROM photos LIMIT 1";
$result = mysql_query($sql);
if (mysql_num_rows($result) == 0) {
echo "There are no photos in the photo table.";
}
Try this
$query1 = mysql_query("SELECT COUNT(*) FROM photos;");
$result = mysql_fetch_array($query1);
if(empty($result)) {
echo "nothing";
} else {
echo "good";
}
This pretty much sums up the answer for this question: http://www.w3schools.com/php/php_mysql_select.asp
They even provided a sample code:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);
if ($result->num_rows > 0) { //<--- here they check if number of rows returned is greater than 0 (so there is data to display)
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results"; //<----- nothing found
}
$conn->close();
?>
Just modify this and you'll be good to go.
Motive: I just want to crreate a row tih specified data if one with the same data does not exists.
Thing I have tried so far is -
CODE:
<?php
if(empty($_GET['a'])) {$xyz ="new";} else{$xyz=$_GET['a'];}
$servername = "mysql.soemwhere.com";
$username = "u130204422_acb";
$password = "YES-I-KNOW";
$dbname = "u130204422_acb";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$output ="SELECT * FROM trailers
WHERE url='$xyz' LIMIT 1";
$result = mysqli_query($conn,$output);
while($row = mysqli_fetch_array($result)) {
$pid=$row["title"];
echo $pid;}
if (mysqli_num_rows($result) > 0) {echo 'yes';}
else{
$msql = "INSERT INTO `trailers`(`url`,`title`) VALUES ('$xyz','dekhlia')";
if ($conn->query($msql) === TRUE) {
echo "New record created successfully. Refrsh the page and it will echo dekhlia";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;}
echo "hehe";
$conn->close();}
?>
I am unable to figure out why this code is'nt working.
You are unnecessarily adding concatenation in the SQL string.
Its a part of the sting, so, remove it.
Change
$output ="SELECT * FROM trailers
WHERE url='.$xyz.' LIMIT 1";
To:
$output ="SELECT * FROM trailers
WHERE url='$xyz' LIMIT 1";