Okay, i have this code
<?php
$email = htmlentities($_SESSION['user']['dato'], ENT_QUOTES, 'UTF-8');
$username = "mcnsaoia_onsafe";
$password = "XXX";
$hostname = "localhost";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
//select a database to work with
$selected = mysql_select_db("mcnsaoia_onsafe",$dbhandle)
or die("Could not select examples");
//execute the SQL query and return records
$result = mysql_query("SELECT * FROM users WHERE id= '$email'") or die(mysql_error());
//fetch tha data from the database
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['status'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "</tr>";
}
echo "</table>";
//close the connection
mysql_close($dbhandle);
?>
but when i run it, it just says Connected to MySQL and it DON'T output anything??
i have no idea why it does that?!
while($row = mysqli_fetch_array($result))
change it with mysql_fetch_array()
while($row = mysql_fetch_array($result))
Check whether rows are returned by your query using mysql_num_rows() function.
if(mysql_num_rows($result)>1)
{
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['status'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "</tr>";
}
}
else
{
echo "No rows found";
}
Note : Use mysqli_* functions . mysql_* functions have been deprecated.
Related
every time I run this code below:
<?php
$servername = "******";
$username = "******";
$password = "*******";
$dbname = "*******";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT date, point, reason, teacher, giver FROM penalty WHERE studentid=".$_GET['id'];
$result = $conn->query($sql);
if ($result["num_rows"] > 0)
{
echo "<table>";
echo "<tr>";
echo "<td>dated</td>";
echo "<td>point</td>";
echo "<td>reason</td>";
echo "<td>giver</td>";
echo "<td>teacher</td>";
echo "</tr>";
while($row = $result->fetch_assoc())
{
echo "<tr>";
echo "<td>".$row["dated"]."</td>";
echo "<td>".$row["point"]."</td>";
echo "<td>".$row["reason"]."</td>";
echo "<td>".$row["giver"]."</td>";
echo "<td>".$row["teacher"]."</td>";
echo "</tr>";
}
echo "</table>";
}
else if ($result["num_rows"] = 0)
{
echo "it is 0!";
}
else
{
echo "Unknown Error!";
}
$conn->close();
?>
I get the error message that I have written like this:
Unknown Error!
So, I'm thinking that the code has some problems in
if ($result["num_rows"] > 0)
What is the problem in the code, and what should I do to solve it?
ps. I'm a newbie in PHP, feel free to answer. Thanks in advance.
To check number of rows return from query we use
$result = $conn->query($sql);
if($result->num_rows > 0)
{
// your code
}
Instead
if ($result["num_rows"] > 0)
Your whole code would be
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table>";
echo "<tr>";
echo "<td>dated</td>";
echo "<td>point</td>";
echo "<td>reason</td>";
echo "<td>giver</td>";
echo "<td>teacher</td>";
echo "</tr>";
while ($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row["dated"] . "</td>";
echo "<td>" . $row["point"] . "</td>";
echo "<td>" . $row["reason"] . "</td>";
echo "<td>" . $row["giver"] . "</td>";
echo "<td>" . $row["teacher"] . "</td>";
echo "</tr>";
}
echo "</table>";
} else {
echo "it is 0!";
}
Read http://php.net/manual/en/mysqli-result.num-rows.php
Your code is open for sql injection
Read http://php.net/manual/en/mysqli.prepare.php Prepare statement in mysqli
I'm trying to achieve the following. On the homepage a MySQL query returns results from table "parcid"(but limited results). The next step is, I would like to hyperlink a field (preferably "ID") to open a new page using the same ID but returning more detailed results. The code below works but obviously I'm missing something somewhere as it is returning all results rather than only results for the clicked on ID. Any help will be greatly appreciated.
Home url: "http://localhost/"
Pdetail url: "http://localhost/pdetail/"
Query on "Home" page
<?php
// set database server access variables:
$host = "localhost";
$user = "root";
$pass = "";
$db = "wordpress";
// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
// select database
mysql_select_db($db) or die ("Unable to select database!");
// create query
$query = "SELECT * FROM parcid";
// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
// see if any rows were returned
if (mysql_num_rows($result) > 0) {
// yes
// print them one after another
echo "<table cellpadding=1 >";
echo "<tr>";
echo "<td>"."ID"."</td>";
echo "<td>"."City"."</td>";
echo "<td>"."Destination City"."</td>";
echo "<td>"."Weight"."</td>";
echo "<td>"."Length"."</td>";
echo "<td>"."Width"."</td>";
echo "<td>"."Height"."</td>";
echo "<td>"."Type"."</td>";
echo "<td>"."Courier Option"."</td>";
echo "</tr>";
while($row = mysql_fetch_row($result)) {
echo "<tr>";
echo '<td>'.$row[0].'</td>';
echo "<td>" . $row[6]."</td>";
echo "<td>" . $row[12]."</td>";
echo "<td>" . $row[14]."</td>";
echo "<td>" . $row[15]."</td>";
echo "<td>" . $row[16]."</td>";
echo "<td>" . $row[17]."</td>";
echo "<td>" . $row[18]."</td>";
echo "<td>" . $row[19]."</td>";
echo "<td>" . $row[20]."</td>";
echo "</tr>";
}
echo "</table>";
}
else {
// no
// print status message
echo "No rows found!";
}
// free result set memory
mysql_free_result($result);
// close connection
mysql_close($connection);
?>
Query on"pdetailed" page.
<?php
// set database server access variables:
$host = "localhost";
$user = "root";
$pass = "";
$db = "wordpress";
// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
// select database
mysql_select_db($db) or die ("Unable to select database!");
$ID = $_GET['ID'];
// create query
$query = "SELECT * FROM parcid WHERE ID LIKE '%$ID%'";
// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
// see if any rows were returned
if (mysql_num_rows($result) > 0) {
// yes
// print them one after another
echo "<table cellpadding=1 >";
echo "<tr>";
echo "<td>"."ID"."</td>";
echo "<td>"."CID"."</td>";
echo "<td>"."From"."</td>";
echo "<td>"."Street"."</td>";
echo "<td>"."Suburb"."</td>";
echo "<td>"."Post Code"."</td>";
echo "<td>"."City"."</td>";
echo "<td>"."Province"."</td>";
echo "<td>"."Receiver"."</td>";
echo "<td>"."Destination Street"."</td>";
echo "<td>"."Destination Suburb"."</td>";
echo "<td>"."Destination Postcode"."</td>";
echo "<td>"."Destination City"."</td>";
echo "<td>"."Destination Province"."</td>";
echo "<td>"."Weight"."</td>";
echo "<td>"."Length"."</td>";
echo "<td>"."Width"."</td>";
echo "<td>"."Height"."</td>";
echo "<td>"."Type"."</td>";
echo "<td>"."Courier Option"."</td>";
echo "</tr>";
while($row = mysql_fetch_row($result)) {
echo "<tr>";
echo "<td>" . $row[0]."</td>";
echo "<td>" . $row[1]."</td>";
echo "<td>" . $row[2]."</td>";
echo "<td>" . $row[3]."</td>";
echo "<td>" . $row[4]."</td>";
echo "<td>" . $row[5]."</td>";
echo "<td>" . $row[6]."</td>";
echo "<td>" . $row[7]."</td>";
echo "<td>" . $row[8]."</td>";
echo "<td>" . $row[9]."</td>";
echo "<td>" . $row[10]."</td>";
echo "<td>" . $row[11]."</td>";
echo "<td>" . $row[12]."</td>";
echo "<td>" . $row[13]."</td>";
echo "<td>" . $row[14]."</td>";
echo "<td>" . $row[15]."</td>";
echo "<td>" . $row[16]."</td>";
echo "<td>" . $row[17]."</td>";
echo "<td>" . $row[18]."</td>";
echo "<td>" . $row[19]."</td>";
echo "<td>" . $row[20]."</td>";
echo "</tr>";
}
echo "</table>";
}
else {
// no
// print status message
echo "No rows found!";
}
// free result set memory
mysql_free_result($result);
// close connection
mysql_close($connection);
?>
If you want just the single row identified by $ID then the query is
$query = "SELECT * FROM parcid WHERE ID = '$ID'";
Also when you know you should only receive a single result row from a query, you do not need to run the mysql_fetch_row($result) in a while loop.
Please dont use the mysql_ database extension, it
is deprecated (gone for ever in PHP7)
Especially if you are just learning PHP, spend your energies learning the PDO or mysqli_ database extensions,
and here is some help to decide which to use
Unless you are using a framework this statement might be wrong
echo '<td>'.$row[0].'</td>';
Maybe it should be
echo '<td>'.$row[0].'</td>';
I've been stuck with this for a while now. How do I hide a row if the second columns (svar) cell is empty on all rows where the cells under svar is not filled in?
Here is my code so far:
PHP
$localhost = "localhost";
$username = "root";
$password = "";
$connect = mysqli_connect($localhost, $username, $password)or
die("Kunde inte koppla");
mysqli_select_db($connect, 'wildfire');
$result = mysqli_query($connect,"SELECT * FROM question");
echo "<table border='1'>
<tr>
<th>Fråga</th>
<th>Svar</th>
<th>Poäng</th>
<th>Redigera</th>
<th>Radera</th>
<th>AID</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['qid'] . "</td>";
echo "<td>" . $row['answer'] . "</td>";
echo "<td>" . $row['Point'] . "</td>";
echo "<td>Redigera</td>";
echo "<td>Ta bort</td>";
echo "<td>" . $row['aid'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($connect);
JQuery
var t = $('table').parent().children().find("td:nth-child(2):empty").parent().hide();
I would update the query so you only return the records you want displayed. So change:
$result = mysqli_query($connect,"SELECT * FROM question");
to
$result = mysqli_query($connect,"SELECT * FROM question where answer <> '' && answer IS NOT NULL");
You can try this:
$('td:nth-child(2):empty').closest('tr').hide();
Here is the FIDDLE.
I've just started with PHP and literally been stuck now for 5 hours straight trying to figure this out! I understand what's happening but cannot for the life of me find a fix anywhere D: Basically each row is displayed on the users browser. Beside each one is a 'Mark as complete' button. When this button is pressed is changes the value from 0 to 1. Problem is, it changes the value of 0 to 1 for every row D: Please help me it's a pain in the but now haha! Heres my code:
<?php
// Declare Variables
$host = localhost;
$user = root;
$pass = root;
$db = test;
// Create connection to database
$link = mysqli_connect($host, $user, $pass, $db);
// Check to see if connection was established
if($link === false) {
die("Connection could not be established to database" .mysqli_error());
}
$sql = "SELECT * FROM details WHERE Status = 0";
// Show data
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>First name</th>";
echo "<th>Last name</th>";
echo "<th>Destination</th>";
echo "<th>Value</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<form action='complete.php' method='post'>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Destination'] . "</td>";
echo "<td>" . $row['Status'] . "</td>";
echo "<td>" . "<input type='submit' value='Mark as complete'>" . "</td>";
echo "</tr>";
}
echo "</table>";
}
else{
echo "No records matching your query were found.";
}
}
else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
?>
and complete.php
<?php
// Declare Variables
$host = localhost;
$user = root;
$pass = root;
$db = test;
// Create connection to database
$link = mysqli_connect($host, $user, $pass, $db);
// Check to see if connection was established
if($link === false) {
die("Connection could not be established to database" .mysqli_error());
}
// sql to delete a record
$sql = "UPDATE details SET Status=1";
if ($link == true) {
echo "Marked as complete";
} else {
echo "Error updating record: " . $link->error;
}
mysqli_close($link)
?>
You are missing some steps. Here is the full process as you want to do.
Just replace with your. I hope this will solve your problem.
<?php
// Declare Variables
$host = localhost;
$user = root;
$pass = root;
$db = test;
// Create connection to database
$link = mysqli_connect($host, $user, $pass, $db);
// Check to see if connection was established
if($link === false) {
die("Connection could not be established to database" .mysqli_error());
}
$sql = "SELECT * FROM details WHERE Status = 0";
// Show data
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>First name</th>";
echo "<th>Last name</th>";
echo "<th>Destination</th>";
echo "<th>Value</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<form action='complete.php' method='post'>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Destination'] . "</td>";
echo "<td>" . $row['Status'] . "</td>";
echo "<td>" . "<input type='submit' value='Mark as complete'>" . "</td>";
// Put your primary key column name in the place of Id
echo "<input type='hidden' name='user_id' value='".$row['Id']."'>";
echo "</form>";
echo "</tr>";
}
echo "</table>";
}
else{
echo "No records matching your query were found.";
}
}
else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
?>
and complete.php
<?php
// Declare Variables
$host = localhost;
$user = root;
$pass = root;
$db = test;
// Create connection to database
$link = mysqli_connect($host, $user, $pass, $db);
// Check to see if connection was established
if($link === false) {
die("Connection could not be established to database" .mysqli_error());
}
// sql to delete a record
// Put your primary key column name in the place of Id
$sql = "UPDATE details SET Status=1 WHERE Id='".$_POST['user_id']."' ";
if ($link == true) {
echo "Marked as complete";
} else {
echo "Error updating record: " . $link->error;
}
mysqli_close($link)
?>
I've commented out the bottom part, and the SQL query works fine. Its the displaying of the query where the error is coming from i believe.
$host = "127.0.0.1";
$user = "root";
$pass = "Toom13371!";
$connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>");
// 2. Selecting DB.
$dbname = "filters";
mysql_select_db($dbname);
// 3. Build/Test SQL Query
$sql = ("select * from filter_bandpass where start_passband=" . $_POST['Lowfreq'] . " and stop_passband='" . $_POST['Highfreq'] . "'");
//echo $sql; //Comment/Uncomment to test sql query.
// 4. Retrieve info from MySQL.
$query = mysql_query($sql);
// 5. Display Query.
echo "<table border='1'>
<tr>
<th>Low Frequency</th>
<th>High Frequency</th>
</tr>";
while($row = mysql_fetch_array($query)) {
echo "<tr>";
echo "<td>" . $row['Lowfreq'] . "</td>";
echo "<td>" . $row['Highfreq'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
Any help would be appreciated, I'm sure it's going to be some small stupid error i've over looked.
Thanks in advance :)
I'm guessing, based on your query, that you need to change this
mysql_select_db($dbname);
to
mysql_select_db($dbname, $connection);
and
while($row = mysql_fetch_array($query)) {
echo "<tr>";
echo "<td>" . $row['Lowfreq'] . "</td>";
echo "<td>" . $row['Highfreq'] . "</td>";
echo "</tr>";
}
to
while($row = mysql_fetch_array($query)) {
echo "<tr>";
echo "<td>" . $row['start_passband'] . "</td>";
echo "<td>" . $row['stop_passband'] . "</td>";
echo "</tr>";
}
Change line
mysql_select_db($dbname);
to
mysql_select_db($dbname, $connection);
Also before query check
$_POST['Lowfreq'] and $_POST['Highfreq']
If there is no value in these variables query will must return empty.
In your query there should not brackets for string.
$sql = ("select * from filter_bandpass where start_passband=" . $_POST['Lowfreq'] . " and stop_passband='" . $_POST['Highfreq'] . "'");
should be:
$sql = "select * from filter_bandpass where start_passband=" . $_POST['Lowfreq'] . " and stop_passband='" . $_POST['Highfreq'] . "'";