Selecting a Column from a Row from a previous Query - php

I'm really sorry if this is a simple fix but I'm completely stumped, probably suffering from a temporary block.
Anyway, my predicament goes like this, my website has a page that includes events, I, of course, have an events table which has the following fields:
EventID
EventName
EventStart
EventEnd
EventDesc
Now my first query, is to make sure I collect all events that haven't ended yet. Make sense?
I am then doing a for which looks like this:
for($i=0;$i<$numr;$i++) - $numr is defined as - $numr = mysqli_num_rows($sql) and you can guess what $sql is? Just to be safe: $sql = mysqli_query($link, "SELECT * FROM events WHERE EventEnd >= NOW()")
Inside this for loop, I intend to create multiple <option> tags for however many events there are. So far so good, it looks like this: echo "<option id='" . $i . "' name='" . $i . "' value='" . $i . "'>
That's where my problem is, I know need to get the EventName of the FIRST row that was returned from the sql query. I am unsure of how to do this? My initially thought was to use an array and store the names there, however, I am not 100% familiar with arrays in php.
Is there a way you guys can help me out here? Again I'm really sorry if this is just a basic function I need to use or something but I'm having real problems right now.
Thanks so much guys!
Mike.

See fetch_assoc doc
Instead of
for($i=0;$i<$numr;$i++) {
//echoing options
}
do
while($event = $sql->fetch_assoc()) {
echo "<option id='" . $event['EventID'] . "' value='" . $event['EventName'] . "'>";
}

Outside your for loop: $first name = null;
Inside your loop:
if(empty($first_name))
$first_name = $row['EventName'];

Related

Cannot selecting first row on database php pdo mysql

I couldn't understand what is wrong here. I cannot take the first row on database. I also cannot the data which are single according to $ara parameter. Here are the codes:
$ara =$_POST['ara'];
$query=$db->prepare("SELECT * FROM uyeler WHERE
name like '%".$ara."%' or
surname like '%".$ara."%' or
email like '%".$ara."%'
");
$query->execute(array());
if ($num_row = $query->fetchAll(PDO::FETCH_BOTH)){
echo "<li><a href='profil.php?id=".$num_row['user_id']."'>" .$num_row['name']. " " .$num_row['surname']."</a></li>";
}else{
echo "Hiç birşey bulunamadı!";
}
You need to correct your array call:
from
$num_row['user_id']
To
$num_row[0]['user_id']
Your code is tested and works, nice job.
what you need to correct is the following line:
echo "<li><a href='profil.php?id=" . $num_row[0]['user_id'] . "'>" . $num_row[0]['name'] . " " . $num_row[0]['surname'] . "</a></li>";
If you face such problem in the future, it is a good practice to var_dump the results like:
var_dump($num_row);
This will give you a total raw picture of the content.

Php/mysql search whith custom Dispaly

I'm trying to make a simple search engine using php & mysql . I know how get mysql result using php. Its Work.
ex : - Mysql database stored this (database table filed "meta")
I am having some difficulty with my PHP/MySQL search. It worked great
a year ago, but for some reason it has just stopped working. I have
tried searching and cannot figure it out. I have it echo my query but
it never adds in the WHERE clause. Any help is much appreciated. Form
Code: PHP Code:
Search word is searching
I Need to dispay search result like this
I have tried searching and cannot figure it out. I have it echo my
query but it never adds in the WHERE clause. Any help is much
appreciated. Form Code: PHP Code:
$query= mysql_query("SELECT meta FROM data WHERE LIKE '%searching%' ");
$count = mysqli_num_rows($query);
if($count >0){
while($row = mysqli_fetch_array($query)) {
echo "$row[id]'>$row[name]</option>";
$count =1;
}
else{
echo "<option value='' selected='selected'></option>";
}
mysqli_close($con);
From the code above here, there's a couple of issues that stand out:
The echo for the option doesn't have an open for , so it's not going to show.
When you're echoing information from the $row variable, you're referencing id and name, but the query is only outputting meta.
The query isn't searching on a field for the where clause, and is therefore failing
The code, from what I can see, should be changed to the following to work (assuming no other surrounding issues):
$query = mysql_query("SELECT meta, id, name FROM data WHERE meta LIKE '%searching%'");
$count = mysqli_num_rows($query);
if ($count > 0)
{
while ($row = $mysqli_fetch_array($query))
{
echo "<option value='" . $row['id'] . "'>" . $row['name'] . "</option>";
$count = 1; // not sure why this needs to be here, but it's in your code so I'm keeping it there
}
}
else
{
echo "<option value='' selected='selected'></option>";
}
mysqli_close($con);

Get value from a dropdown list and insert it in the database

Hello I have a drop down list with options that a get from a table in my database and I want when the user select on of the options to store it in another table of the database. I have the code for the select by a cant find the way to get the correct value in order to store.
Here is the code for the drop down list ...
<select id="SelectDisease" name="disease">
<?php
$sql = mysql_query("SELECT name FROM disease");
while ($row = mysql_fetch_array($sql))
{
echo "<option value=$disease>" . $row['name'] . "</option>";
}
?>
</select>
And here is the insert into code ...
queryMysql("INSERT INTO patient (fname,lname,username,email,password,gender,age,disease,spid) VALUES('$fname','$lname','$username','$email', '$password','$PatientG','$PatientAge','$disease','$PatientSPID')");
die("<h4>Account created</h4>Please Log in.<br /><br />");
I'm not sure if I understand your question correctly, but here is a method:
Include the <select/> tag within a <form/> tag with an action to another PHP page.
In the second PHP page, read the variable out $disease = $_POST['disease']
Call your SQL Insert Statement
Side Note:
Perhaps change:
echo "<option value=$disease>" . $row['name'] . "</option>";
to
echo "<option value=\"" . $row['name'] . "\">" . $row['name'] . "</option>";
Is this helpful?
This is a bit of a mess, so this might be a messy post, but I'll try to clarify what I think you're trying to do. For your select input:
<select id="SelectDisease" name="disease">
<?php
$sql = mysql_query("SELECT name FROM disease");
while ($row = mysql_fetch_array($sql)){
echo "<option value='".$row['name']."'>".$row['name']."</option>";
}
?>
</select>
That way, your select's options each have a different value, and not $disease, whatever that equated to.
Next, where you process your POST request:
$fname = $_POST['fname'];
$lname = $_POST['lname'];
... // Etc for each input you have.
$disease = $_POST['disease'];
Then your query should work correctly:
queryMysql("INSERT INTO patient
(fname,lname,username,email,password,gender,age,disease,spid)
VALUES('$fname','$lname','$username','$email', '$password','$PatientG','$PatientAge','$disease','$PatientSPID')");
If that doesn't work, comment with any errors and we'll try to sort them out.
NOTE
This is INCREDIBLY insecure. I would never recommend doing this on a live application, as SQL Injections would be quite easy. Also, please note that MySQL functions are being deprecated. Look up MySQLi or PDO and learn to implement those.
Best of luck.

How do I use quotation marks properly when echoing properties and attributes through PHP and SQL)?

while ($row = $arr_result ) {
echo "<option value='" . $row['fallacytype'] . "'>" . $row['fallacytype'] . "</option>";
}
I am trying to create a and menu in html by using the string values in my SQL table.
I've confirmed that the rest of my code (not shown here, but can be provided if needed to diagnose), is working. The only thing that goes wrong is that my loop up there becomes infinite (i know this because it creates infinite sql value).
I've looked up and tried all the variations of quotation marks to no avail.
I've checked in w3shool.com, and other video tutorials that my loop syntax is correct.
It seems that the $row variable is not incrementing or moving on to the next value in the loop for some curious reason.
Thanks in advance.
You have to loop via following way for all the elements:
foreach($arr_result as $row) {
echo "<option value='" . $row['fallacytype'] . "'>" . $row['fallacytype'] . "</option>";
}

Html table returns empty after SQL query

I'm having problems with SQL query that returns no results instead of the data from the tables.
I have two tables on my DB, one is for Products and the other is Basket. The idea is to take the product id's from the basket and retrieve all the rest of the data from the product table. This is what i did:
$sql = sprintf("SELECT * FROM Basket");
$result = mysql_query($sql, $link);
while ($row = mysql_fetch_array($result)) {
$my_id = $row["Id"];
$prod_s=sprintf("SELECT * FROM Products WHERE Id='%s'",$my_id) ;
$prod= mysql_fetch_array($prod_s);
echo "<td>" . htmlentities($prod["Name"],ENT_QUOTES,"UTF-8") . "</td>";
echo "<td>" . htmlentities($prod["Size"]) . "</td>";
.
.
.
The table is being created but all fields are empty.
Thank you!
First of all, your current code is vulnerable to second-level SQL injections: if one of the IDs in the database is a malicious string (e.g. the good old ; DROP DATABASE foo), you're screwed.
Now, your actual problem is that you're not actually sending the second query to the SQL server. You'll want to run mysql_query() on it and use the result handle with mysql_fetch_array. You're already doing it correctly with the initial query. Just do the same thing again.
Finally, you might want to know that all of this can be done in a single SQL query, using joins. You may want to ask your favourite search engine about those. Good luck!
I think you still have to add a mysql_query for prod_s.
$my_id = $row["Id"];
$prod_s=sprintf("SELECT * FROM Products WHERE Id='%s'",$my_id) ;
$prod_q=mysql_query($prod_s);
$prod= mysql_fetch_array($prod_q);
echo "<td>" . htmlentities($prod["Name"],ENT_QUOTES,"UTF-8") . "</td>";
echo "<td>" . htmlentities($prod["Size"]) . "</td>";

Categories